Bläddra i källkod

SplitNode: Fix unecessary swizzle (#24170)

* SplitNode: Fix unecessary swizzle

* replace .substr() to .slice()
sunag 3 år sedan
förälder
incheckning
7713736d5f
1 ändrade filer med 21 tillägg och 5 borttagningar
  1. 21 5
      examples/jsm/nodes/utils/SplitNode.js

+ 21 - 5
examples/jsm/nodes/utils/SplitNode.js

@@ -1,6 +1,8 @@
 import Node from '../core/Node.js';
 import { vector } from '../core/NodeBuilder.js';
 
+const vectorComponents = 'xyzw';
+
 class SplitNode extends Node {
 
 	constructor( node, components = 'x' ) {
@@ -32,11 +34,13 @@ class SplitNode extends Node {
 
 	}
 
-	generate( builder ) {
+	generate( builder, output ) {
 
 		const node = this.node;
 		const nodeTypeLength = builder.getTypeLength( node.getNodeType( builder ) );
 
+		let snippet = null;
+
 		if ( nodeTypeLength > 1 ) {
 
 			let type = null;
@@ -45,7 +49,7 @@ class SplitNode extends Node {
 
 			if ( componentsLength >= nodeTypeLength ) {
 
-				// need expand the input node
+				// needed expand the input node
 
 				type = builder.getTypeFromLength( this.getVectorLength() );
 
@@ -53,16 +57,28 @@ class SplitNode extends Node {
 
 			const nodeSnippet = node.build( builder, type );
 
-			return `${nodeSnippet}.${this.components}`;
+			if ( this.components.length === nodeTypeLength && this.components === vectorComponents.slice( 0, this.components.length ) ) {
+
+				// unecessary swizzle
+
+				snippet = builder.format( nodeSnippet, type, output );
+
+			} else {
+
+				snippet = builder.format( `${nodeSnippet}.${this.components}`, this.getNodeType( builder ), output );
+
+			}
 
 		} else {
 
-			// ignore components if node is a float
+			// ignore .components if .node returns float/integer
 
-			return node.build( builder );
+			snippet = node.build( builder, output );
 
 		}
 
+		return snippet;
+
 	}
 
 	serialize( data ) {