Browse Source

TSL: Avoid name collision (#30785)

* auto-rename

* Update NodeBuilder.js

* Update NodeBuilder.js
sunag 11 months ago
parent
commit
558068d0d3
2 changed files with 51 additions and 6 deletions
  1. 51 0
      src/nodes/core/NodeBuilder.js
  2. 0 6
      src/nodes/core/VarNode.js

+ 51 - 0
src/nodes/core/NodeBuilder.js

@@ -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.
 		 *
@@ -1226,6 +1234,8 @@ class NodeBuilder {
 
 		const attribute = new NodeAttribute( name, type );
 
+		this.registerDeclaration( attribute );
+
 		attributes.push( attribute );
 
 		return attribute;
@@ -1676,6 +1686,8 @@ class NodeBuilder {
 
 			this.uniforms[ shaderStage ].push( nodeUniform );
 
+			this.registerDeclaration( nodeUniform );
+
 			nodeData.uniform = nodeUniform;
 
 		}
@@ -1745,6 +1757,8 @@ class NodeBuilder {
 
 			}
 
+			this.registerDeclaration( nodeVar );
+
 			nodeData.variable = nodeVar;
 
 		}
@@ -1825,6 +1839,8 @@ class NodeBuilder {
 
 			varyings.push( nodeVarying );
 
+			this.registerDeclaration( nodeVarying );
+
 			nodeData.varying = nodeVarying;
 
 		}
@@ -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.
 	 *

+ 0 - 6
src/nodes/core/VarNode.js

@@ -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 );

粤ICP备19079148号