瀏覽代碼

glTF, loading interleaved buffers (#10107)

* Check for interleaved buffers when loading accessors

* Add missing onUpload feature (from BufferAttribute)
Mr.doob 8 年之前
父節點
當前提交
361c1585fb
共有 2 個文件被更改,包括 31 次插入2 次删除
  1. 21 2
      examples/js/loaders/GLTFLoader.js
  2. 10 0
      src/core/InterleavedBuffer.js

+ 21 - 2
examples/js/loaders/GLTFLoader.js

@@ -890,9 +890,28 @@ GLTFParser.prototype.loadAccessors = function() {
 			var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
 			var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
 
-			var array = new TypedArray( arraybuffer, accessor.byteOffset, accessor.count * itemSize );
+			// For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12.
+			var elementBytes = TypedArray.BYTES_PER_ELEMENT;
+			var itemBytes = elementBytes * itemSize;
 
-			return new THREE.BufferAttribute( array, itemSize );
+			// The buffer is not interleaved if the stride is the item size in bytes.
+			if ( accessor.byteStride && accessor.byteStride !== itemBytes ) {
+
+				// Use the full buffer if it's interleaved.
+				var array = new TypedArray( arraybuffer );
+
+				// Integer parameters to IB/IBA are in array elements, not bytes.
+				var ib = new THREE.InterleavedBuffer( array, accessor.byteStride / elementBytes );
+
+				return new THREE.InterleavedBufferAttribute( ib, itemSize, accessor.byteOffset / elementBytes );
+
+			} else {
+
+				array = new TypedArray( arraybuffer, accessor.byteOffset, accessor.count * itemSize );
+
+				return new THREE.BufferAttribute( array, itemSize );
+
+			}
 
 		});
 

+ 10 - 0
src/core/InterleavedBuffer.js

@@ -15,6 +15,8 @@ function InterleavedBuffer( array, stride ) {
 	this.dynamic = false;
 	this.updateRange = { offset: 0, count: - 1 };
 
+	this.onUploadCallback = function () {};
+
 	this.version = 0;
 
 }
@@ -92,6 +94,14 @@ InterleavedBuffer.prototype = {
 
 		return new this.constructor().copy( this );
 
+	},
+
+	onUpload: function ( callback ) {
+
+		this.onUploadCallback = callback;
+
+		return this;
+
 	}
 
 };