Browse Source

GLTFLoader: Clean up.

Mr.doob 5 years ago
parent
commit
475efec944
2 changed files with 18 additions and 0 deletions
  1. 16 0
      examples/js/loaders/GLTFLoader.js
  2. 2 0
      examples/jsm/loaders/GLTFLoader.js

+ 16 - 0
examples/js/loaders/GLTFLoader.js

@@ -1404,6 +1404,9 @@ THREE.GLTFLoader = ( function () {
 		// loader object cache
 		this.cache = new GLTFRegistry();
 
+		// associations between Three.js objects and glTF elements
+		this.associations = new Map();
+
 		// BufferGeometry caching
 		this.primitiveCache = {};
 
@@ -1913,6 +1916,11 @@ THREE.GLTFLoader = ( function () {
 			texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || THREE.RepeatWrapping;
 			texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || THREE.RepeatWrapping;
 
+			parser.associations.set( texture, {
+				type: 'textures',
+				index: textureIndex
+			} );
+
 			return texture;
 
 		} );
@@ -1962,7 +1970,9 @@ THREE.GLTFLoader = ( function () {
 
 				if ( transform ) {
 
+					var gltfReference = this.associations.get( texture );
 					texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, transform );
+					this.associations.set( texture, gltfReference );
 
 				}
 
@@ -2062,6 +2072,8 @@ THREE.GLTFLoader = ( function () {
 
 				this.cache.add( cacheKey, cachedMaterial );
 
+				this.associations.set( cachedMaterial, this.associations.get( material ) );
+
 			}
 
 			material = cachedMaterial;
@@ -2257,6 +2269,8 @@ THREE.GLTFLoader = ( function () {
 
 			assignExtrasToUserData( material, materialDef );
 
+			parser.associations.set( material, { type: 'materials', index: materialIndex } );
+
 			if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef );
 
 			return material;
@@ -3127,6 +3141,8 @@ THREE.GLTFLoader = ( function () {
 
 			}
 
+			parser.associations.set( node, { type: 'nodes', index: nodeIndex } );
+
 			return node;
 
 		} );

+ 2 - 0
examples/jsm/loaders/GLTFLoader.js

@@ -2135,7 +2135,9 @@ var GLTFLoader = ( function () {
 				if ( useMorphNormals ) cachedMaterial.morphNormals = true;
 
 				this.cache.add( cacheKey, cachedMaterial );
+
 				this.associations.set( cachedMaterial, this.associations.get( material ) );
+
 			}
 
 			material = cachedMaterial;