2
0
Эх сурвалжийг харах

Merge pull request #11950 from donmccurdy/feat-gltf2-td-textures

GLTF2Loader: Top-down loading for textures, buffers, and bufferviews.
Mr.doob 8 жил өмнө
parent
commit
26416bd4f7

+ 205 - 181
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'
 	};
 	};
 
 
 	/**
 	/**
@@ -316,10 +315,12 @@ THREE.GLTF2Loader = ( function () {
 
 
 	};
 	};
 
 
-	GLTFMaterialsCommonExtension.prototype.extendParams = function ( materialParams, material, dependencies ) {
+	GLTFMaterialsCommonExtension.prototype.extendParams = function ( materialParams, material, parser ) {
 
 
 		var khrMaterial = material.extensions[ this.name ];
 		var khrMaterial = material.extensions[ this.name ];
 
 
+		var pending = [];
+
 		var keys = [];
 		var keys = [];
 
 
 		// TODO: Currently ignored: 'ambientFactor', 'ambientTexture'
 		// TODO: Currently ignored: 'ambientFactor', 'ambientTexture'
@@ -357,7 +358,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 		if ( materialValues.diffuseTexture !== undefined ) {
 		if ( materialValues.diffuseTexture !== undefined ) {
 
 
-			materialParams.map = dependencies.textures[ materialValues.diffuseTexture.index ];
+			pending.push( parser.assignTexture( materialParams, 'map', materialValues.diffuseTexture.index ) );
 
 
 		}
 		}
 
 
@@ -369,7 +370,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 		if ( materialValues.specularTexture !== undefined ) {
 		if ( materialValues.specularTexture !== undefined ) {
 
 
-			materialParams.specularMap = dependencies.textures[ materialValues.specularTexture.index ];
+			pending.push( parser.assignTexture( materialParams, 'specularMap', materialValues.specularTexture.index ) );
 
 
 		}
 		}
 
 
@@ -379,6 +380,8 @@ THREE.GLTF2Loader = ( function () {
 
 
 		}
 		}
 
 
+		return Promise.all( pending );
+
 	};
 	};
 
 
 	/* BINARY EXTENSION */
 	/* BINARY EXTENSION */
@@ -466,10 +469,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 			},
 			},
 
 
