Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
13 changes: 12 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,17 @@ export const cameraViewMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

} ).once() )();

/**
* TSL object that represents the world-to-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 worldToViewRotation = /*@__PURE__*/ mat3( cameraViewMatrix[ 0 ].xyz, cameraViewMatrix[ 1 ].xyz, cameraViewMatrix[ 2 ].xyz ).toVar();

/**
* 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.mul( 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.mul( transformedNormal ).normalize();

} );

Expand Down