Skip to content

TSL: Avoid name collision #30785

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

Merged
merged 3 commits into from
Mar 24, 2025
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
51 changes: 51 additions & 0 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,14 @@ class NodeBuilder {
*/
this.bindings = { vertex: {}, fragment: {}, compute: {} };


/**
* This dictionary holds the declarations for each shader stage.
*
* @type {Object}
*/
this.declarations = { vertex: {}, fragment: {}, compute: {} };

/**
* This dictionary maintains the binding indices per bind group.
*
Expand Down Expand Up @@ -1226,6 +1234,8 @@ class NodeBuilder {

const attribute = new NodeAttribute( name, type );

this.registerDeclaration( attribute );

attributes.push( attribute );

return attribute;
Expand Down Expand Up @@ -1676,6 +1686,8 @@ class NodeBuilder {

this.uniforms[ shaderStage ].push( nodeUniform );

this.registerDeclaration( nodeUniform );

nodeData.uniform = nodeUniform;

}
Expand Down Expand Up @@ -1745,6 +1757,8 @@ class NodeBuilder {

}

this.registerDeclaration( nodeVar );

nodeData.variable = nodeVar;

}
Expand Down Expand Up @@ -1825,6 +1839,8 @@ class NodeBuilder {

varyings.push( nodeVarying );

this.registerDeclaration( nodeVarying );

nodeData.varying = nodeVarying;

}
Expand All @@ -1833,6 +1849,41 @@ class NodeBuilder {

}

/**
* Registers a node declaration in the current shader stage.
*
* @param {Object} node - The node to be registered.
*/
registerDeclaration( node ) {

const shaderStage = this.shaderStage;
const declarations = this.declarations[ shaderStage ];

const property = this.getPropertyName( node );

let index = 1;
let name = property;

// Automatically renames the property if the name is already in use.

while ( declarations[ name ] !== undefined ) {

name = property + '_' + index ++;

}

declarations[ name ] = node;

if ( index > 1 ) {

node.name = name;

console.warn( `THREE.TSL: Declaration name '${ property }' of '${ node.type }' already in use. Renamed to '${ name }'.` );

}

}

/**
* Returns an instance of {@link NodeCode} for the given code node.
*
Expand Down
6 changes: 0 additions & 6 deletions src/nodes/core/VarNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@ class VarNode extends Node {

}

getHash( builder ) {

return this.name || super.getHash( builder );

}

getMemberType( builder, name ) {

return this.node.getMemberType( builder, name );
Expand Down