Browse Source

[glTF] Clean up buffers/bufferViews.

Don McCurdy 8 years ago
parent
commit
637fb106a5
1 changed files with 34 additions and 84 deletions
  1. 34 84
      examples/js/loaders/GLTF2Loader.js

+ 34 - 84
examples/js/loaders/GLTF2Loader.js

@@ -196,8 +196,7 @@ THREE.GLTF2Loader = ( function () {
 		KHR_BINARY_GLTF: 'KHR_binary_glTF',
 		KHR_BINARY_GLTF: 'KHR_binary_glTF',
 		KHR_LIGHTS: 'KHR_lights',
 		KHR_LIGHTS: 'KHR_lights',
 		KHR_MATERIALS_COMMON: 'KHR_materials_common',
 		KHR_MATERIALS_COMMON: 'KHR_materials_common',
-		KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness',
-		KHR_TECHNIQUE_WEBGL: 'KHR_technique_webgl',
+		KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness'
 	};
 	};
 
 
 	/**
 	/**
@@ -746,7 +745,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 					defines.USE_GLOSSINESSMAP = '';
 					defines.USE_GLOSSINESSMAP = '';
 					// set USE_ROUGHNESSMAP to enable vUv
 					// set USE_ROUGHNESSMAP to enable vUv
-					defines.USE_ROUGHNESSMAP = ''
+					defines.USE_ROUGHNESSMAP = '';
 
 
 				}
 				}
 
 
@@ -1319,40 +1318,35 @@ THREE.GLTF2Loader = ( function () {
 
 
 	};
 	};
 
 
-	GLTFParser.prototype.loadBuffers = function () {
-
-		var json = this.json;
-		var extensions = this.extensions;
-		var options = this.options;
-
-		return _each( json.buffers, function ( buffer, name ) {
-
-			if ( buffer.type === 'arraybuffer' || buffer.type === undefined ) {
-
-				// If present, GLB container is required to be the first buffer.
-				if ( buffer.uri === undefined && name === 0 ) {
+	/**
+	 * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
+	 * @param {number} bufferIndex
+	 * @return {Promise<ArrayBuffer>}
+	 */
+	GLTFParser.prototype.loadBuffer = function ( bufferIndex ) {
 
 
-					return extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body;
+		var bufferDef = this.json.buffers[ bufferIndex ];
 
 
-				}
+		if ( bufferDef.type && bufferDef.type !== 'arraybuffer' ) {
 
 
-				return new Promise( function ( resolve ) {
+			throw new Error( 'THREE.GLTF2Loader: %s buffer type is not supported.', bufferDef.type );
 
 
-					var loader = new THREE.FileLoader();
-					loader.setResponseType( 'arraybuffer' );
-					loader.load( resolveURL( buffer.uri, options.path ), function ( buffer ) {
+		}
 
 
-						resolve( buffer );
+		// If present, GLB container is required to be the first buffer.
+		if ( bufferDef.uri === undefined && bufferIndex === 0 ) {
 
 
-					} );
+			return Promise.resolve( this.extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body );
 
 
-				} );
+		}
 
 
-			} else {
+		var options = this.options;
 
 
-				console.warn( 'THREE.GLTF2Loader: %s buffer type is not supported.', buffer.type );
+		return new Promise( function ( resolve ) {
 
 
-			}
+			var loader = new THREE.FileLoader();
+			loader.setResponseType( 'arraybuffer' );
+			loader.load( resolveURL( bufferDef.uri, options.path ), resolve);
 
 
 		} );
 		} );
 
 
