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
3 changes: 1 addition & 2 deletions src/nodes/core/AssignNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ class AssignNode extends TempNode {

const needsSplitAssign = this.needsSplitAssign( builder );

const target = targetNode.build( builder );
const targetType = targetNode.getNodeType( builder );

const target = targetNode.build( builder );
const source = sourceNode.build( builder, targetType );

const sourceType = sourceNode.getNodeType( builder );

const nodeData = builder.getDataFromNode( this );
Expand Down
10 changes: 10 additions & 0 deletions src/nodes/core/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,16 @@ class Node extends EventDispatcher {

}

if ( result === '' && output !== null && output !== 'void' && output !== 'OutputType' ) {

// if no snippet is generated, return a default value

console.error( `THREE.TSL: Invalid generated code, expected a "${ output }".` );

result = builder.generateConst( output );

}

}

builder.removeChain( this );
Expand Down
6 changes: 0 additions & 6 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,6 @@ class NodeBuilder {

}


/**
* Generates the shader string for the given type and value.
*
Expand Down Expand Up @@ -2923,11 +2922,6 @@ class NodeBuilder {

}

/**
* Prevents the node builder from being used as an iterable in TSL.Fn(), avoiding potential runtime errors.
*/
*[ Symbol.iterator ]() { }

}

export default NodeBuilder;
33 changes: 29 additions & 4 deletions src/nodes/core/StackNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class StackNode extends Node {

getNodeType( builder ) {

return this.outputNode ? this.outputNode.getNodeType( builder ) : 'void';
return this.hasOutput ? this.outputNode.getNodeType( builder ) : 'void';

}

getMemberType( builder, name ) {

return this.outputNode ? this.outputNode.getMemberType( builder, name ) : 'void';
return this.hasOutput ? this.outputNode.getMemberType( builder, name ) : 'void';

}

Expand All @@ -98,6 +98,13 @@ class StackNode extends Node {
*/
add( node ) {

if ( node.isNode !== true ) {

console.error( 'THREE.TSL: Invalid node added to stack.' );
return this;

}

this.nodes.push( node );

return this;
Expand Down Expand Up @@ -191,7 +198,7 @@ class StackNode extends Node {

} else {

throw new Error( 'TSL: Invalid parameter length. Case() requires at least two parameters.' );
console.error( 'THREE.TSL: Invalid parameter length. Case() requires at least two parameters.' );

}

Expand Down Expand Up @@ -275,6 +282,12 @@ class StackNode extends Node {

}

get hasOutput() {

return this.outputNode && this.outputNode.isNode;

}

build( builder, ...params ) {

const previousBuildStack = builder.currentStack;
Expand Down Expand Up @@ -325,7 +338,19 @@ class StackNode extends Node {

}

const result = this.outputNode ? this.outputNode.build( builder, ...params ) : super.build( builder, ...params );
//

let result;

if ( this.hasOutput ) {

result = this.outputNode.build( builder, ...params );

} else {

result = super.build( builder, ...params );

}

setCurrentStack( previousStack );

Expand Down
19 changes: 14 additions & 5 deletions src/nodes/core/UniformNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import InputNode from './InputNode.js';
import { objectGroup } from './UniformGroupNode.js';
import { nodeObject, getConstNodeType } from '../tsl/TSLCore.js';
import { getValueFromType } from './NodeUtils.js';

/**
* Class for representing a uniform.
Expand Down Expand Up @@ -219,16 +220,24 @@ export default UniformNode;
*
* @tsl
* @function
* @param {any} arg1 - The value of this node. Usually a JS primitive or three.js object (vector, matrix, color, texture).
* @param {string} [arg2] - The node type. If no explicit type is defined, the node tries to derive the type from its value.
* @param {any|string} value - The value of this uniform or your type. Usually a JS primitive or three.js object (vector, matrix, color, texture).
* @param {string} [type] - The node type. If no explicit type is defined, the node tries to derive the type from its value.
* @returns {UniformNode}
*/
export const uniform = ( arg1, arg2 ) => {
export const uniform = ( value, type ) => {

const nodeType = getConstNodeType( arg2 || arg1 );
const nodeType = getConstNodeType( type || value );

if ( nodeType === value ) {

// if the value is a type but no having a value

value = getValueFromType( nodeType );

}

// @TODO: get ConstNode from .traverse() in the future
const value = ( arg1 && arg1.isNode === true ) ? ( arg1.node && arg1.node.value ) || arg1.value : arg1;
value = ( value && value.isNode === true ) ? ( value.node && value.node.value ) || value.value : value;

return nodeObject( new UniformNode( value, nodeType ) );

Expand Down
52 changes: 48 additions & 4 deletions src/nodes/tsl/TSLCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,25 +407,57 @@ class ShaderCallNodeInternal extends Node {
let index = 0;

inputs = new Proxy( inputs, {

get: ( target, property, receiver ) => {

let value;

if ( target[ property ] === undefined ) {

return target[ index ++ ];
value = target[ index ++ ];

} else {

return Reflect.get( target, property, receiver );
value = Reflect.get( target, property, receiver );

}

return value;

}

} );

}

const secureNodeBuilder = new Proxy( builder, {

get: ( target, property, receiver ) => {

let value;

if ( Symbol.iterator === property ) {

value = function* () {

yield undefined;

};

} else {

value = Reflect.get( target, property, receiver );

}

return value;

}

} );

const jsFunc = shaderNode.jsFunc;
const outputNode = inputs !== null || jsFunc.length > 1 ? jsFunc( inputs || [], builder ) : jsFunc( builder );
const outputNode = inputs !== null || jsFunc.length > 1 ? jsFunc( inputs || [], secureNodeBuilder ) : jsFunc( secureNodeBuilder );

result = nodeObject( outputNode );

Expand Down Expand Up @@ -611,6 +643,18 @@ const ConvertType = function ( type, cacheMap = null ) {

return ( ...params ) => {

for ( const param of params ) {

if ( param === undefined ) {

console.error( `THREE.TSL: Invalid parameter for the type "${ type }".` );

return nodeObject( new ConstNode( 0, type ) );

}

}

if ( params.length === 0 || ( ! [ 'bool', 'float', 'int', 'uint' ].includes( type ) && params.every( param => {

const paramType = typeof param;
Expand Down Expand Up @@ -792,7 +836,7 @@ class FnNode extends Node {

const type = this.getNodeType( builder );

console.warn( 'THREE.TSL: "Fn()" was declared but not invoked. Try calling it like "Fn()( ...params )".' );
console.error( 'THREE.TSL: "Fn()" was declared but not invoked. Try calling it like "Fn()( ...params )".' );

return builder.generateConst( type );

Expand Down
Loading