Skip to content

Commit

Permalink
TSL: if, elseif, else syntax (#25653)
Browse files Browse the repository at this point in the history
* WebGLNodeBuilder: Remove redundant code.

* Nodes: Added if/else
  • Loading branch information
sunag committed Mar 12, 2023
1 parent 7b3c58b commit fb2db55
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 95 deletions.
2 changes: 1 addition & 1 deletion examples/jsm/nodes/accessors/CubeTextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class CubeTextureNode extends TextureNode {

}

builder.addFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}` );

nodeData.snippet = snippet;
nodeData.propertyName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class TextureNode extends UniformNode {

}

builder.addFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}` );

nodeData.snippet = snippet;
nodeData.propertyName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/BypassNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class BypassNode extends Node {

if ( snippet !== '' ) {

builder.addFlowCode( snippet );
builder.addLineFlowCode( snippet );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/ExpressionNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ExpressionNode extends Node {

if ( type === 'void' ) {

builder.addFlowCode( snippet );
builder.addLineFlowCode( snippet );

} else {

Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class Node {
}

builder.addNode( this );
builder.addStack( this );
builder.addChain( this );

/* Build stages expected results:
- "construct" -> Node
Expand Down Expand Up @@ -233,7 +233,7 @@ class Node {

}

builder.removeStack( this );
builder.removeChain( this );

return result;

Expand Down
110 changes: 77 additions & 33 deletions examples/jsm/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class NodeBuilder {
this.varyings = [];
this.vars = { vertex: [], fragment: [], compute: [] };
this.flow = { code: '' };
this.stack = [];
this.chaining = [];
this.stack = stack();
this.tab = '\t';

this.context = {
keywords: new NodeKeywords(),
Expand All @@ -75,59 +77,59 @@ class NodeBuilder {

}

get node() {
setHashNode( node, hash ) {

return this.stack[ this.stack.length - 1 ];
this.hashNodes[ hash ] = node;

}

addStack( node ) {

/*
if ( this.stack.indexOf( node ) !== - 1 ) {
console.warn( 'Recursive node: ', node );
addNode( node ) {

}
*/
if ( this.nodes.indexOf( node ) === - 1 ) {

this.stack.push( node );
const updateType = node.getUpdateType( this );

}
if ( updateType !== NodeUpdateType.NONE ) {

removeStack( node ) {
this.updateNodes.push( node );

const lastStack = this.stack.pop();
}

if ( lastStack !== node ) {
this.nodes.push( node );

throw new Error( 'NodeBuilder: Invalid node stack!' );
this.setHashNode( node, node.getHash( this ) );

}

}

setHashNode( node, hash ) {
get currentNode() {

this.hashNodes[ hash ] = node;
return this.chaining[ this.chaining.length - 1 ];

}

addNode( node ) {
addChain( node ) {

if ( this.nodes.indexOf( node ) === - 1 ) {
/*
if ( this.chaining.indexOf( node ) !== - 1 ) {
const updateType = node.getUpdateType( this );
console.warn( 'Recursive node: ', node );
if ( updateType !== NodeUpdateType.NONE ) {
}
*/

this.updateNodes.push( node );
this.chaining.push( node );

}
}

this.nodes.push( node );
removeChain( node ) {

this.setHashNode( node, node.getHash( this ) );
const lastChain = this.chaining.pop();

if ( lastChain !== node ) {

throw new Error( 'NodeBuilder: Invalid node chaining!' );

}

Expand Down Expand Up @@ -447,9 +449,21 @@ class NodeBuilder {

}

createStack() {
addStack() {

this.stack = stack( this.stack );

return this.stack;

}

removeStack() {

const currentStack = this.stack;

return stack();
this.stack = currentStack.parent;

return currentStack;

}

Expand Down Expand Up @@ -570,16 +584,46 @@ class NodeBuilder {

}

addFlowCode( code, breakline = true ) {
addLineFlowCode( code ) {

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

if ( breakline && ! /;\s*$/.test( code ) ) {
code = this.tab + code;

code += ';\n\t';
if ( ! /;\s*$/.test( code ) ) {

code = code + ';\n';

}

this.flow.code += code;

return this;

}

addFlowCode( code ) {

this.flow.code += code;

return this;

}

addFlowTab() {

this.tab += '\t';

return this;

}

removeFlowTab() {

this.tab = this.tab.slice( 0, - 1 );

return this;

}

getFlowData( node/*, shaderStage*/ ) {
Expand Down Expand Up @@ -628,7 +672,7 @@ class NodeBuilder {

if ( propertyName !== null ) {

flowData.code += `${propertyName} = ${flowData.result};\n\t`;
flowData.code += `${propertyName} = ${flowData.result};\n` + this.tab;

}

Expand Down
38 changes: 36 additions & 2 deletions examples/jsm/nodes/core/StackNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import Node, { addNodeClass } from './Node.js';
import { assign } from '../math/OperatorNode.js';
import { bypass } from '../core/BypassNode.js';
import { expression } from '../core/ExpressionNode.js';
import { nodeProxy } from '../shadernode/ShaderNode.js';
import { cond } from '../math/CondNode.js';
import { nodeProxy, shader } from '../shadernode/ShaderNode.js';

class StackNode extends Node {

constructor() {
constructor( parent = null ) {

super();

this.nodes = [];
this.outputNode = null;

this.parent = parent;

this._currentCond = null;

this.isStackNode = true;

}
Expand All @@ -31,6 +36,35 @@ class StackNode extends Node {

}

if( boolNode, method ) {

const methodNode = shader( method );
this._currentCond = cond( boolNode, methodNode );

return this.add( this._currentCond );

}

elseif( boolNode, method ) {

const methodNode = shader( method );
const ifNode = cond( boolNode, methodNode );

this._currentCond.elseNode = ifNode;
this._currentCond = ifNode;

return this;

}

else( method ) {

this._currentCond.elseNode = shader( method );

return this;

}

assign( targetNode, sourceValue ) {

return this.add( assign( targetNode, sourceValue ) );
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/TempNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TempNode extends Node {
const nodeVar = builder.getVarFromNode( this, type );
const propertyName = builder.getPropertyName( nodeVar );

builder.addFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}` );

nodeData.snippet = snippet;
nodeData.propertyName = propertyName;
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class VarNode extends Node {

const propertyName = builder.getPropertyName( nodeVar );

builder.addFlowCode( `${propertyName} = ${snippet}` );
builder.addLineFlowCode( `${propertyName} = ${snippet}` );

return propertyName;

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/gpgpu/ComputeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ComputeNode extends Node {

if ( snippet !== '' ) {

builder.addFlowCode( snippet );
builder.addLineFlowCode( snippet );

}

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/materials/MeshNormalNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MeshNormalNodeMaterial extends NodeMaterial {

}

constructDiffuseColor( builder, stack ) {
constructDiffuseColor( { stack } ) {

const opacityNode = this.opacityNode ? float( this.opacityNode ) : materialOpacity;

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/materials/MeshPhongNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MeshPhongNodeMaterial extends NodeMaterial {

}

constructVariants( builder, stack ) {
constructVariants( { stack } ) {

// SHININESS

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/materials/MeshStandardNodeMaterial.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class MeshStandardNodeMaterial extends NodeMaterial {

}

constructVariants( builder, stack ) {
constructVariants( { stack } ) {

// METALNESS

Expand Down
Loading

0 comments on commit fb2db55

Please sign in to comment.