@@ -1365,39 +1359,13 @@ THREE.GLTF2Loader = ( function () {
 	 */
 	 */
 	GLTFParser.prototype.loadBufferView = function ( bufferViewIndex ) {
 	GLTFParser.prototype.loadBufferView = function ( bufferViewIndex ) {
 
 
-		return this._withDependencies( [
+		var bufferViewDef = this.json.bufferViews[ bufferViewIndex ];
 
 
-			'bufferViews'
+		return this.getDependency( 'buffer', bufferViewDef.buffer ).then( function ( buffer ) {
 
 
-		] ).then( function ( dependencies ) {
-
-			return dependencies.bufferViews[ bufferViewIndex ];
-
-		} );
-
-	};
-
-	/** @deprecated */
-	GLTFParser.prototype.loadBufferViews = function () {
-
-		var json = this.json;
-
-		return this._withDependencies( [
-
-			'buffers'
-
-		] ).then( function ( dependencies ) {
-
-			return _each( json.bufferViews, function ( bufferView ) {
-
-				var arraybuffer = dependencies.buffers[ bufferView.buffer ];
-
-				var byteLength = bufferView.byteLength || 0;
-				var byteOffset = bufferView.byteOffset || 0;
-
-				return arraybuffer.slice( byteOffset, byteOffset + byteLength );
-
-			} );
+			var byteLength = bufferViewDef.byteLength || 0;
+			var byteOffset = bufferViewDef.byteOffset || 0;
+			return buffer.slice( byteOffset, byteOffset + byteLength );
 
 
 		} );
 		} );
 
 
@@ -1405,17 +1373,13 @@ THREE.GLTF2Loader = ( function () {
 
 
 	GLTFParser.prototype.loadAccessors = function () {
 	GLTFParser.prototype.loadAccessors = function () {
 
 
+		var parser = this;
 		var json = this.json;
 		var json = this.json;
 
 
-		return this._withDependencies( [
-
-			'bufferViews'
+		return _each( json.accessors, function ( accessor ) {
 
 
-		] ).then( function ( dependencies ) {
+			return parser.getDependency( 'bufferView', accessor.bufferView ).then( function ( bufferView ) {
 
 
-			return _each( json.accessors, function ( accessor ) {
-
-				var arraybuffer = dependencies.bufferViews[ accessor.bufferView ];
 				var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
 				var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
 				var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
 				var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
 
 
@@ -1429,7 +1393,7 @@ THREE.GLTF2Loader = ( function () {
 				if ( byteStride && byteStride !== itemBytes ) {
 				if ( byteStride && byteStride !== itemBytes ) {
 
 
 					// Use the full buffer if it's interleaved.
 					// Use the full buffer if it's interleaved.
-					array = new TypedArray( arraybuffer );
+					array = new TypedArray( bufferView );
 
 
 					// Integer parameters to IB/IBA are in array elements, not bytes.
 					// Integer parameters to IB/IBA are in array elements, not bytes.
 					var ib = new THREE.InterleavedBuffer( array, byteStride / elementBytes );
 					var ib = new THREE.InterleavedBuffer( array, byteStride / elementBytes );
@@ -1438,7 +1402,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 				} else {
 				} else {
 
 
-					array = new TypedArray( arraybuffer, accessor.byteOffset, accessor.count * itemSize );
+					array = new TypedArray( bufferView, accessor.byteOffset, accessor.count * itemSize );
 
 
 					return new THREE.BufferAttribute( array, itemSize );
 					return new THREE.BufferAttribute( array, itemSize );
 
 
@@ -1450,8 +1414,6 @@ THREE.GLTF2Loader = ( function () {
 
 
 	};
 	};
 
 
-	var URL = window.URL || window.webkitURL;
-
 	/**
 	/**
 	 * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures
 	 * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures
 	 * @param {number} textureIndex
 	 * @param {number} textureIndex
@@ -1463,14 +1425,9 @@ THREE.GLTF2Loader = ( function () {
 		var json = this.json;
 		var json = this.json;
 		var options = this.options;
 		var options = this.options;
 
 
-		var textureDef = this.json.textures[ textureIndex ];
-
-		if ( textureDef.source === undefined ) {
-
-			throw new Error( 'THREE.GLTF2Loader: Unknown texture for index ' + textureIndex );
-
-		}
+		var URL = window.URL || window.webkitURL;
 
 
+		var textureDef = json.textures[ textureIndex ];
 		var source = json.images[ textureDef.source ];
 		var source = json.images[ textureDef.source ];
 		var sourceURI = source.uri;
 		var sourceURI = source.uri;
 		var isObjectURL = false;
 		var isObjectURL = false;
@@ -1479,7 +1436,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 			// Load binary image data from bufferView, if provided.
 			// Load binary image data from bufferView, if provided.
 
 
-			sourceURI = parser.loadBufferView( source.bufferView )
+			sourceURI = parser.getDependency( 'bufferView', source.bufferView )
 				.then( function ( bufferView ) {
 				.then( function ( bufferView ) {
 
 
 					isObjectURL = true;
 					isObjectURL = true;
@@ -1590,11 +1547,6 @@ THREE.GLTF2Loader = ( function () {
 				materialType = sgExtension.getMaterialType( material );
 				materialType = sgExtension.getMaterialType( material );
 				pending.push( sgExtension.extendParams( materialParams, material, parser ) );
 				pending.push( sgExtension.extendParams( materialParams, material, parser ) );
 
 
-			} else if ( materialExtensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ] ) {
-
-				materialType = extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].getMaterialType( material );
-				extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].extendParams( materialParams, material, dependencies );
-
 			} else if ( material.pbrMetallicRoughness !== undefined ) {
 			} else if ( material.pbrMetallicRoughness !== undefined ) {
 
 
 				// Specification:
 				// Specification:
@@ -1732,7 +1684,6 @@ THREE.GLTF2Loader = ( function () {
 		return this._withDependencies( [
 		return this._withDependencies( [
 
 
 			'accessors',
 			'accessors',
-			'bufferViews',
 
 
 		] ).then( function ( dependencies ) {
 		] ).then( function ( dependencies ) {
 
 
@@ -1821,7 +1772,6 @@ THREE.GLTF2Loader = ( function () {
 
 
 		return this._withDependencies( [
 		return this._withDependencies( [
 
 
-			'accessors',
 			'materials'
 			'materials'
 
 
 		] ).then( function ( dependencies ) {
 		] ).then( function ( dependencies ) {