Skip to content

Commit 4437d86

Browse files
authored
TSL: Introduce subBuild() (#31260)
* TSL: Introduce `subBuild()` * add optional type * add `subBuildFn` * add docs * cleanup * add type * add docs * cleanup
1 parent 26a2768 commit 4437d86

File tree

12 files changed

+364
-101
lines changed

12 files changed

+364
-101
lines changed

src/Three.TSL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ export const mx_transform_uv = TSL.mx_transform_uv;
327327
export const mx_worley_noise_float = TSL.mx_worley_noise_float;
328328
export const mx_worley_noise_vec2 = TSL.mx_worley_noise_vec2;
329329
export const mx_worley_noise_vec3 = TSL.mx_worley_noise_vec3;
330-
export const namespace = TSL.namespace;
331330
export const negate = TSL.negate;
332331
export const neutralToneMapping = TSL.neutralToneMapping;
333332
export const nodeArray = TSL.nodeArray;
@@ -462,6 +461,7 @@ export const storageTexture = TSL.storageTexture;
462461
export const string = TSL.string;
463462
export const struct = TSL.struct;
464463
export const sub = TSL.sub;
464+
export const subBuild = TSL.subBuild;
465465
export const subgroupIndex = TSL.subgroupIndex;
466466
export const subgroupSize = TSL.subgroupSize;
467467
export const tan = TSL.tan;

src/materials/nodes/NodeMaterial.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { positionLocal, positionView } from '../../nodes/accessors/Position.js';
1313
import { skinning } from '../../nodes/accessors/SkinningNode.js';
1414
import { morphReference } from '../../nodes/accessors/MorphNode.js';
1515
import { mix } from '../../nodes/math/MathNode.js';
16-
import { namespace, float, vec3, vec4, bool } from '../../nodes/tsl/TSLBase.js';
16+
import { float, vec3, vec4, bool } from '../../nodes/tsl/TSLBase.js';
1717
import AONode from '../../nodes/lighting/AONode.js';
1818
import { lightingContext } from '../../nodes/lighting/LightingContextNode.js';
1919
import IrradianceNode from '../../nodes/lighting/IrradianceNode.js';
@@ -25,6 +25,7 @@ import getAlphaHashThreshold from '../../nodes/functions/material/getAlphaHashTh
2525
import { modelViewMatrix } from '../../nodes/accessors/ModelNode.js';
2626
import { vertexColor } from '../../nodes/accessors/VertexColorNode.js';
2727
import { premultiplyAlpha } from '../../nodes/display/BlendModes.js';
28+
import { subBuild } from '../../nodes/core/SubBuildNode.js';
2829

2930
/**
3031
* Base class for all node materials.
@@ -768,7 +769,7 @@ class NodeMaterial extends Material {
768769

769770
if ( this.positionNode !== null ) {
770771

771-
positionLocal.assign( namespace( this.positionNode, 'POSITION' ) );
772+
positionLocal.assign( subBuild( this.positionNode, 'POSITION', 'vec3' ) );
772773

773774
}
774775

src/nodes/Nodes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export { default as StructNode } from './core/StructNode.js';
3333
export { default as StructTypeNode } from './core/StructTypeNode.js';
3434
export { default as OutputStructNode } from './core/OutputStructNode.js';
3535
export { default as MRTNode } from './core/MRTNode.js';
36+
export { default as SubBuildNode } from './core/SubBuildNode.js';
3637

3738
import * as NodeUtils from './core/NodeUtils.js';
3839
export { NodeUtils };

src/nodes/accessors/Position.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ export const positionPrevious = /*@__PURE__*/ positionGeometry.toVarying( 'posit
3535
*/
3636
export const positionWorld = /*@__PURE__*/ ( Fn( ( builder ) => {
3737

38-
return modelWorldMatrix.mul( positionLocal ).xyz.toVarying( builder.getNamespace( 'v_positionWorld' ) );
38+
return modelWorldMatrix.mul( positionLocal ).xyz.toVarying( builder.getSubBuildProperty( 'v_positionWorld' ) );
3939

40-
}, 'vec3' ).once( 'POSITION' ) )();
40+
}, 'vec3' ).once( [ 'POSITION' ] ) )();
4141

4242
/**
4343
* TSL object that represents the position world direction of the current rendered object.
4444
*
4545
* @tsl
4646
* @type {Node<vec3>}
4747
*/
48-
export const positionWorldDirection = /*@__PURE__*/ ( Fn( ( builder ) => {
48+
export const positionWorldDirection = /*@__PURE__*/ ( Fn( () => {
4949

50-
const vertexPWD = positionLocal.transformDirection( modelWorldMatrix ).toVarying( builder.getNamespace( 'v_positionWorldDirection' ) );
50+
const vertexPWD = positionLocal.transformDirection( modelWorldMatrix ).toVarying( 'v_positionWorldDirection' );
5151

5252
return vertexPWD.normalize().toVar( 'positionWorldDirection' );
5353

54-
}, 'vec3' ).once( 'POSITION' ) )();
54+
}, 'vec3' ).once( [ 'POSITION' ] ) )();
5555

5656
/**
5757
* TSL object that represents the vertex position in view space of the current rendered object.
@@ -61,9 +61,9 @@ export const positionWorldDirection = /*@__PURE__*/ ( Fn( ( builder ) => {
6161
*/
6262
export const positionView = /*@__PURE__*/ ( Fn( ( builder ) => {
6363

64-
return builder.context.setupPositionView().toVarying( builder.getNamespace( 'v_positionView' ) );
64+
return builder.context.setupPositionView().toVarying( 'v_positionView' );
6565

66-
}, 'vec3' ).once( 'POSITION' ) )();
66+
}, 'vec3' ).once( [ 'POSITION' ] ) )();
6767

6868
/**
6969
* TSL object that represents the position view direction of the current rendered object.

src/nodes/core/CacheNode.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,4 @@ export default CacheNode;
9797
*/
9898
export const cache = ( node, parent ) => nodeObject( new CacheNode( nodeObject( node ), parent ) );
9999

100-
/**
101-
* Assigns a namespace to the given node by updating its context.
102-
*
103-
* Important for TSL functions that use `.once( namespace )` to ensure that the namespace will run twice,
104-
* once when the node is build in the specific namespace and once when the node is built in the others namespace.
105-
*
106-
* This is useful for nodes like `positionWorld` that need to be re-updated if used in `material.positionNode` and outside of it in the same material.
107-
*
108-
* @param {Object} node - The node to which the namespace will be assigned.
109-
* @param {string} namespace - The namespace to be assigned to the node.
110-
* @returns {Object} The updated node with the new namespace in its context.
111-
*/
112-
export const namespace = ( node, namespace ) => node.context( { namespace } );
113-
114100
addMethodChaining( 'cache', cache );

src/nodes/core/Node.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ class Node extends EventDispatcher {
735735

736736
console.warn( 'THREE.Node: Recursion detected.', this );
737737

738-
result = '';
738+
result = '/* Recursion detected. */';
739739

740740
}
741741

0 commit comments

Comments
 (0)