Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,13 +435,13 @@ class NodeBuilder {
*/
this.subBuildLayers = [];


/**
* The current stack of nodes.
* The active stack nodes.
*
* @type {?StackNode}
* @default null
* @type {Array<StackNode>}
*/
this.currentStack = null;
this.activeStacks = [];

/**
* The current sub-build TSL function(Fn).
Expand Down Expand Up @@ -1605,6 +1605,58 @@ class NodeBuilder {

}

/**
* Adds an active stack to the internal stack.
*
* @param {StackNode} stack - The stack node to add.
*/
setActiveStack( stack ) {

this.activeStacks.push( stack );

}

/**
* Removes the active stack from the internal stack.
*
* @param {StackNode} stack - The stack node to remove.
*/
removeActiveStack( stack ) {

if ( this.activeStacks[ this.activeStacks.length - 1 ] === stack ) {

this.activeStacks.pop();

} else {

throw new Error( 'NodeBuilder: Invalid active stack removal.' );

}

}

/**
* Returns the active stack.
*
* @return {StackNode} The active stack.
*/
getActiveStack() {

return this.activeStacks[ this.activeStacks.length - 1 ];

}

/**
* Returns the base stack.
*
* @return {StackNode} The base stack.
*/
getBaseStack() {

return this.activeStacks[ 0 ];

}

/**
* Adds a stack node to the internal stack.
*
Expand Down
8 changes: 5 additions & 3 deletions src/nodes/core/StackNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,11 @@ class StackNode extends Node {

build( builder, ...params ) {

const previousBuildStack = builder.currentStack;
const previousStack = getCurrentStack();

setCurrentStack( this );

builder.currentStack = this;
builder.setActiveStack( this );

const buildStage = builder.buildStage;

Expand All @@ -312,6 +311,9 @@ class StackNode extends Node {

if ( buildStage === 'setup' ) {

const nodeData = builder.getDataFromNode( node );
nodeData.stack = this;

node.build( builder );

} else if ( buildStage === 'analyze' ) {
Expand Down Expand Up @@ -351,7 +353,7 @@ class StackNode extends Node {

setCurrentStack( previousStack );

builder.currentStack = previousBuildStack;
builder.removeActiveStack( this );

return result;

Expand Down
30 changes: 21 additions & 9 deletions src/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Node from './Node.js';
import { addMethodChaining, getCurrentStack, nodeProxy } from '../tsl/TSLCore.js';
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';
import { error } from '../../utils.js';

/**
Expand Down Expand Up @@ -179,9 +179,19 @@ class VarNode extends Node {

build( ...params ) {

if ( this.intent === true ) {
const builder = params[ 0 ];

if ( this._hasStack( builder ) === false && builder.buildStage === 'setup' ) {

if ( builder.context.nodeLoop || builder.context.nodeBlock ) {

const builder = params[ 0 ];
builder.getBaseStack().addToStack( this );

}

}

if ( this.intent === true ) {

if ( this.isAssign( builder ) !== true ) {

Expand Down Expand Up @@ -262,6 +272,14 @@ class VarNode extends Node {

}

_hasStack( builder ) {

const nodeData = builder.getDataFromNode( this );

return nodeData.stack !== undefined;

}

}

export default VarNode;
Expand Down Expand Up @@ -313,12 +331,6 @@ export const Const = ( node, name = null ) => createVar( node, name, true ).toSt
*/
export const VarIntent = ( node ) => {

if ( getCurrentStack() === null ) {

return node;

}

return createVar( node ).setIntent( true ).toStack();

};
Expand Down
10 changes: 5 additions & 5 deletions src/nodes/functions/VolumetricLightingModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class VolumetricLightingModel extends LightingModel {

start( builder ) {

const { material, context } = builder;
const { material } = builder;

const startPos = property( 'vec3' );
const endPos = property( 'vec3' );
Expand Down Expand Up @@ -79,13 +79,13 @@ class VolumetricLightingModel extends LightingModel {

linearDepthRay.assign( linearDepth( viewZToPerspectiveDepth( positionViewRay.z, cameraNear, cameraFar ) ) );

context.sceneDepthNode = linearDepth( material.depthNode ).toVar();
builder.context.sceneDepthNode = linearDepth( material.depthNode ).toVar();

}

context.positionWorld = positionRay;
context.shadowPositionWorld = positionRay;
context.positionView = positionViewRay;
builder.context.positionWorld = positionRay;
builder.context.shadowPositionWorld = positionRay;
builder.context.positionView = positionViewRay;

scatteringDensity.assign( 0 );

Expand Down
44 changes: 40 additions & 4 deletions src/nodes/geometry/RangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,11 @@ class RangeNode extends Node {
*/
getVectorLength( builder ) {

const minLength = builder.getTypeLength( getValueType( this.minNode.value ) );
const maxLength = builder.getTypeLength( getValueType( this.maxNode.value ) );
const minNode = this.getConstNode( this.minNode );
const maxNode = this.getConstNode( this.maxNode );

const minLength = builder.getTypeLength( getValueType( minNode.value ) );
const maxLength = builder.getTypeLength( getValueType( maxNode.value ) );

return minLength > maxLength ? minLength : maxLength;

Expand All @@ -86,6 +89,36 @@ class RangeNode extends Node {

}

/**
* Returns a constant node from the given node by traversing it.
*
* @param {Node} node - The node to traverse.
* @returns {Node} The constant node, if found.
*/
getConstNode( node ) {

let output = null;

node.traverse( n => {

if ( n.isConstNode === true ) {

output = n;

}

} );

if ( output === null ) {

throw new Error( 'THREE.TSL: No "ConstNode" found in node graph.' );

}

return output;

}

setup( builder ) {

const object = builder.object;
Expand All @@ -94,8 +127,11 @@ class RangeNode extends Node {

if ( object.count > 1 ) {

const minValue = this.minNode.value;
const maxValue = this.maxNode.value;
const minNode = this.getConstNode( this.minNode );
const maxNode = this.getConstNode( this.maxNode );

const minValue = minNode.value;
const maxValue = maxNode.value;

const minLength = builder.getTypeLength( getValueType( minValue ) );
const maxLength = builder.getTypeLength( getValueType( maxValue ) );
Expand Down
30 changes: 9 additions & 21 deletions src/nodes/utils/LoopNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class LoopNode extends Node {
*/
constructor( params = [] ) {

super();
super( 'void' );

this.params = params;

Expand Down Expand Up @@ -100,16 +100,20 @@ class LoopNode extends Node {

}

const stack = builder.addStack(); // TODO: cache() it
const stack = builder.addStack();

properties.returnsNode = this.params[ this.params.length - 1 ]( inputs, builder );
const fnCall = this.params[ this.params.length - 1 ]( inputs );

properties.returnsNode = fnCall.context( { nodeLoop: fnCall } );
properties.stackNode = stack;

const baseParam = this.params[ 0 ];

if ( baseParam.isNode !== true && typeof baseParam.update === 'function' ) {

properties.updateNode = Fn( this.params[ 0 ].update )( inputs );
const fnUpdateCall = Fn( this.params[ 0 ].update )( inputs );

properties.updateNode = fnUpdateCall.context( { nodeLoop: fnUpdateCall } );

}

Expand All @@ -119,20 +123,6 @@ class LoopNode extends Node {

}

/**
* This method is overwritten since the node type is inferred based on the loop configuration.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The node type.
*/
getNodeType( builder ) {

const { returnsNode } = this.getProperties( builder );

return returnsNode ? returnsNode.getNodeType( builder ) : 'void';

}

setup( builder ) {

// setup properties
Expand Down Expand Up @@ -312,7 +302,7 @@ class LoopNode extends Node {

const stackSnippet = stackNode.build( builder, 'void' );

const returnsSnippet = properties.returnsNode ? properties.returnsNode.build( builder ) : '';
properties.returnsNode.build( builder, 'void' );

builder.removeFlowTab().addFlowCode( '\n' + builder.tab + stackSnippet );

Expand All @@ -324,8 +314,6 @@ class LoopNode extends Node {

builder.addFlowTab();

return returnsSnippet;

}

}
Expand Down