Skip to content

Commit 28d1942

Browse files
committed
NodeBuilder: .buildFunctionNode() and getFunctionOperator()
1 parent fd57e8d commit 28d1942

File tree

6 files changed

+101
-14
lines changed

6 files changed

+101
-14
lines changed

examples/jsm/nodes/core/NodeBuilder.js

+18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import NodeCode from './NodeCode.js';
66
import NodeKeywords from './NodeKeywords.js';
77
import NodeCache from './NodeCache.js';
88
import ParameterNode from './ParameterNode.js';
9+
import FunctionNode from '../code/FunctionNode.js';
910
import { createNodeMaterialFromType } from '../materials/NodeMaterial.js';
1011
import { NodeUpdateType, defaultBuildStages, shaderStages } from './constants.js';
1112

@@ -95,6 +96,8 @@ class NodeBuilder {
9596
this.stacks = [];
9697
this.tab = '\t';
9798

99+
this.currentFunctionNode = null;
100+
98101
this.context = {
99102
keywords: new NodeKeywords(),
100103
material: this.material
@@ -848,6 +851,15 @@ class NodeBuilder {
848851

849852
}
850853

854+
buildFunctionNode( shaderNode ) {
855+
856+
const fn = new FunctionNode();
857+
858+
const previous = this.currentFunctionNode;
859+
860+
861+
}
862+
851863
flowShaderNode( shaderNode ) {
852864

853865
const layout = shaderNode.layout;
@@ -920,6 +932,12 @@ class NodeBuilder {
920932

921933
}
922934

935+
getFunctionOperator() {
936+
937+
return null;
938+
939+
}
940+
923941
flowChildNode( node, output = null ) {
924942

925943
const previousFlow = this.flow;

examples/jsm/nodes/math/OperatorNode.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -157,34 +157,47 @@ class OperatorNode extends TempNode {
157157
const b = bNode.build( builder, typeB );
158158

159159
const outputLength = builder.getTypeLength( output );
160+
const fnOpSnippet = builder.getFunctionOperator( op );
160161

161162
if ( output !== 'void' ) {
162163

163164
if ( op === '<' && outputLength > 1 ) {
164165

165-
return builder.format( `${ builder.getMethod( 'lessThan' ) }( ${a}, ${b} )`, type, output );
166+
return builder.format( `${ builder.getMethod( 'lessThan' ) }( ${ a }, ${ b } )`, type, output );
166167

167168
} else if ( op === '<=' && outputLength > 1 ) {
168169

169-
return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${a}, ${b} )`, type, output );
170+
return builder.format( `${ builder.getMethod( 'lessThanEqual' ) }( ${ a }, ${ b } )`, type, output );
170171

171172
} else if ( op === '>' && outputLength > 1 ) {
172173

173-
return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${a}, ${b} )`, type, output );
174+
return builder.format( `${ builder.getMethod( 'greaterThan' ) }( ${ a }, ${ b } )`, type, output );
174175

175176
} else if ( op === '>=' && outputLength > 1 ) {
176177

177-
return builder.format( `${ builder.getMethod( 'greaterThanEqual' ) }( ${a}, ${b} )`, type, output );
178+
return builder.format( `${ builder.getMethod( 'greaterThanEqual' ) }( ${ a }, ${ b } )`, type, output );
179+
180+
} else if ( fnOpSnippet ) {
181+
182+
return builder.format( `${ fnOpSnippet }( ${ a }, ${ b } )`, type, output );
178183

179184
} else {
180185

181-
return builder.format( `( ${a} ${this.op} ${b} )`, type, output );
186+
return builder.format( `( ${ a } ${ op } ${ b } )`, type, output );
182187

183188
}
184189

185190
} else if ( typeA !== 'void' ) {
186191

187-
return builder.format( `${a} ${this.op} ${b}`, type, output );
192+
if ( fnOpSnippet ) {
193+
194+
return builder.format( `${ fnOpSnippet }( ${ a }, ${ b } )`, type, output );
195+
196+
} else {
197+
198+
return builder.format( `${ a } ${ op } ${ b }`, type, output );
199+
200+
}
188201

189202
}
190203

examples/jsm/nodes/shadernode/ShaderNode.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,12 @@ class ShaderCallNodeInternal extends Node {
271271

272272
}
273273

274+
if ( builder.currentFunctionNode !== null ) {
275+
276+
builder.currentFunctionNode.includes.push( functionNode );
277+
278+
}
279+
274280
return nodeObject( functionNode.call( inputNodes ) );
275281

276282
}
@@ -505,7 +511,18 @@ addNodeClass( 'ShaderNode', ShaderNode );
505511

506512
//
507513

508-
export const setCurrentStack = stack => currentStack = stack;
514+
export const setCurrentStack = ( stack ) => {
515+
516+
if ( currentStack === stack ) {
517+
518+
//throw new Error( 'Stack already defined.' );
519+
520+
}
521+
522+
currentStack = stack;
523+
524+
};
525+
509526
export const getCurrentStack = () => currentStack;
510527

511528
export const If = ( ...params ) => currentStack.if( ...params );

examples/jsm/renderers/webgl-legacy/nodes/WebGLNodeBuilder.js

+30
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,36 @@ class WebGLNodeBuilder extends NodeBuilder {
439439

440440
}
441441

442+
buildFunctionCode( shaderNode ) {
443+
444+
const layout = shaderNode.layout;
445+
const flowData = this.flowShaderNode( shaderNode );
446+
447+
const parameters = [];
448+
449+
for ( const input of layout.inputs ) {
450+
451+
parameters.push( this.getType( input.type ) + ' ' + input.name );
452+
453+
}
454+
455+
//
456+
457+
const code = `${ this.getType( layout.type ) } ${ layout.name }( ${ parameters.join( ', ' ) } ) {
458+
459+
${ flowData.vars }
460+
461+
${ flowData.code }
462+
return ${ flowData.result };
463+
464+
}`;
465+
466+
//
467+
468+
return code;
469+
470+
}
471+
442472
getUniforms( shaderStage ) {
443473

444474
const uniforms = this.uniforms[ shaderStage ];

examples/jsm/renderers/webgl/nodes/GLSLNodeBuilder.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial, FunctionNode } from '../../../nodes/Nodes.js';
1+
import { MathNode, GLSLNodeParser, NodeBuilder, NodeMaterial } from '../../../nodes/Nodes.js';
22

33
import UniformBuffer from '../../common/UniformBuffer.js';
44
import NodeUniformsGroup from '../../common/nodes/NodeUniformsGroup.js';
@@ -53,7 +53,7 @@ class GLSLNodeBuilder extends NodeBuilder {
5353

5454
}
5555

56-
buildFunctionNode( shaderNode ) {
56+
buildFunctionCode( shaderNode ) {
5757

5858
const layout = shaderNode.layout;
5959
const flowData = this.flowShaderNode( shaderNode );
@@ -79,7 +79,7 @@ ${ flowData.code }
7979

8080
//
8181

82-
return new FunctionNode( code );
82+
return code;
8383

8484
}
8585

examples/jsm/renderers/webgpu/nodes/WGSLNodeBuilder.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import UniformBuffer from '../../common/UniformBuffer.js';
99
import StorageBuffer from '../../common/StorageBuffer.js';
1010
import { getVectorLength, getStrideLength } from '../../common/BufferUtils.js';
1111

12-
import { NodeBuilder, CodeNode, NodeMaterial, FunctionNode } from '../../../nodes/Nodes.js';
12+
import { NodeBuilder, CodeNode, NodeMaterial } from '../../../nodes/Nodes.js';
1313

1414
import { getFormat } from '../utils/WebGPUTextureUtils.js';
1515

@@ -492,7 +492,7 @@ class WGSLNodeBuilder extends NodeBuilder {
492492

493493
}
494494

495-
buildFunctionNode( shaderNode ) {
495+
buildFunctionCode( shaderNode ) {
496496

497497
const layout = shaderNode.layout;
498498
const flowData = this.flowShaderNode( shaderNode );
@@ -516,7 +516,7 @@ ${ flowData.code }
516516

517517
//
518518

519-
return new FunctionNode( code );
519+
return code;
520520

521521
}
522522

@@ -962,7 +962,16 @@ ${ flowData.code }
962962

963963
_include( name ) {
964964

965-
wgslPolyfill[ name ].build( this );
965+
const codeNode = wgslPolyfill[ name ];
966+
codeNode.build( this );
967+
968+
if ( this.currentFunctionNode !== null ) {
969+
970+
this.currentFunctionNode.includes.push( codeNode );
971+
972+
}
973+
974+
return codeNode;
966975

967976
}
968977

0 commit comments

Comments
 (0)