Преглед изворни кода

Merge pull request #19359 from cdata/track-gltf-associations

Track glTF associations for node, material and texture
Mr.doob пре 5 година
родитељ
комит
c47fde754d
2 измењених фајлова са 26 додато и 2 уклоњено
  1. 11 1
      examples/jsm/loaders/GLTFLoader.d.ts
  2. 15 1
      examples/jsm/loaders/GLTFLoader.js

+ 11 - 1
examples/jsm/loaders/GLTFLoader.d.ts

@@ -3,7 +3,10 @@ import {
 	Camera,
 	Group,
 	Loader,
-	LoadingManager
+	LoadingManager,
+	Object3D,
+	Material,
+	Texture
 } from '../../../src/Three';
 
 import { DRACOLoader } from './DRACOLoader';
@@ -39,10 +42,17 @@ export class GLTFLoader extends Loader {
 
 }
 
+export interface GLTFReference {
+	type: 'materials'|'nodes'|'textures';
+	index: number;
+}
+
 export class GLTFParser {
 
 	json: any;
 
+	associations: Map<Object3D|Material|Texture, GLTFReference>;
+
 	getDependency: ( type: string, index: number ) => Promise<any>;
 	getDependencies: ( type: string ) => Promise<any[]>;
 

+ 15 - 1
examples/jsm/loaders/GLTFLoader.js

@@ -1468,6 +1468,9 @@ var 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 = {};
 
@@ -1977,6 +1980,11 @@ var GLTFLoader = ( function () {
 			texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping;
 			texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping;
 
+			parser.associations.set( texture, {
+				type: 'textures',
+				index: textureIndex
+			} );
+
 			return texture;
 
 		} );
@@ -2026,7 +2034,9 @@ var 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 );
 
 				}
 
@@ -2125,7 +2135,7 @@ var GLTFLoader = ( function () {
 				if ( useMorphNormals ) cachedMaterial.morphNormals = true;
 
 				this.cache.add( cacheKey, cachedMaterial );
-
+				this.associations.set( cachedMaterial, this.associations.get( material ) );
 			}
 
 			material = cachedMaterial;
@@ -2321,6 +2331,8 @@ var GLTFLoader = ( function () {
 
 			assignExtrasToUserData( material, materialDef );
 
+			parser.associations.set( material, { type: 'materials', index: materialIndex } );
+
 			if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef );
 
 			return material;
@@ -3191,6 +3203,8 @@ var GLTFLoader = ( function () {
 
 			}
 
+			parser.associations.set( node, { type: 'nodes', index: nodeIndex } );
+
 			return node;
 
 		} );