Răsfoiți Sursa

InterleavedBufferAttribute: Implement .clone().

Don McCurdy 7 ani în urmă
părinte
comite
1733ddb9f3
2 a modificat fișierele cu 24 adăugiri și 29 ștergeri
  1. 4 29
      examples/js/loaders/GLTFLoader.js
  2. 20 0
      src/core/InterleavedBufferAttribute.js

+ 4 - 29
examples/js/loaders/GLTFLoader.js

@@ -1100,7 +1100,7 @@ THREE.GLTFLoader = ( function () {
 				// So morphTarget value will depend on mesh's position, then cloning attribute
 				// for the case if attribute is shared among two or more meshes.
 
-				positionAttribute = cloneBufferAttribute( accessors[ target.POSITION ] );
+				positionAttribute = accessors[ target.POSITION ].clone();
 				var position = geometry.attributes.position;
 
 				for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) {
@@ -1118,7 +1118,7 @@ THREE.GLTFLoader = ( function () {
 
 				// Copying the original position not to affect the final position.
 				// See the formula above.
-				positionAttribute = cloneBufferAttribute( geometry.attributes.position );
+				positionAttribute = geometry.attributes.position.clone();
 
 			}
 
@@ -1135,7 +1135,7 @@ THREE.GLTFLoader = ( function () {
 
 				// see target.POSITION's comment
 
-				normalAttribute = cloneBufferAttribute( accessors[ target.NORMAL ] );
+				normalAttribute = accessors[ target.NORMAL ].clone();
 				var normal = geometry.attributes.normal;
 
 				for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) {
@@ -1151,7 +1151,7 @@ THREE.GLTFLoader = ( function () {
 
 			} else if ( geometry.attributes.normal !== undefined ) {
 
-				normalAttribute = cloneBufferAttribute( geometry.attributes.normal );
+				normalAttribute = geometry.attributes.normal.clone();
 
 			}
 
@@ -1231,31 +1231,6 @@ THREE.GLTFLoader = ( function () {
 
 	}
 
-	function cloneBufferAttribute( attribute ) {
-
-		if ( attribute.isInterleavedBufferAttribute ) {
-
-			var count = attribute.count;
-			var itemSize = attribute.itemSize;
-			var array = attribute.array.slice( 0, count * itemSize );
-
-			for ( var i = 0; i < count; ++ i ) {
-
-				array[ i ] = attribute.getX( i );
-				if ( itemSize >= 2 ) array[ i + 1 ] = attribute.getY( i );
-				if ( itemSize >= 3 ) array[ i + 2 ] = attribute.getZ( i );
-				if ( itemSize >= 4 ) array[ i + 3 ] = attribute.getW( i );
-
-			}
-
-			return new THREE.BufferAttribute( array, itemSize, attribute.normalized );
-
-		}
-
-		return attribute.clone();
-
-	}
-
 	/* GLTF PARSER */
 
 	function GLTFParser( json, extensions, options ) {

+ 20 - 0
src/core/InterleavedBufferAttribute.js

@@ -1,3 +1,4 @@
+import { BufferAttribute } from './BufferAttribute.js';
 import { _Math } from '../math/Math.js';
 
 /**
@@ -134,6 +135,25 @@ Object.assign( InterleavedBufferAttribute.prototype, {
 
 		return this;
 
+	},
+
+	clone: function () {
+
+		var count = this.count;
+		var itemSize = this.itemSize;
+		var array = this.data.array.slice( 0, count * itemSize );
+
+		for ( var i = 0; i < count; ++ i ) {
+
+			array[ i ] = this.getX( i );
+			if ( itemSize >= 2 ) array[ i + 1 ] = this.getY( i );
+			if ( itemSize >= 3 ) array[ i + 2 ] = this.getZ( i );
+			if ( itemSize >= 4 ) array[ i + 3 ] = this.getW( i );
+
+		}
+
+		return new BufferAttribute( array, itemSize, this.normalized );
+
 	}
 
 } );