Skip to content

Commit 4c75646

Browse files
authored
Merge pull request #19892 from takahirox/GLTFLoaderNodeHook
Add node hookpoint to GLTFLoader and move the lights extension handler to the new system
2 parents cb392c3 + 345c52e commit 4c75646

File tree

2 files changed

+168
-72
lines changed

2 files changed

+168
-72
lines changed

examples/js/loaders/GLTFLoader.js

Lines changed: 84 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ THREE.GLTFLoader = ( function () {
1717
return new GLTFMaterialsClearcoatExtension( parser );
1818

1919
} );
20+
2021
this.register( function ( parser ) {
2122

2223
return new GLTFTextureBasisUExtension( parser );
@@ -29,6 +30,12 @@ THREE.GLTFLoader = ( function () {
2930

3031
} );
3132

33+
this.register( function ( parser ) {
34+
35+
return new GLTFLightsExtension( parser );
36+
37+
} );
38+
3239
}
3340

3441
GLTFLoader.prototype = Object.assign( Object.create( THREE.Loader.prototype ), {
@@ -235,10 +242,6 @@ THREE.GLTFLoader = ( function () {
235242

236243
switch ( extensionName ) {
237244

238-
case EXTENSIONS.KHR_LIGHTS_PUNCTUAL:
239-
extensions[ extensionName ] = new GLTFLightsExtension( json );
240-
break;
241-
242245
case EXTENSIONS.KHR_MATERIALS_UNLIT:
243246
extensions[ extensionName ] = new GLTFMaterialsUnlitExtension();
244247
break;
@@ -363,21 +366,53 @@ THREE.GLTFLoader = ( function () {
363366
*
364367
* Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
365368
*/
366-
function GLTFLightsExtension( json ) {
369+
function GLTFLightsExtension( parser ) {
367370

371+
this.parser = parser;
368372
this.name = EXTENSIONS.KHR_LIGHTS_PUNCTUAL;
369373

370-
var extension = ( json.extensions && json.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ] ) || {};
371-
this.lightDefs = extension.lights || [];
374+
// Object3D instance caches
375+
this.cache = { refs: {}, uses: {} };
372376

373377
}
374378

375-
GLTFLightsExtension.prototype.loadLight = function ( lightIndex ) {
379+
GLTFLightsExtension.prototype._markDefs = function () {
380+
381+
var parser = this.parser;
382+
var nodeDefs = this.parser.json.nodes || [];
383+
384+
for ( var nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex ++ ) {
385+
386+
var nodeDef = nodeDefs[ nodeIndex ];
376387

377-
var lightDef = this.lightDefs[ lightIndex ];
388+
if ( nodeDef.extensions
389+
&& nodeDef.extensions[ this.name ]
390+
&& nodeDef.extensions[ this.name ].light !== undefined ) {
391+
392+
parser._addNodeRef( this.cache, nodeDef.extensions[ this.name ].light );
393+
394+
}
395+
396+
}
397+
398+
};
399+
400+
GLTFLightsExtension.prototype._loadLight = function ( lightIndex ) {
401+
402+
var parser = this.parser;
403+
var cacheKey = 'light:' + lightIndex;
404+
var dependency = parser.cache.get( cacheKey );
405+
406+
if ( dependency ) return dependency;
407+
408+
var json = parser.json;
409+
var extensions = ( json.extensions && json.extensions[ this.name ] ) || {};
410+
var lightDefs = extensions.lights || [];
411+
var lightDef = lightDefs[ lightIndex ];
378412
var lightNode;
379413

380414
var color = new THREE.Color( 0xffffff );
415+
381416
if ( lightDef.color !== undefined ) color.fromArray( lightDef.color );
382417

383418
var range = lightDef.range !== undefined ? lightDef.range : 0;
@@ -423,7 +458,30 @@ THREE.GLTFLoader = ( function () {
423458

424459
lightNode.name = lightDef.name || ( 'light_' + lightIndex );
425460

426-
return Promise.resolve( lightNode );
461+
dependency = Promise.resolve( lightNode );
462+
463+
parser.cache.add( cacheKey, dependency );
464+
465+
return dependency;
466+
467+
};
468+
469+
GLTFLightsExtension.prototype.createNodeAttachment = function ( nodeIndex ) {
470+
471+
var self = this;
472+
var parser = this.parser;
473+
var json = parser.json;
474+
var nodeDef = json.nodes[ nodeIndex ];
475+
var lightDef = ( nodeDef.extensions && nodeDef.extensions[ this.name ] ) || {};
476+
var lightIndex = lightDef.light;
477+
478+
if ( lightIndex === undefined ) return null;
479+
480+
return this._loadLight( lightIndex ).then( function ( light ) {
481+
482+
return parser._getNodeRef( self.cache, lightIndex, light );
483+
484+
} );
427485

428486
};
429487

@@ -1668,7 +1726,11 @@ THREE.GLTFLoader = ( function () {
16681726
this.cache.removeAll();
16691727

16701728
// Mark the special nodes/meshes in json for efficient parse
1671-
this._markDefs();
1729+
this._invokeAll( function ( ext ) {
1730+
1731+
return ext._markDefs && ext._markDefs();
1732+
1733+
} );
16721734

16731735
Promise.all( [
16741736

@@ -1748,14 +1810,6 @@ THREE.GLTFLoader = ( function () {
17481810

17491811
}
17501812

1751-
if ( nodeDef.extensions
1752-
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ]
1753-
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].light !== undefined ) {
1754-
1755-
this._addNodeRef( this.lightCache, nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].light );
1756-
1757-
}
1758-
17591813
}
17601814

17611815
};
@@ -1820,11 +1874,13 @@ THREE.GLTFLoader = ( function () {
18201874

18211875
for ( var i = 0; i < extensions.length; i ++ ) {
18221876

1823-
pending.push( func( extensions[ i ] ) );
1877+
var result = func( extensions[ i ] );
1878+
1879+
if ( result ) pending.push( result );
18241880

18251881
}
18261882

1827-
return Promise.all( pending );
1883+
return pending;
18281884

18291885
};
18301886

@@ -1903,10 +1959,6 @@ THREE.GLTFLoader = ( function () {
19031959
dependency = this.loadCamera( index );
19041960
break;
19051961

1906-
case 'light':
1907-
dependency = this.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].loadLight( index );
1908-
break;
1909-
19101962
default:
19111963
throw new Error( 'Unknown type: ' + type );
19121964

@@ -2516,11 +2568,11 @@ THREE.GLTFLoader = ( function () {
25162568

25172569
} );
25182570

2519-
pending.push( this._invokeAll( function ( ext ) {
2571+
pending.push( Promise.all( this._invokeAll( function ( ext ) {
25202572

25212573
return ext.extendMaterialParams && ext.extendMaterialParams( materialIndex, materialParams );
25222574

2523-
} ) );
2575+
} ) ) );
25242576

25252577
}
25262578

@@ -3389,19 +3441,15 @@ THREE.GLTFLoader = ( function () {
33893441

33903442
}
33913443

3392-
if ( nodeDef.extensions
3393-
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ]
3394-
&& nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].light !== undefined ) {
3395-
3396-
var lightIndex = nodeDef.extensions[ EXTENSIONS.KHR_LIGHTS_PUNCTUAL ].light;
3444+
parser._invokeAll( function ( ext ) {
33973445

3398-
pending.push( parser.getDependency( 'light', lightIndex ).then( function ( light ) {
3446+
return ext.createNodeAttachment && ext.createNodeAttachment( nodeIndex );
33993447

3400-
return parser._getNodeRef( parser.lightCache, lightIndex, light );
3448+
} ).forEach( function ( promise ) {
34013449

3402-
} ) );
3450+
pending.push( promise );
34033451

3404-
}
3452+
} );
34053453

34063454
return Promise.all( pending );
34073455

0 commit comments

Comments
 (0)