浏览代码

utils.js: Introduce getTypedArray().

Mugen87 4 年之前
父节点
当前提交
cd0498f63d
共有 3 个文件被更改,包括 27 次插入32 次删除
  1. 5 17
      src/loaders/BufferGeometryLoader.js
  2. 2 14
      src/loaders/ObjectLoader.js
  3. 20 1
      src/utils.js

+ 5 - 17
src/loaders/BufferGeometryLoader.js

@@ -8,6 +8,7 @@ import { InstancedBufferGeometry } from '../core/InstancedBufferGeometry.js';
 import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
 import { InstancedBufferAttribute } from '../core/InstancedBufferAttribute.js';
 import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';
 import { InterleavedBufferAttribute } from '../core/InterleavedBufferAttribute.js';
 import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
 import { InterleavedBuffer } from '../core/InterleavedBuffer.js';
+import { getTypedArray } from '../utils.js';
 
 
 function BufferGeometryLoader( manager ) {
 function BufferGeometryLoader( manager ) {
 
 
@@ -67,7 +68,7 @@ BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype
 
 
 			const buffer = getArrayBuffer( json, interleavedBuffer.buffer );
 			const buffer = getArrayBuffer( json, interleavedBuffer.buffer );
 
 
-			const array = new TYPED_ARRAYS[ interleavedBuffer.type ]( buffer );
+			const array = getTypedArray( interleavedBuffer.type, buffer );
 			const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
 			const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
 			ib.uuid = interleavedBuffer.uuid;
 			ib.uuid = interleavedBuffer.uuid;
 
 
@@ -98,7 +99,7 @@ BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype
 
 
 		if ( index !== undefined ) {
 		if ( index !== undefined ) {
 
 
-			const typedArray = new TYPED_ARRAYS[ index.type ]( index.array );
+			const typedArray = getTypedArray( index.type, index.array );
 			geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
 			geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
 
 
 		}
 		}
@@ -117,7 +118,7 @@ BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype
 
 
 			} else {
 			} else {
 
 
-				const typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
+				const typedArray = getTypedArray( attribute.type, attribute.array );
 				const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
 				const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
 				bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
 				bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
 
 
@@ -150,7 +151,7 @@ BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype
 
 
 					} else {
 					} else {
 
 
-						const typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
+						const typedArray = getTypedArray( attribute.type, attribute.array );
 						bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
 						bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
 
 
 					}
 					}
@@ -213,17 +214,4 @@ BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype
 
 
 } );
 } );
 
 
-const TYPED_ARRAYS = {
-	Int8Array: Int8Array,
-	Uint8Array: Uint8Array,
-	// Workaround for IE11 pre KB2929437. See #11440
-	Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
-	Int16Array: Int16Array,
-	Uint16Array: Uint16Array,
-	Int32Array: Int32Array,
-	Uint32Array: Uint32Array,
-	Float32Array: Float32Array,
-	Float64Array: Float64Array
-};
-
 export { BufferGeometryLoader };
 export { BufferGeometryLoader };

+ 2 - 14
src/loaders/ObjectLoader.js

@@ -59,6 +59,7 @@ import { Loader } from './Loader.js';
 import { FileLoader } from './FileLoader.js';
 import { FileLoader } from './FileLoader.js';
 import * as Geometries from '../geometries/Geometries.js';
 import * as Geometries from '../geometries/Geometries.js';
 import * as Curves from '../extras/curves/Curves.js';
 import * as Curves from '../extras/curves/Curves.js';
+import { getTypedArray } from '../utils.js';
 
 
 class ObjectLoader extends Loader {
 class ObjectLoader extends Loader {
 
 
@@ -595,7 +596,7 @@ class ObjectLoader extends Loader {
 				if ( image.data ) {
 				if ( image.data ) {
 
 
 					return {
 					return {
-						data: new TYPED_ARRAYS[ image.type ]( image.data ),
+						data: getTypedArray( image.type, image.data ),
 						width: image.width,
 						width: image.width,
 						height: image.height
 						height: image.height
 					};
 					};
@@ -1148,17 +1149,4 @@ const TEXTURE_FILTER = {
 	LinearMipmapLinearFilter: LinearMipmapLinearFilter
 	LinearMipmapLinearFilter: LinearMipmapLinearFilter
 };
 };
 
 
-const TYPED_ARRAYS = {
-	Int8Array: Int8Array,
-	Uint8Array: Uint8Array,
-	// Workaround for IE11 pre KB2929437. See #11440
-	Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
-	Int16Array: Int16Array,
-	Uint16Array: Uint16Array,
-	Int32Array: Int32Array,
-	Uint32Array: Uint32Array,
-	Float32Array: Float32Array,
-	Float64Array: Float64Array
-};
-
 export { ObjectLoader };
 export { ObjectLoader };

+ 20 - 1
src/utils.js

@@ -30,4 +30,23 @@ function arrayMax( array ) {
 
 
 }
 }
 
 
-export { arrayMin, arrayMax };
+const TYPED_ARRAYS = {
+	Int8Array: Int8Array,
+	Uint8Array: Uint8Array,
+	// Workaround for IE11 pre KB2929437. See #11440
+	Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array,
+	Int16Array: Int16Array,
+	Uint16Array: Uint16Array,
+	Int32Array: Int32Array,
+	Uint32Array: Uint32Array,
+	Float32Array: Float32Array,
+	Float64Array: Float64Array
+};
+
+function getTypedArray( type, buffer ) {
+
+	return new TYPED_ARRAYS[ type ]( buffer );
+
+}
+
+export { arrayMin, arrayMax, getTypedArray };