Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/Three.TSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const buffer = TSL.buffer;
export const bufferAttribute = TSL.bufferAttribute;
export const bumpMap = TSL.bumpMap;
export const burn = TSL.burn;
export const builtin = TSL.builtin;
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't exported from TSL.js? Should it be?

Copy link
Contributor

Choose a reason for hiding this comment

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

export const bvec2 = TSL.bvec2;
export const bvec3 = TSL.bvec3;
export const bvec4 = TSL.bvec4;
Expand All @@ -111,6 +112,7 @@ export const cameraPosition = TSL.cameraPosition;
export const cameraProjectionMatrix = TSL.cameraProjectionMatrix;
export const cameraProjectionMatrixInverse = TSL.cameraProjectionMatrixInverse;
export const cameraViewMatrix = TSL.cameraViewMatrix;
export const cameraViewport = TSL.cameraViewport;
export const cameraWorldMatrix = TSL.cameraWorldMatrix;
export const cbrt = TSL.cbrt;
export const cdl = TSL.cdl;
Expand Down
144 changes: 137 additions & 7 deletions src/nodes/accessors/Camera.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { uniform } from '../core/UniformNode.js';
import { renderGroup, sharedUniformGroup } from '../core/UniformGroupNode.js';
import { Vector3 } from '../../math/Vector3.js';
import { Fn } from '../tsl/TSLBase.js';
import { Fn, vec4 } from '../tsl/TSLBase.js';
import { uniformArray } from './UniformArrayNode.js';
import { builtin } from './BuiltinNode.js';
import { screenSize } from '../display/ScreenNode.js';

/**
* TSL object that represents the current `index` value of the camera if used ArrayCamera.
Expand Down Expand Up @@ -51,7 +52,7 @@ export const cameraProjectionMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

const cameraProjectionMatrices = uniformArray( matrices ).setGroup( renderGroup ).setName( 'cameraProjectionMatrices' );

cameraProjectionMatrix = cameraProjectionMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toVar( 'cameraProjectionMatrix' );
cameraProjectionMatrix = cameraProjectionMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraProjectionMatrix' );

} else {

Expand Down Expand Up @@ -85,7 +86,7 @@ export const cameraProjectionMatrixInverse = /*@__PURE__*/ ( Fn( ( { camera } )

const cameraProjectionMatricesInverse = uniformArray( matrices ).setGroup( renderGroup ).setName( 'cameraProjectionMatricesInverse' );

cameraProjectionMatrixInverse = cameraProjectionMatricesInverse.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toVar( 'cameraProjectionMatrixInverse' );
cameraProjectionMatrixInverse = cameraProjectionMatricesInverse.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraProjectionMatrixInverse' );

} else {

Expand Down Expand Up @@ -119,7 +120,7 @@ export const cameraViewMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

const cameraViewMatrices = uniformArray( matrices ).setGroup( renderGroup ).setName( 'cameraViewMatrices' );

cameraViewMatrix = cameraViewMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toVar( 'cameraViewMatrix' );
cameraViewMatrix = cameraViewMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraViewMatrix' );

} else {

Expand All @@ -137,20 +138,149 @@ export const cameraViewMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {
* @tsl
* @type {UniformNode<mat4>}
*/
export const cameraWorldMatrix = /*@__PURE__*/ uniform( 'mat4' ).setName( 'cameraWorldMatrix' ).setGroup( renderGroup ).onRenderUpdate( ( { camera } ) => camera.matrixWorld );
export const cameraWorldMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

let cameraWorldMatrix;

if ( camera.isArrayCamera && camera.cameras.length > 0 ) {

const matrices = [];

for ( const subCamera of camera.cameras ) {

matrices.push( subCamera.matrixWorld );

}

const cameraWorldMatrices = uniformArray( matrices ).setGroup( renderGroup ).setName( 'cameraWorldMatrices' );

cameraWorldMatrix = cameraWorldMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraWorldMatrix' );

} else {

cameraWorldMatrix = uniform( 'mat4' ).setName( 'cameraWorldMatrix' ).setGroup( renderGroup ).onRenderUpdate( ( { camera } ) => camera.matrixWorld );

}

return cameraWorldMatrix;

} ).once() )();