-			extendParams: function ( params, material, dependencies ) {
-
-				// specification
-				// https://github.com/sbtron/glTF/tree/KHRpbrSpecGloss/extensions/Khronos/KHR_materials_pbrSpecularGlossiness
+			extendParams: function ( params, material, parser ) {
 
 
 				var pbrSpecularGlossiness = material.extensions[ this.name ];
 				var pbrSpecularGlossiness = material.extensions[ this.name ];
 
 
@@ -542,6 +542,8 @@ THREE.GLTF2Loader = ( function () {
 				params.color = new THREE.Color( 1.0, 1.0, 1.0 );
 				params.color = new THREE.Color( 1.0, 1.0, 1.0 );
 				params.opacity = 1.0;
 				params.opacity = 1.0;
 
 
+				var pending = [];
+
 				if ( Array.isArray( pbrSpecularGlossiness.diffuseFactor ) ) {
 				if ( Array.isArray( pbrSpecularGlossiness.diffuseFactor ) ) {
 
 
 					var array = pbrSpecularGlossiness.diffuseFactor;
 					var array = pbrSpecularGlossiness.diffuseFactor;
@@ -553,7 +555,7 @@ THREE.GLTF2Loader = ( function () {
 
 
 				if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) {
 				if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) {
 
 
-					params.map = dependencies.textures[ pbrSpecularGlossiness.diffuseTexture.index ];
+					pending.push( parser.assignTexture( params, 'map', pbrSpecularGlossiness.diffuseTexture.index ) );
 
 
 				}
 				}
 
 
@@ -569,11 +571,14 @@ THREE.GLTF2Loader = ( function () {
 
 
 				if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) {
 				if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) {
 
 
-					params.glossinessMap = dependencies.textures[ pbrSpecularGlossiness.specularGlossinessTexture.index ];
-					params.specularMap = dependencies.textures[ pbrSpecularGlossiness.specularGlossinessTexture.index ];
+					var specGlossIndex = pbrSpecularGlossiness.specularGlossinessTexture.index;
+					pending.push( parser.assignTexture( params, 'glossinessMap', specGlossIndex ) );
+					pending.push( parser.assignTexture( params, 'specularMap', specGlossIndex ) );
 
 
 				}
 				}
 
 
+				return Promise.all( pending );
+
 			},
 			},
 
 
 			createMaterial: function ( params ) {
 			createMaterial: function ( params ) {
@@ -740,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 = '';
 
 
 				}
 				}
 
 
@@ -1290,65 +1295,77 @@ 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 ) {
+	/**
+	 * Requests the specified dependency asynchronously, with caching.
+	 * @param {string} type
+	 * @param {number} index
+	 * @return {Promise<Object>}
+	 */
+	GLTFParser.prototype.getDependency = function ( type, index ) {
 
 
-			if ( buffer.type === 'arraybuffer' || buffer.type === undefined ) {
+		var cacheKey = type + ':' + index;
+		var dependency = this.cache.get( cacheKey );
 
 
-				// If present, GLB container is required to be the first buffer.
-				if ( buffer.uri === undefined && name === 0 ) {
+		if ( !dependency ) {
 
 
-					return extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body;
+			var fnName = 'load' + type.charAt( 0 ).toUpperCase() + type.slice( 1 );
+			dependency = this[ fnName ]( index );
+			this.cache.add( cacheKey, dependency );
 
 
-				}
-
-				return new Promise( function ( resolve ) {
+		}
 
 
-					var loader = new THREE.FileLoader();
-					loader.setResponseType( 'arraybuffer' );
-					loader.load( resolveURL( buffer.uri, options.path ), function ( buffer ) {
+		return dependency;
 
 
-						resolve( buffer );
+	};
 
 
-					} );
+	/**
+	 * 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 ) {
 
 
-				} );
+		var bufferDef = this.json.buffers[ bufferIndex ];
 
 
-			} else {
+		if ( bufferDef.type && bufferDef.type !== 'arraybuffer' ) {
 
 
-				console.warn( 'THREE.GLTF2Loader: %s buffer type is not supported.', buffer.type );
+			throw new Error( 'THREE.GLTF2Loader: %s buffer type is not supported.', bufferDef.type );
 
 
-			}
+		}
 
 
-		} );
+		// 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 );
 
 
-	GLTFParser.prototype.loadBufferViews = function () {
+		}
 
 
-		var json = this.json;
+		var options = this.options;
 
 
-		return this._withDependencies( [
+		return new Promise( function ( resolve ) {
 
 
-			'buffers'
+			var loader = new THREE.FileLoader();
+			loader.setResponseType( 'arraybuffer' );
+			loader.load( resolveURL( bufferDef.uri, options.path ), resolve);
 
 
-		] ).then( function ( dependencies ) {
+		} );
 
 
-			return _each( json.bufferViews, function ( bufferView ) {
+	};
 
 
-				var arraybuffer = dependencies.buffers[ bufferView.buffer ];
+	/**
+	 * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views
+	 * @param {number} bufferViewIndex
+	 * @return {Promise<ArrayBuffer>}
+	 */
+	GLTFParser.prototype.loadBufferView = function ( bufferViewIndex ) {
 
 
-				var byteLength = bufferView.byteLength || 0;
-				var byteOffset = bufferView.byteOffset || 0;
+		var bufferViewDef = this.json.bufferViews[ bufferViewIndex ];
 
 
-				return arraybuffer.slice( byteOffset, byteOffset + byteLength );
+		return this.getDependency( 'buffer', bufferViewDef.buffer ).then( function ( buffer ) {
 
 
-			} );
+			var byteLength = bufferViewDef.byteLength || 0;
+			var byteOffset = bufferViewDef.byteOffset || 0;
+			return buffer.slice( byteOffset, byteOffset + byteLength );
 
 
 		} );
 		} );
 
 
@@ -1356,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 ];
 
 
@@ -1380,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 );
@@ -1389,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 );
 
 
@@ -1401,230 +1414,243 @@ THREE.GLTF2Loader = ( function () {
 
 
 	};
 	};
 
 
-	GLTFParser.prototype.loadTextures = function () {
+	/**
+	 * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures
+	 * @param {number} textureIndex
+	 * @return {Promise<THREE.Texture>}
+	 */
+	GLTFParser.prototype.loadTexture = function ( textureIndex ) {
 
 
+		var parser = this;
 		var json = this.json;
 		var json = this.json;
 		var options = this.options;
 		var options = this.options;
 
 
-		return this._withDependencies( [
-
-			'bufferViews'
-
-		] ).then( function ( dependencies ) {
+		var URL = window.URL || window.webkitURL;
 
 
-			return _each( json.textures, function ( texture ) {
+		var textureDef = json.textures[ textureIndex ];
+		var source = json.images[ textureDef.source ];
+		var sourceURI = source.uri;
+		var isObjectURL = false;
 
 
-				if ( texture.source !== undefined ) {
+		if ( source.bufferView !== undefined ) {
 
 
-					return new Promise( function ( resolve ) {
+			// Load binary image data from bufferView, if provided.
 
 
-						var source = json.images[ texture.source ];
-						var sourceUri = source.uri;
+			sourceURI = parser.getDependency( 'bufferView', source.bufferView )
+				.then( function ( bufferView ) {
 
 
-						var urlCreator;
+					isObjectURL = true;
+					var blob = new Blob( [ bufferView ], { type: source.mimeType } );
+					sourceURI = URL.createObjectURL( blob );
+					return sourceURI;
 
 
-						if ( source.bufferView !== undefined ) {
+				} );
 
 
-							var bufferView = dependencies.bufferViews[ source.bufferView ];
-							var blob = new Blob( [ bufferView ], { type: source.mimeType } );
-							urlCreator = window.URL || window.webkitURL;
-							sourceUri = urlCreator.createObjectURL( blob );
+		}
 
 
-						}
+		return Promise.resolve( sourceURI ).then( function ( sourceURI ) {
 
 
-						var textureLoader = THREE.Loader.Handlers.get( sourceUri );
+			// Load Texture resource.
 
 
-						if ( textureLoader === null ) {
+			var textureLoader = THREE.Loader.Handlers.get( sourceURI ) || new THREE.TextureLoader();
+			textureLoader.setCrossOrigin( options.crossOrigin );
 
 
-							textureLoader = new THREE.TextureLoader();
+			return new Promise( function ( resolve, reject ) {
 
 
-						}
+				textureLoader.load( resolveURL( sourceURI, options.path ), resolve, undefined, reject );
 
 
-						textureLoader.setCrossOrigin( options.crossOrigin );
+			} );
 
 
-						textureLoader.load( resolveURL( sourceUri, options.path ), function ( _texture ) {
+		} ).then( function ( texture ) {
 
 
-							if ( urlCreator !== undefined ) {
+			// Clean up resources and configure Texture.
 
 
-								urlCreator.revokeObjectURL( sourceUri );
+			if ( isObjectURL !== undefined ) {
 
 
-							}
+				URL.revokeObjectURL( sourceURI );
 
 
-							_texture.flipY = false;
+			}
 
 
-							if ( texture.name !== undefined ) _texture.name = texture.name;
+			texture.flipY = false;
 
 
-							_texture.format = texture.format !== undefined ? WEBGL_TEXTURE_FORMATS[ texture.format ] : THREE.RGBAFormat;
+			if ( textureDef.name !== undefined ) texture.name = textureDef.name;
 
 
-							if ( texture.internalFormat !== undefined && _texture.format !== WEBGL_TEXTURE_FORMATS[ texture.internalFormat ] ) {
+			texture.format = textureDef.format !== undefined ? WEBGL_TEXTURE_FORMATS[ textureDef.format ] : THREE.RGBAFormat;
 
 
-								console.warn( 'THREE.GLTF2Loader: Three.js does not support texture internalFormat which is different from texture format. ' +
-															'internalFormat will be forced to be the same value as format.' );
+			if ( textureDef.internalFormat !== undefined && texture.format !== WEBGL_TEXTURE_FORMATS[ textureDef.internalFormat ] ) {
 
 
-							}
+				console.warn( 'THREE.GLTF2Loader: Three.js does not support texture internalFormat which is different from texture format. ' +
+											'internalFormat will be forced to be the same value as format.' );
 
 
-							_texture.type = texture.type !== undefined ? WEBGL_TEXTURE_DATATYPES[ texture.type ] : THREE.UnsignedByteType;
+			}
 
 
-							var samplers = json.samplers || {};
-							var sampler = samplers[ texture.sampler ] || {};
+			texture.type = textureDef.type !== undefined ? WEBGL_TEXTURE_DATATYPES[ textureDef.type ] : THREE.UnsignedByteType;
 
 
-							_texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || THREE.LinearFilter;
-							_texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
-							_texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
-							_texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
+			var samplers = json.samplers || {};
+			var sampler = samplers[ textureDef.sampler ] || {};
 
 
-							resolve( _texture );
+			texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || THREE.LinearFilter;
+			texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || THREE.LinearMipMapLinearFilter;
+			texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
+			texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
 
 
-						}, undefined, function () {
+			return texture;
 
 
-							resolve();
+		} );
 
 
-						} );
+	};
 
 
-					} );
+	/**
+	 * Asynchronously assigns a texture to the given material parameters.
+	 * @param {Object} materialParams
+	 * @param {string} textureName
+	 * @param {number} textureIndex
+	 * @return {Promise}
+	 */
+	GLTFParser.prototype.assignTexture = function ( materialParams, textureName, textureIndex ) {
 
 
-				}
+		return this.getDependency( 'texture', textureIndex ).then( function ( texture ) {
 
 
-			} );
+			materialParams[ textureName ] = texture;
 
 
 		} );
 		} );
 
 
 	};
 	};
 
 
+	/**
+	 * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials
+	 * @return {Promise<Array<THREE.Material>>}
+	 */
 	GLTFParser.prototype.loadMaterials = function () {
 	GLTFParser.prototype.loadMaterials = function () {
 
 
+		var parser = this;
 		var json = this.json;
 		var json = this.json;
 		var extensions = this.extensions;
 		var extensions = this.extensions;
 
 
-		return this._withDependencies( [
+		return _each( json.materials, function ( material ) {
 
 
-			'textures'
+			var materialType;
+			var materialParams = {};
+			var materialExtensions = material.extensions || {};
 
 
-		] ).then( function ( dependencies ) {
-
-			return _each( json.materials, function ( material ) {
-
-				var materialType;
-				var materialParams = {};
-				var materialExtensions = material.extensions || {};
-
-				if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_COMMON ] ) {
-
-					materialType = extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].getMaterialType( material );
-					extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ].extendParams( materialParams, material, dependencies );
-
-				} else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) {
+			var pending = [];
 
 
-					materialType = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].getMaterialType( material );
-					extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].extendParams( materialParams, material, dependencies );
+			if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_COMMON ] ) {
 
 
-				} else if ( materialExtensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ] ) {
+				var khcExtension = extensions[ EXTENSIONS.KHR_MATERIALS_COMMON ];
+				materialType = khcExtension.getMaterialType( material );
+				pending.push( khcExtension.extendParams( materialParams, material, parser ) );
 
 
-					materialType = extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].getMaterialType( material );
-					extensions[ EXTENSIONS.KHR_TECHNIQUE_WEBGL ].extendParams( materialParams, material, dependencies );
+			} else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) {
 
 
-				} else if ( material.pbrMetallicRoughness !== undefined ) {
+				var sgExtension = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ];
+				materialType = sgExtension.getMaterialType( material );
+				pending.push( sgExtension.extendParams( materialParams, material, parser ) );
 
 
-					// Specification:
-					// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
+			} else if ( material.pbrMetallicRoughness !== undefined ) {
 
 
-					materialType = THREE.MeshStandardMaterial;
+				// Specification:
+				// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
 
 
-					var metallicRoughness = material.pbrMetallicRoughness;
+				materialType = THREE.MeshStandardMaterial;
 
 
-					materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 );
-					materialParams.opacity = 1.0;
+				var metallicRoughness = material.pbrMetallicRoughness;
 
 
-					if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
+				materialParams.color = new THREE.Color( 1.0, 1.0, 1.0 );
+				materialParams.opacity = 1.0;
 
 
-						var array = metallicRoughness.baseColorFactor;
+				if ( Array.isArray( metallicRoughness.baseColorFactor ) ) {
 
 
-						materialParams.color.fromArray( array );
-						materialParams.opacity = array[ 3 ];
+					var array = metallicRoughness.baseColorFactor;
 
 
-					}
+					materialParams.color.fromArray( array );
+					materialParams.opacity = array[ 3 ];
 
 
-					if ( metallicRoughness.baseColorTexture !== undefined ) {
+				}
 
 
-						materialParams.map = dependencies.textures[ metallicRoughness.baseColorTexture.index ];
+				if ( metallicRoughness.baseColorTexture !== undefined ) {
 
 
-					}
+					pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture.index ) );
 
 
-					materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0;
-					materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0;
+				}
 
 
-					if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
+				materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0;
+				materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0;
 
 
-						var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
-						materialParams.metalnessMap = dependencies.textures[ textureIndex ];
-						materialParams.roughnessMap = dependencies.textures[ textureIndex ];
+				if ( metallicRoughness.metallicRoughnessTexture !== undefined ) {
 
 
-					}
+					var textureIndex = metallicRoughness.metallicRoughnessTexture.index;
+					pending.push( parser.assignTexture( materialParams, 'metalnessMap', textureIndex ) );
+					pending.push( parser.assignTexture( materialParams, 'roughnessMap', textureIndex ) );
 
 
-				} else {
+				}
 
 
-					materialType = THREE.MeshPhongMaterial;
+			} else {
 
 
-				}
+				materialType = THREE.MeshPhongMaterial;
 
 
-				if ( material.doubleSided === true ) {
+			}
 
 
-					materialParams.side = THREE.DoubleSide;
+			if ( material.doubleSided === true ) {
 
 
-				}
+				materialParams.side = THREE.DoubleSide;
 
 
-				var alphaMode = material.alphaMode || ALPHA_MODES.OPAQUE;
+			}
 
 
-				if ( alphaMode !== ALPHA_MODES.OPAQUE ) {
+			var alphaMode = material.alphaMode || ALPHA_MODES.OPAQUE;
 
 
-					materialParams.transparent = true;
+			if ( alphaMode !== ALPHA_MODES.OPAQUE ) {
 
 
-				} else {
+				materialParams.transparent = true;
 
 
-					materialParams.transparent = false;
+			} else {
 
 
-				}
+				materialParams.transparent = false;
 
 
-				if ( material.normalTexture !== undefined ) {
+			}
 
 
-					materialParams.normalMap = dependencies.textures[ material.normalTexture.index ];
+			if ( material.normalTexture !== undefined ) {
 
 
-				}
+				pending.push( parser.assignTexture( materialParams, 'normalMap', material.normalTexture.index ) );
 
 
-				if ( material.occlusionTexture !== undefined ) {
+			}
 
 
-					materialParams.aoMap = dependencies.textures[ material.occlusionTexture.index ];
+			if ( material.occlusionTexture !== undefined ) {
 
 
-				}
+				pending.push( parser.assignTexture( materialParams, 'aoMap', material.occlusionTexture.index ) );
 
 
-				if ( material.emissiveFactor !== undefined ) {
+			}
 
 
-					if ( materialType === THREE.MeshBasicMaterial ) {
+			if ( material.emissiveFactor !== undefined ) {
 
 
-						materialParams.color = new THREE.Color().fromArray( material.emissiveFactor );
+				if ( materialType === THREE.MeshBasicMaterial ) {
 
 
-					} else {
+					materialParams.color = new THREE.Color().fromArray( material.emissiveFactor );
 
 
-						materialParams.emissive = new THREE.Color().fromArray( material.emissiveFactor );
+				} else {
 
 
-					}
+					materialParams.emissive = new THREE.Color().fromArray( material.emissiveFactor );
 
 
 				}
 				}
 
 
-				if ( material.emissiveTexture !== undefined ) {
+			}
 
 
-					if ( materialType === THREE.MeshBasicMaterial ) {
+			if ( material.emissiveTexture !== undefined ) {
 
 
-						materialParams.map = dependencies.textures[ material.emissiveTexture.index ];
+				if ( materialType === THREE.MeshBasicMaterial ) {
 
 
-					} else {
+					pending.push( parser.assignTexture( materialParams, 'map', material.emissiveTexture.index ) );
 
 
-						materialParams.emissiveMap = dependencies.textures[ material.emissiveTexture.index ];
+				} else {
 
 
-					}
+					pending.push( parser.assignTexture( materialParams, 'emissiveMap', material.emissiveTexture.index ) );
 
 
 				}
 				}
 
 
+			}
+
+			return Promise.all( pending ).then( function () {
+
 				var _material;
 				var _material;
 
 
 				if ( materialType === THREE.ShaderMaterial ) {
 				if ( materialType === THREE.ShaderMaterial ) {
@@ -1658,7 +1684,6 @@ THREE.GLTF2Loader = ( function () {
 		return this._withDependencies( [
 		return this._withDependencies( [
 
 
 			'accessors',
 			'accessors',
-			'bufferViews',
 
 
 		] ).then( function ( dependencies ) {
 		] ).then( function ( dependencies ) {
 
 
@@ -1747,7 +1772,6 @@ THREE.GLTF2Loader = ( function () {
 
 
 		return this._withDependencies( [
 		return this._withDependencies( [
 
 
-			'accessors',
 			'materials'
 			'materials'
 
 
 		] ).then( function ( dependencies ) {
 		] ).then( function ( dependencies ) {