Skip to content

Commit

Permalink
TSL: PositionNode/ReflectVectorNode - Move to TSL approach (#28510)
Browse files Browse the repository at this point in the history
* PositionNode: Move to TSL approach

* ReflectVectorNode: Move to TSL approach
  • Loading branch information
sunag authored May 28, 2024
1 parent d2a7e15 commit 66e1aa2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 133 deletions.
4 changes: 2 additions & 2 deletions examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ export { default as ModelViewProjectionNode, modelViewProjection } from './acces
export * from './accessors/NormalNode.js';
export { default as Object3DNode, objectDirection, objectViewMatrix, objectNormalMatrix, objectWorldMatrix, objectPosition, objectScale, objectViewPosition } from './accessors/Object3DNode.js';
export { default as PointUVNode, pointUV } from './accessors/PointUVNode.js';
export { default as PositionNode, positionGeometry, positionLocal, positionWorld, positionWorldDirection, positionView, positionViewDirection } from './accessors/PositionNode.js';
export * from './accessors/PositionNode.js';
export { default as ReferenceNode, reference, referenceBuffer } from './accessors/ReferenceNode.js';
export { default as ReflectVectorNode, reflectVector } from './accessors/ReflectVectorNode.js';
export * from './accessors/ReflectVectorNode.js';
export { default as SkinningNode, skinning } from './accessors/SkinningNode.js';
export { default as SceneNode, backgroundBlurriness, backgroundIntensity } from './accessors/SceneNode.js';
export { default as StorageBufferNode, storage, storageObject } from './accessors/StorageBufferNode.js';
Expand Down
106 changes: 6 additions & 100 deletions examples/jsm/nodes/accessors/PositionNode.js
Original file line number Diff line number Diff line change
@@ -1,104 +1,10 @@
import Node, { addNodeClass } from '../core/Node.js';
import { attribute } from '../core/AttributeNode.js';
import { varying } from '../core/VaryingNode.js';
import { normalize } from '../math/MathNode.js';
import { modelWorldMatrix, modelViewMatrix } from './ModelNode.js';
import { nodeImmutable } from '../shadernode/ShaderNode.js';

class PositionNode extends Node {

constructor( scope = PositionNode.LOCAL ) {

super( 'vec3' );

this.scope = scope;

}

isGlobal() {

return true;

}

getHash( /*builder*/ ) {

return `position-${this.scope}`;

}

generate( builder ) {

const scope = this.scope;

let outputNode = null;

if ( scope === PositionNode.GEOMETRY ) {

outputNode = attribute( 'position', 'vec3' );

} else if ( scope === PositionNode.LOCAL ) {

outputNode = varying( positionGeometry );

} else if ( scope === PositionNode.WORLD ) {

const vertexPositionNode = modelWorldMatrix.mul( positionLocal );
outputNode = varying( vertexPositionNode );

} else if ( scope === PositionNode.VIEW ) {

const vertexPositionNode = modelViewMatrix.mul( positionLocal );
outputNode = varying( vertexPositionNode );

} else if ( scope === PositionNode.VIEW_DIRECTION ) {

const vertexPositionNode = positionView.negate();
outputNode = normalize( varying( vertexPositionNode ) );

} else if ( scope === PositionNode.WORLD_DIRECTION ) {

const vertexPositionNode = positionLocal.transformDirection( modelWorldMatrix );
outputNode = normalize( varying( vertexPositionNode ) );

}

return outputNode.build( builder, this.getNodeType( builder ) );

}

serialize( data ) {

super.serialize( data );

data.scope = this.scope;

}

deserialize( data ) {

super.deserialize( data );

this.scope = data.scope;

}

}

PositionNode.GEOMETRY = 'geometry';
PositionNode.LOCAL = 'local';
PositionNode.WORLD = 'world';
PositionNode.WORLD_DIRECTION = 'worldDirection';
PositionNode.VIEW = 'view';
PositionNode.VIEW_DIRECTION = 'viewDirection';

export default PositionNode;

export const positionGeometry = nodeImmutable( PositionNode, PositionNode.GEOMETRY );
export const positionLocal = nodeImmutable( PositionNode, PositionNode.LOCAL ).temp( 'Position' );
export const positionWorld = nodeImmutable( PositionNode, PositionNode.WORLD );
export const positionWorldDirection = nodeImmutable( PositionNode, PositionNode.WORLD_DIRECTION );
export const positionView = nodeImmutable( PositionNode, PositionNode.VIEW );
export const positionViewDirection = nodeImmutable( PositionNode, PositionNode.VIEW_DIRECTION );

addNodeClass( 'PositionNode', PositionNode );
export const positionGeometry = /*#__PURE__*/ attribute( 'position', 'vec3' );
export const positionLocal = /*#__PURE__*/ positionGeometry.toVar( 'positionLocal' );
export const positionWorld = /*#__PURE__*/ varying( modelWorldMatrix.mul( positionLocal ).xyz, 'v_positionWorld' );
export const positionWorldDirection = /*#__PURE__*/ varying( positionLocal.transformDirection( modelWorldMatrix ), 'v_positionWorldDirection' ).normalize().toVar( 'positionWorldDirection' );
export const positionView = /*#__PURE__*/ varying( modelViewMatrix.mul( positionLocal ).xyz, 'v_positionView' );
export const positionViewDirection = /*#__PURE__*/ varying( positionView.negate(), 'v_positionViewDirection' ).normalize().toVar( 'positionViewDirection' );
33 changes: 2 additions & 31 deletions examples/jsm/nodes/accessors/ReflectVectorNode.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,6 @@
import Node, { addNodeClass } from '../core/Node.js';
import { cameraViewMatrix } from './CameraNode.js';
import { transformedNormalView } from './NormalNode.js';
import { positionViewDirection } from './PositionNode.js';
import { nodeImmutable } from '../shadernode/ShaderNode.js';

class ReflectVectorNode extends Node {

constructor() {

super( 'vec3' );

}

getHash( /*builder*/ ) {

return 'reflectVector';

}

setup() {

const reflectView = positionViewDirection.negate().reflect( transformedNormalView );

return reflectView.transformDirection( cameraViewMatrix );

}

}

export default ReflectVectorNode;

export const reflectVector = nodeImmutable( ReflectVectorNode );

addNodeClass( 'ReflectVectorNode', ReflectVectorNode );
export const reflectView = /*#__PURE__*/ positionViewDirection.negate().reflect( transformedNormalView );
export const reflectVector = /*#__PURE__*/ reflectView.transformDirection( cameraViewMatrix ).toVar( 'reflectVector' );

0 comments on commit 66e1aa2

Please sign in to comment.