Skip to content

Commit

Permalink
Nodes: NormalNode - Improve tree shaking using TSL (#28408)
Browse files Browse the repository at this point in the history
* NormalNode: Improve tree shaking using TSL

* fix attribute default value
  • Loading branch information
sunag committed May 17, 2024
1 parent f127bdd commit 5f21c50
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 103 deletions.
2 changes: 1 addition & 1 deletion examples/jsm/nodes/Nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export { default as MorphNode, morphReference } from './accessors/MorphNode.js';
export { default as TextureBicubicNode, textureBicubic } from './accessors/TextureBicubicNode.js';
export { default as ModelNode, modelDirection, modelViewMatrix, modelNormalMatrix, modelWorldMatrix, modelPosition, modelViewPosition, modelScale } from './accessors/ModelNode.js';
export { default as ModelViewProjectionNode, modelViewProjection } from './accessors/ModelViewProjectionNode.js';
export { default as NormalNode, normalGeometry, normalLocal, normalView, normalWorld, transformedNormalView, transformedNormalWorld, transformedClearcoatNormalView } from './accessors/NormalNode.js';
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';
Expand Down
110 changes: 9 additions & 101 deletions examples/jsm/nodes/accessors/NormalNode.js
Original file line number Diff line number Diff line change
@@ -1,106 +1,14 @@
import Node, { addNodeClass } from '../core/Node.js';
import { attribute } from '../core/AttributeNode.js';
import { varying } from '../core/VaryingNode.js';
import { property } from '../core/PropertyNode.js';
import { normalize } from '../math/MathNode.js';
import { cameraViewMatrix } from './CameraNode.js';
import { modelNormalMatrix } from './ModelNode.js';
import { nodeImmutable, vec3 } from '../shadernode/ShaderNode.js';

class NormalNode extends Node {

constructor( scope = NormalNode.LOCAL ) {

super( 'vec3' );

this.scope = scope;

}

isGlobal() {

return true;

}

getHash( /*builder*/ ) {

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

}

generate( builder ) {

const scope = this.scope;

let outputNode = null;

if ( scope === NormalNode.GEOMETRY ) {

const geometryAttribute = builder.hasGeometryAttribute( 'normal' );

if ( geometryAttribute === false ) {

outputNode = vec3( 0, 1, 0 );

} else {

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

}

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

outputNode = varying( normalGeometry );

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

const vertexNode = modelNormalMatrix.mul( normalLocal );
outputNode = normalize( varying( vertexNode ) );

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

// To use inverseTransformDirection only inverse the param order like this: cameraViewMatrix.transformDirection( normalView )
const vertexNode = normalView.transformDirection( cameraViewMatrix );
outputNode = normalize( varying( vertexNode ) );

}

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;

}

}

NormalNode.GEOMETRY = 'geometry';
NormalNode.LOCAL = 'local';
NormalNode.VIEW = 'view';
NormalNode.WORLD = 'world';

export default NormalNode;

export const normalGeometry = nodeImmutable( NormalNode, NormalNode.GEOMETRY );
export const normalLocal = nodeImmutable( NormalNode, NormalNode.LOCAL ).temp( 'Normal' );
export const normalView = nodeImmutable( NormalNode, NormalNode.VIEW );
export const normalWorld = nodeImmutable( NormalNode, NormalNode.WORLD );
export const transformedNormalView = property( 'vec3', 'TransformedNormalView' );
export const transformedNormalWorld = transformedNormalView.transformDirection( cameraViewMatrix ).normalize();
export const transformedClearcoatNormalView = property( 'vec3', 'TransformedClearcoatNormalView' );

addNodeClass( 'NormalNode', NormalNode );
import { vec3 } from '../shadernode/ShaderNode.js';

export const normalGeometry = /*#__PURE__*/ attribute( 'normal', 'vec3', vec3( 0, 1, 0 ) );
export const normalLocal = /*#__PURE__*/ varying( normalGeometry ).toVar( 'normalLocal' );
export const normalView = /*#__PURE__*/ varying( modelNormalMatrix.mul( normalLocal ), 'normalView' ).normalize();
export const normalWorld = /*#__PURE__*/ varying( normalView.transformDirection( cameraViewMatrix ), 'normalWorld' ).normalize();
export const transformedNormalView = /*#__PURE__*/ property( 'vec3', 'transformedNormalView' );
export const transformedNormalWorld = /*#__PURE__*/ transformedNormalView.transformDirection( cameraViewMatrix ).normalize();
export const transformedClearcoatNormalView = /*#__PURE__*/ property( 'vec3', 'transformedClearcoatNormalView' );
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/AttributeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ class AttributeNode extends Node {

export default AttributeNode;

export const attribute = ( name, nodeType ) => nodeObject( new AttributeNode( name, nodeType ) );
export const attribute = ( name, nodeType, defaultNode ) => nodeObject( new AttributeNode( name, nodeType, nodeObject( defaultNode ) ) );

addNodeClass( 'AttributeNode', AttributeNode );

0 comments on commit 5f21c50

Please sign in to comment.