Skip to content

Commit b53db68

Browse files
author
Chris Joel
committed
Track glTF associations for node, material and texture
1 parent 79edf22 commit b53db68

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

examples/jsm/loaders/GLTFLoader.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import {
33
Camera,
44
Group,
55
Loader,
6-
LoadingManager
6+
LoadingManager,
7+
Object3D,
8+
Material,
9+
Texture
710
} from '../../../src/Three';
811

912
import { DRACOLoader } from './DRACOLoader';
@@ -39,10 +42,17 @@ export class GLTFLoader extends Loader {
3942

4043
}
4144

45+
export interface GLTFReference {
46+
type: 'materials'|'nodes'|'textures';
47+
index: number;
48+
}
49+
4250
export class GLTFParser {
4351

4452
json: any;
4553

54+
associations: Map<Object3D|Material|Texture, GLTFReference>;
55+
4656
getDependency: ( type: string, index: number ) => Promise<any>;
4757
getDependencies: ( type: string ) => Promise<any[]>;
4858

examples/jsm/loaders/GLTFLoader.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,9 @@ var GLTFLoader = ( function () {
14681468
// loader object cache
14691469
this.cache = new GLTFRegistry();
14701470

1471+
// associations between Three.js objects and glTF elements
1472+
this.associations = new Map();
1473+
14711474
// BufferGeometry caching
14721475
this.primitiveCache = {};
14731476

@@ -1977,6 +1980,11 @@ var GLTFLoader = ( function () {
19771980
texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping;
19781981
texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping;
19791982

1983+
parser.associations.set( texture, {
1984+
type: 'textures',
1985+
index: textureIndex
1986+
} );
1987+
19801988
return texture;
19811989

19821990
} );
@@ -2026,7 +2034,9 @@ var GLTFLoader = ( function () {
20262034

20272035
if ( transform ) {
20282036

2037+
var gltfReference = this.associations.get( texture );
20292038
texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, transform );
2039+
this.associations.set( texture, gltfReference );
20302040

20312041
}
20322042

@@ -2125,7 +2135,7 @@ var GLTFLoader = ( function () {
21252135
if ( useMorphNormals ) cachedMaterial.morphNormals = true;
21262136

21272137
this.cache.add( cacheKey, cachedMaterial );
2128-
2138+
this.associations.set( cachedMaterial, this.associations.get( material ) );
21292139
}
21302140

21312141
material = cachedMaterial;
@@ -2321,6 +2331,8 @@ var GLTFLoader = ( function () {
23212331

23222332
assignExtrasToUserData( material, materialDef );
23232333

2334+
parser.associations.set( material, { type: 'materials', index: materialIndex } );
2335+
23242336
if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef );
23252337

23262338
return material;
@@ -3161,6 +3173,8 @@ var GLTFLoader = ( function () {
31613173

31623174
assignExtrasToUserData( node, nodeDef );
31633175

3176+
parser.associations.set( node, { type: 'nodes', index: nodeIndex } );
3177+
31643178
if ( nodeDef.extensions ) addUnknownExtensionsToUserData( extensions, node, nodeDef );
31653179

31663180
if ( nodeDef.matrix !== undefined ) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)