Skip to content

Commit 46de256

Browse files
authored
NodeBuilder: Introduce addFlowCodeHierarchy() / NodeBlock (#29495)
* VolumeNodeMaterial: simplify a little * cleanup * NodeBuilder: Introduce `addFlowCodeHierarchy()`
1 parent 8b66cd5 commit 46de256

20 files changed

+96
-32
lines changed

examples/webgpu_volume_perlin.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989

9090
const geometry = new THREE.BoxGeometry( 1, 1, 1 );
9191
const material = new THREE.VolumeNodeMaterial( {
92-
side: THREE.BackSide,
92+
side: THREE.BackSide
9393
} );
9494

9595
material.base = new THREE.Color( 0x798aa0 );
@@ -103,7 +103,7 @@
103103

104104
If( mapValue.greaterThan( threshold ), () => {
105105

106-
const p = vec3().temp().assign( probe ).addAssign( 0.5 );
106+
const p = vec3( probe ).add( 0.5 );
107107

108108
finalColor.rgb.assign( map.normal( p ).mul( 0.5 ).add( probe.mul( 1.5 ).add( 0.25 ) ) );
109109
finalColor.a.assign( 1 );

src/materials/nodes/VolumeNodeMaterial.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { materialReference } from '../../nodes/accessors/MaterialReferenceNode.j
44
import { modelWorldMatrixInverse } from '../../nodes/accessors/ModelNode.js';
55
import { cameraPosition } from '../../nodes/accessors/Camera.js';
66
import { positionGeometry } from '../../nodes/accessors/Position.js';
7-
import { Fn, varying, vec2, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';
7+
import { Fn, varying, float, vec2, vec3, vec4 } from '../../nodes/tsl/TSLBase.js';
88
import { min, max } from '../../nodes/math/MathNode.js';
99
import { Loop, Break } from '../../nodes/utils/LoopNode.js';
1010
import { texture3D } from '../../nodes/accessors/Texture3DNode.js';
@@ -59,19 +59,19 @@ class VolumeNodeMaterial extends NodeMaterial {
5959
const vDirection = varying( positionGeometry.sub( vOrigin ) );
6060

6161
const rayDir = vDirection.normalize();
62-
const bounds = property( 'vec2', 'bounds' ).assign( hitBox( { orig: vOrigin, dir: rayDir } ) );
62+
const bounds = vec2( hitBox( { orig: vOrigin, dir: rayDir } ) ).toVar();
6363

6464
bounds.x.greaterThan( bounds.y ).discard();
6565

6666
bounds.assign( vec2( max( bounds.x, 0.0 ), bounds.y ) );
6767

68-
const p = property( 'vec3', 'p' ).assign( vOrigin.add( bounds.x.mul( rayDir ) ) );
69-
const inc = property( 'vec3', 'inc' ).assign( vec3( rayDir.abs().reciprocal() ) );
70-
const delta = property( 'float', 'delta' ).assign( min( inc.x, min( inc.y, inc.z ) ) );
68+
const p = vec3( vOrigin.add( bounds.x.mul( rayDir ) ) ).toVar();
69+
const inc = vec3( rayDir.abs().reciprocal() ).toVar();
70+
const delta = float( min( inc.x, min( inc.y, inc.z ) ) ).toVar( 'delta' ); // used 'delta' name in loop
7171

7272
delta.divAssign( materialReference( 'steps', 'float' ) );
7373

74-
const ac = property( 'vec4', 'ac' ).assign( vec4( materialReference( 'base', 'color' ), 0.0 ) );
74+
const ac = vec4( materialReference( 'base', 'color' ), 0.0 ).toVar();
7575

7676
Loop( { type: 'float', start: bounds.x, end: bounds.y, update: '+= delta' }, () => {
7777

src/nodes/accessors/StorageTextureNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class StorageTextureNode extends TextureNode {
8686

8787
const snippet = builder.generateTextureStore( builder, textureProperty, uvSnippet, storeSnippet );
8888

89-
builder.addLineFlowCode( snippet );
89+
builder.addLineFlowCode( snippet, this );
9090

9191
}
9292

src/nodes/accessors/Texture3DNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const normal = Fn( ( { texture, uv } ) => {
55

66
const epsilon = 0.0001;
77

8-
const ret = vec3().temp();
8+
const ret = vec3().toVar();
99

1010
If( uv.x.lessThan( epsilon ), () => {
1111

src/nodes/accessors/TextureNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ class TextureNode extends UniformNode {
277277

278278
const snippet = this.generateSnippet( builder, textureProperty, uvSnippet, levelSnippet, biasSnippet, depthSnippet, compareSnippet, gradSnippet );
279279

280-
builder.addLineFlowCode( `${propertyName} = ${snippet}` );
280+
builder.addLineFlowCode( `${propertyName} = ${snippet}`, this );
281281

282282
nodeData.snippet = snippet;
283283
nodeData.propertyName = propertyName;

src/nodes/code/ExpressionNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ExpressionNode extends Node {
2424

2525
if ( type === 'void' ) {
2626

27-
builder.addLineFlowCode( snippet );
27+
builder.addLineFlowCode( snippet, this );
2828

2929
} else {
3030

src/nodes/core/AssignNode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ class AssignNode extends TempNode {
8080
const sourceVar = builder.getVarFromNode( this, null, targetType );
8181
const sourceProperty = builder.getPropertyName( sourceVar );
8282

83-
builder.addLineFlowCode( `${ sourceProperty } = ${ source }` );
83+
builder.addLineFlowCode( `${ sourceProperty } = ${ source }`, this );
8484

8585
const targetRoot = targetNode.node.context( { assign: true } ).build( builder );
8686

8787
for ( let i = 0; i < targetNode.components.length; i ++ ) {
8888

8989
const component = targetNode.components[ i ];
9090

91-
builder.addLineFlowCode( `${ targetRoot }.${ component } = ${ sourceProperty }[ ${ i } ]` );
91+
builder.addLineFlowCode( `${ targetRoot }.${ component } = ${ sourceProperty }[ ${ i } ]`, this );
9292

9393
}
9494

@@ -104,7 +104,7 @@ class AssignNode extends TempNode {
104104

105105
if ( output === 'void' || sourceType === 'void' ) {
106106

107-
builder.addLineFlowCode( snippet );
107+
builder.addLineFlowCode( snippet, this );
108108

109109
if ( output !== 'void' ) {
110110

src/nodes/core/BypassNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class BypassNode extends Node {
3232

3333
if ( snippet !== '' ) {
3434

35-
builder.addLineFlowCode( snippet );
35+
builder.addLineFlowCode( snippet, this );
3636

3737
}
3838

src/nodes/core/Node.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ class Node extends EventDispatcher {
361361

362362
nodeData.snippet = result;
363363

364+
} else if ( nodeData.flowCodes !== undefined && builder.context.nodeBlock !== undefined ) {
365+
366+
builder.addFlowCodeHierarchy( this, builder.context.nodeBlock );
367+
364368
}
365369

366370
result = builder.format( result, type, output );

src/nodes/core/NodeBuilder.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,59 @@ class NodeBuilder {
941941

942942
}
943943

944-
addLineFlowCode( code ) {
944+
addFlowCodeHierarchy( node, nodeBlock ) {
945+
946+
const { flowCodes, flowCodeBlock } = this.getDataFromNode( node );
947+
948+
let needsFlowCode = true;
949+
let nodeBlockHierarchy = nodeBlock;
950+
951+
while ( nodeBlockHierarchy ) {
952+
953+
if ( flowCodeBlock.get( nodeBlockHierarchy ) === true ) {
954+
955+
needsFlowCode = false;
956+
break;
957+
958+
}
959+
960+
nodeBlockHierarchy = this.getDataFromNode( nodeBlockHierarchy ).parentNodeBlock;
961+
962+
}
963+
964+
if ( needsFlowCode ) {
965+
966+
for ( const flowCode of flowCodes ) {
967+
968+
this.addLineFlowCode( flowCode );
969+
970+
}
971+
972+
}
973+
974+
}
975+
976+
addLineFlowCodeBlock( node, code, nodeBlock ) {
977+
978+
const nodeData = this.getDataFromNode( node );
979+
const flowCodes = nodeData.flowCodes || ( nodeData.flowCodes = [] );
980+
const codeBlock = nodeData.flowCodeBlock || ( nodeData.flowCodeBlock = new WeakMap() );
981+
982+
flowCodes.push( code );
983+
codeBlock.set( nodeBlock, true );
984+
985+
}
986+
987+
addLineFlowCode( code, node = null ) {
945988

946989
if ( code === '' ) return this;
947990

991+
if ( node !== null && this.context.nodeBlock ) {
992+
993+
this.addLineFlowCodeBlock( node, code, this.context.nodeBlock );
994+
995+
}
996+
948997
code = this.tab + code;
949998

950999
if ( ! /;\s*$/.test( code ) ) {

0 commit comments

Comments
 (0)