Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor VarNode #26821

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/jsm/nodes/accessors/CubeTextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class CubeTextureNode extends TextureNode {

} else {

const nodeType = this.getNodeType( builder );
const nodeData = builder.getDataFromNode( this );

let propertyName = nodeData.propertyName;
Expand All @@ -64,7 +63,7 @@ class CubeTextureNode extends TextureNode {
const cubeUV = vec3( uvNode.x.negate(), uvNode.yz );
const uvSnippet = cubeUV.build( builder, 'vec3' );

const nodeVar = builder.getVarFromNode( this, 'vec4' );
const nodeVar = builder.getVarFromNode( this );

propertyName = builder.getPropertyName( nodeVar );

Expand Down Expand Up @@ -94,14 +93,15 @@ class CubeTextureNode extends TextureNode {
}

let snippet = propertyName;
const nodeType = this.getNodeType( builder );

if ( builder.needsColorSpaceToLinear( this.value ) ) {

snippet = colorSpaceToLinear( expression( snippet, nodeType ), this.value.colorSpace ).setup( builder ).build( builder, nodeType );

}

return builder.format( snippet, 'vec4', output );
return builder.format( snippet, nodeType, output );

}

Expand Down
4 changes: 2 additions & 2 deletions examples/jsm/nodes/accessors/TextureNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,14 @@ class TextureNode extends UniformNode {

} else {

const nodeType = this.getNodeType( builder );
const nodeData = builder.getDataFromNode( this );

let propertyName = nodeData.propertyName;

if ( propertyName === undefined ) {

const uvSnippet = uvNode.build( builder, 'vec2' );
const nodeVar = builder.getVarFromNode( this, nodeType );
const nodeVar = builder.getVarFromNode( this );

propertyName = builder.getPropertyName( nodeVar );

Expand Down Expand Up @@ -184,6 +183,7 @@ class TextureNode extends UniformNode {
}

let snippet = propertyName;
const nodeType = this.getNodeType( builder );

if ( builder.needsColorSpaceToLinear( this.value ) ) {

Expand Down
19 changes: 7 additions & 12 deletions examples/jsm/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,9 @@ class Node extends EventDispatcher {

const self = this;

for ( const { property, index, childNode } of getNodeChildren( this ) ) {
for ( const { childNode } of getNodeChildren( this ) ) {

yield { childNode, replaceNode( node ) {

if ( index === undefined ) self[ property ] = node;
else self[ property ][ index ] = node;

} };
yield childNode;

}

Expand All @@ -75,13 +70,13 @@ class Node extends EventDispatcher {

}

traverse( callback, replaceNode = null ) {
traverse( callback ) {

callback( this, replaceNode );
callback( this );

for ( const { childNode, replaceNode } of this.getChildren() ) {
for ( const childNode of this.getChildren() ) {

childNode.traverse( callback, replaceNode );
childNode.traverse( callback );

}

Expand Down Expand Up @@ -138,7 +133,7 @@ class Node extends EventDispatcher {

const nodeProperties = builder.getNodeProperties( this );

for ( const { childNode } of this.getChildren() ) {
for ( const childNode of this.getChildren() ) {

nodeProperties[ '_node' + childNode.id ] = childNode;

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

}

getVarFromNode( node, type, shaderStage = this.shaderStage ) {
getVarFromNode( node, name = null, type = node.getNodeType( this ), shaderStage = this.shaderStage ) {

const nodeData = this.getDataFromNode( node, shaderStage );

Expand All @@ -685,9 +685,10 @@ class NodeBuilder {
if ( nodeVar === undefined ) {

const vars = this.vars[ shaderStage ];
const index = vars.length;

nodeVar = new NodeVar( 'nodeVar' + index, type );
if ( name === null ) name = 'nodeVar' + vars.length;

nodeVar = new NodeVar( name, type );

vars.push( nodeVar );

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

generate( builder, output ) {

const nodeVar = builder.getVarFromNode( this, this.nodeType );
const nodeVar = builder.getVarFromNode( this );
nodeVar.isOutputStructVar = true;

const propertyName = builder.getPropertyName( nodeVar );
Expand Down
11 changes: 1 addition & 10 deletions examples/jsm/nodes/core/PropertyNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,7 @@ class PropertyNode extends Node {

generate( builder ) {

const nodeVary = builder.getVarFromNode( this, this.getNodeType( builder ) );
const name = this.name;

if ( name !== null ) {

nodeVary.name = name;

}

return builder.getPropertyName( nodeVary );
return builder.getPropertyName( builder.getVarFromNode( this, this.name ) );

}

Expand Down
34 changes: 1 addition & 33 deletions examples/jsm/nodes/core/StackNode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import Node, { addNodeClass } from './Node.js';
import { assign } from '../math/OperatorNode.js';
import { bypass } from '../core/BypassNode.js';
import { expression } from '../code/ExpressionNode.js';
import { cond } from '../math/CondNode.js';
import { loop } from '../utils/LoopNode.js';
import SetNode from '../utils/SetNode.js';
import { ShaderNode, nodeProxy, nodeObject } from '../shadernode/ShaderNode.js';
import { ShaderNode, nodeProxy } from '../shadernode/ShaderNode.js';

class StackNode extends Node {

Expand Down Expand Up @@ -67,35 +64,6 @@ class StackNode extends Node {

}

assign( targetNode, sourceValue ) {

sourceValue = nodeObject( sourceValue );

if ( targetNode.isSplitNode ) {

sourceValue = new SetNode( targetNode.node, targetNode.components, sourceValue );
targetNode = targetNode.node;

}

if ( targetNode.isPropertyNode !== true && targetNode.isVarNode !== true && targetNode.isArrayElementNode !== true && targetNode.isVaryingNode !== true ) {

console.error( 'THREE.TSL: Invalid assign, target must be a property or variable.', targetNode.getSelf() );

//return this;

}

return this.add( assign( targetNode, sourceValue ) );

}

loop( ...params ) {

return this.add( loop( ...params ) );

}

build( builder, ...params ) {

for ( const node of this.nodes ) {
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 @@ -33,7 +33,7 @@ class TempNode extends Node {

const snippet = super.build( builder, type );

const nodeVar = builder.getVarFromNode( this, type );
const nodeVar = builder.getVarFromNode( this, null, type );
const propertyName = builder.getPropertyName( nodeVar );

builder.addLineFlowCode( `${propertyName} = ${snippet}` );
Expand Down
15 changes: 3 additions & 12 deletions examples/jsm/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class VarNode extends Node {
this.node = node;
this.name = name;

this.isVarNode = true;

}

isGlobal() {
Expand All @@ -36,19 +34,12 @@ class VarNode extends Node {

const { node, name } = this;

const type = builder.getVectorType( this.getNodeType( builder ) );

const snippet = node.build( builder, type );
const nodeVar = builder.getVarFromNode( this, type );

if ( name !== null ) {

nodeVar.name = name;

}
const nodeVar = builder.getVarFromNode( this, name, builder.getVectorType( this.getNodeType( builder ) ) );

const propertyName = builder.getPropertyName( nodeVar );

const snippet = node.build( builder, nodeVar.type );

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

return propertyName;
Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/lighting/HemisphereLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class HemisphereLightNode extends AnalyticLightNode {

const irradiance = mix( groundColorNode, colorNode, hemiDiffuseWeight );

builder.context.irradiance.addAssign( irradiance );
builder.stack.addAssign( builder.context.irradiance, irradiance );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunag I think you missed this in your PR?


}

Expand Down
2 changes: 1 addition & 1 deletion examples/jsm/nodes/shadernode/ShaderNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const shaderNodeHandler = {

const nodeElement = NodeElements.get( prop );

return ( ...params ) => nodeElement( nodeObj, ...params );
return node.isStackNode ? ( ...params ) => nodeObj.add( nodeElement( ...params ) ) : ( ...params ) => nodeElement( nodeObj, ...params );

} else if ( prop === 'self' ) {

Expand Down