/**
* TSL object that represents the normal matrix of the camera used for the current render.
*
* @tsl
* @type {UniformNode<mat3>}
*/
export const cameraNormalMatrix = /*@__PURE__*/ uniform( 'mat3' ).setName( 'cameraNormalMatrix' ).setGroup( renderGroup ).onRenderUpdate( ( { camera } ) => camera.normalMatrix );
export const cameraNormalMatrix = /*@__PURE__*/ ( Fn( ( { camera } ) => {

let cameraNormalMatrix;

if ( camera.isArrayCamera && camera.cameras.length > 0 ) {

const matrices = [];

for ( const subCamera of camera.cameras ) {

matrices.push( subCamera.normalMatrix );

}

const cameraNormalMatrices = uniformArray( matrices ).setGroup( renderGroup ).setName( 'cameraNormalMatrices' );

cameraNormalMatrix = cameraNormalMatrices.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraNormalMatrix' );

} else {

cameraNormalMatrix = uniform( 'mat3' ).setName( 'cameraNormalMatrix' ).setGroup( renderGroup ).onRenderUpdate( ( { camera } ) => camera.normalMatrix );

}

return cameraNormalMatrix;

} ).once() )();

/**
* TSL object that represents the position in world space of the camera used for the current render.
*
* @tsl
* @type {UniformNode<vec3>}
*/
export const cameraPosition = /*@__PURE__*/ uniform( new Vector3() ).setName( 'cameraPosition' ).setGroup( renderGroup ).onRenderUpdate( ( { camera }, self ) => self.value.setFromMatrixPosition( camera.matrixWorld ) );
export const cameraPosition = /*@__PURE__*/ ( Fn( ( { camera } ) => {

let cameraPosition;

if ( camera.isArrayCamera && camera.cameras.length > 0 ) {

const positions = [];

for ( let i = 0, l = camera.cameras.length; i < l; i ++ ) {

positions.push( new Vector3() );

}

const cameraPositions = uniformArray( positions ).setGroup( renderGroup ).setName( 'cameraPositions' ).onRenderUpdate( ( { camera }, self ) => {

const subCameras = camera.cameras;
const array = self.array;

for ( let i = 0, l = subCameras.length; i < l; i ++ ) {

array[ i ].setFromMatrixPosition( subCameras[ i ].matrixWorld );

}

} );

cameraPosition = cameraPositions.element( camera.isMultiViewCamera ? builtin( 'gl_ViewID_OVR' ) : cameraIndex ).toConst( 'cameraPosition' );

} else {

cameraPosition = uniform( new Vector3() ).setName( 'cameraPosition' ).setGroup( renderGroup ).onRenderUpdate( ( { camera }, self ) => self.value.setFromMatrixPosition( camera.matrixWorld ) );

}

return cameraPosition;

} ).once() )();


/**
* TSL object that represents the viewport of the camera used for the current render.
*
* @tsl
* @type {UniformNode<vec4>}
*/
export const cameraViewport = /*@__PURE__*/ Fn( ( { camera } ) => {

let cameraViewport;

if ( camera.isArrayCamera && camera.cameras.length > 0 ) {

const viewports = [];

for ( const subCamera of camera.cameras ) {

viewports.push( subCamera.viewport );

}

const cameraViewports = uniformArray( viewports, 'vec4' )
.setGroup( renderGroup )
.setName( 'cameraViewports' );

cameraViewport = cameraViewports
.element( cameraIndex )
.toConst( 'cameraViewport' );

} else {

// Fallback for single camera
cameraViewport = vec4( 0, 0, screenSize.x, screenSize.y ).toConst( 'cameraViewport' );

}

return cameraViewport;

} ).once()();
Loading