Skip to content

TSL: cameraViewRotationMatrix mat3#33115

Open
PoseidonEnergy wants to merge 6 commits intomrdoob:devfrom
PoseidonEnergy:tsl-world-to-view-rotation-1
Open

TSL: cameraViewRotationMatrix mat3#33115
PoseidonEnergy wants to merge 6 commits intomrdoob:devfrom
PoseidonEnergy:tsl-world-to-view-rotation-1

Conversation

@PoseidonEnergy
Copy link
Contributor

@PoseidonEnergy PoseidonEnergy commented Mar 3, 2026

Description

Adds cameraViewRotationMatrix, a mat3 extracted from the upper-left 3x3 of cameraViewMatrix, for transforming direction vectors from world to view space. Helps avoid repeated full 4x4 multiply mat4 * vec4(v, 0) patterns and simplifies generated shader code.

  • Avoids expensive calls to cameraViewMatrix.transformDirection() which always normalizes the output, even if the value was already normalized.
  • Makes using material.normalNode a little simpler and clearer:

Before

material.normalNode = tsl.Fn(() => {
  const normalWorld = tsl.vec3(0, 1, 0);
  return tsl.cameraViewMatrix.mul(tsl.vec4(normalWorld, 0)).xyz;
})();

After

material.normalNode = tsl.Fn(() => {
  const normalWorld = tsl.vec3(0, 1, 0);
  return tsl.cameraViewRotationMatrix.mul(normalWorld);
})();

@github-actions
Copy link

github-actions bot commented Mar 3, 2026

📦 Bundle size

Full ESM build, minified and gzipped.

Before After Diff
WebGL 359.27
85.31
359.27
85.31
+0 B
+0 B
WebGPU 630.28
175
630.07
175.03
-215 B
+36 B
WebGPU Nodes 628.86
174.75
628.65
174.78
-215 B
+36 B

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Before After Diff
WebGL 491.03
119.72
491.03
119.72
+0 B
+0 B
WebGPU 703.88
190.04
703.63
190.03
-250 B
-7 B
WebGPU Nodes 653.11
177.48
652.84
177.41
-271 B
-69 B

@PoseidonEnergy PoseidonEnergy changed the title TSL: worldToViewRotation mat3 TSL: cameraViewRotationMatrix mat3 Mar 4, 2026
@PoseidonEnergy PoseidonEnergy changed the title TSL: cameraViewRotationMatrix mat3 TSL: cameraViewRotationMatrix mat3 Mar 4, 2026
@sunag sunag added this to the r184 milestone Mar 5, 2026
@sunag sunag requested a review from WestLangley March 5, 2026 03:16
*/
export const cameraViewRotationMatrix = /*@__PURE__*/ mat3(

cameraViewMatrix[ 0 ].xyz,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the [] access work in a TSL object?

Copy link
Contributor Author

@PoseidonEnergy PoseidonEnergy Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TSL simulates an indexer by defining properties 0–31 on the node:

// Set/get static properties for array elements (0-31).
for ( let i = 0; i < 32; i ++ ) {
proto[ i ] = {
get() {
this._cache = this._cache || {};
//
let element = this._cache[ i ];
if ( element === undefined ) {
element = new ArrayElementNode( this, new ConstNode( i, 'uint' ) );
this._cache[ i ] = element;
}
return element;
},
set( value ) {
this[ i ].assign( nodeObject( value ) );
}
};
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants