Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/nodes/accessors/Camera.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { uniform } from '../core/UniformNode.js';
import { renderGroup, sharedUniformGroup } from '../core/UniformGroupNode.js';
import { Vector3 } from '../../math/Vector3.js';
import { Fn, vec4 } from '../tsl/TSLBase.js';
import { Fn, vec4, mat3 } from '../tsl/TSLBase.js';
import { uniformArray } from './UniformArrayNode.js';
import { builtin } from './BuiltinNode.js';
import { screenSize } from '../display/ScreenNode.js';
Expand Down Expand Up @@ -197,6 +197,34 @@ export const cameraViewMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

} ).once() )();

/**
* TSL object that represents the view rotation matrix of the camera used for the current render.
*
* It is the upper-left 3x3 of {@link cameraViewMatrix} (translation removed) and is typically used to transform
* direction vectors (e.g. normals) from world space into view space.
*
* @tsl
* @type {Node<mat3>}
*/
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 ) );
}
};
}


cameraViewMatrix[ 1 ].xyz,

cameraViewMatrix[ 2 ].xyz

).toVar( 'cameraViewRotationMatrix' );

/**
* TSL function that transforms a vector from world space to view space.
*
* @tsl
* @param {Node<vec3>} vector The vector to transform.
* @returns {Node<vec3>}
*/
export const worldToViewRotation = Fn( ( [ vector ] ) => cameraViewRotationMatrix.mul( vector ) );

/**
* TSL object that represents the world matrix of the camera used for the current render.
*
Expand Down
4 changes: 2 additions & 2 deletions src/nodes/accessors/Lights.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { uniform } from '../core/UniformNode.js';
import { renderGroup } from '../core/UniformGroupNode.js';
import { Vector3 } from '../../math/Vector3.js';
import { cameraViewMatrix } from './Camera.js';
import { worldToViewRotation } from './Camera.js';
import { positionWorld } from './Position.js';

let uniformsLib;
Expand Down Expand Up @@ -136,4 +136,4 @@ export function lightViewPosition( light ) {
* @param {Light} light -The light source.
* @returns {Node<vec3>} The light's target direction.
*/
export const lightTargetDirection = ( light ) => cameraViewMatrix.transformDirection( lightPosition( light ).sub( lightTargetPosition( light ) ) );
export const lightTargetDirection = ( light ) => worldToViewRotation( lightPosition( light ).sub( lightTargetPosition( light ) ) ).normalize();
4 changes: 2 additions & 2 deletions src/nodes/accessors/Normal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { attribute } from '../core/AttributeNode.js';
import { cameraViewMatrix } from './Camera.js';
import { cameraViewMatrix, worldToViewRotation } from './Camera.js';
import { modelNormalMatrix, modelWorldMatrix } from './ModelNode.js';
import { mat3, vec3, Fn } from '../tsl/TSLBase.js';
import { positionView } from './Position.js';
Expand Down Expand Up @@ -194,7 +194,7 @@ export const transformNormalToView = /*@__PURE__*/ Fn( ( [ normal ], builder ) =

const transformedNormal = modelNormalMatrix.mul( normal );

return cameraViewMatrix.transformDirection( transformedNormal );
return worldToViewRotation( transformedNormal ).normalize();

} );

Expand Down