@@ -405,7 +405,7 @@ class GLTFLoader extends Loader {
405
405
}
406
406
407
407
/**
408
- * Parses the given FBX data and returns the resulting group .
408
+ * Parses the given glTF data and returns the generated data to `onLoad`, with the root Object3D in the `.scene` property .
409
409
*
410
410
* @param {string|ArrayBuffer } data - The raw glTF data.
411
411
* @param {string } path - The URL base path.
@@ -619,7 +619,8 @@ const EXTENSIONS = {
619
619
EXT_TEXTURE_WEBP : 'EXT_texture_webp' ,
620
620
EXT_TEXTURE_AVIF : 'EXT_texture_avif' ,
621
621
EXT_MESHOPT_COMPRESSION : 'EXT_meshopt_compression' ,
622
- EXT_MESH_GPU_INSTANCING : 'EXT_mesh_gpu_instancing'
622
+ EXT_MESH_GPU_INSTANCING : 'EXT_mesh_gpu_instancing' ,
623
+ GODOT_SINGLE_ROOT : 'GODOT_single_root' ,
623
624
} ;
624
625
625
626
/**
@@ -4479,18 +4480,28 @@ class GLTFParser {
4479
4480
4480
4481
const extensions = this . extensions ;
4481
4482
const sceneDef = this . json . scenes [ sceneIndex ] ;
4483
+ const nodeIds = sceneDef . nodes || [ ] ;
4482
4484
const parser = this ;
4485
+ const extensionsUsed = this . json . extensionsUsed ;
4486
+ const isSingleRoot = Array . isArray ( extensionsUsed ) ? extensionsUsed . includes ( EXTENSIONS . GODOT_SINGLE_ROOT ) : false ;
4483
4487
4484
- // Loader returns Group, not Scene.
4485
- // See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172
4486
- const scene = new Group ( ) ;
4487
- if ( sceneDef . name ) scene . name = parser . createUniqueName ( sceneDef . name ) ;
4488
+ let scene ;
4489
+ if ( isSingleRoot ) {
4488
4490
4489
- assignExtrasToUserData ( scene , sceneDef ) ;
4491
+ if ( nodeIds . length !== 1 ) {
4492
+ throw new Error ( 'THREE.GLTFLoader: glTF file with the single root flag must have exactly one scene root node. File is invalid.' ) ;
4493
+ }
4490
4494
4491
- if ( sceneDef . extensions ) addUnknownExtensionsToUserData ( extensions , scene , sceneDef ) ;
4495
+ } else {
4496
+ // Loader returns Group, not Scene.
4497
+ // See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172
4498
+ scene = new Group ( ) ;
4499
+ if ( sceneDef . name ) scene . name = parser . createUniqueName ( sceneDef . name ) ;
4492
4500
4493
- const nodeIds = sceneDef . nodes || [ ] ;
4501
+ assignExtrasToUserData ( scene , sceneDef ) ;
4502
+
4503
+ if ( sceneDef . extensions ) addUnknownExtensionsToUserData ( extensions , scene , sceneDef ) ;
4504
+ }
4494
4505
4495
4506
const pending = [ ] ;
4496
4507
@@ -4502,9 +4513,17 @@ class GLTFParser {
4502
4513
4503
4514
return Promise . all ( pending ) . then ( function ( nodes ) {
4504
4515
4505
- for ( let i = 0 , il = nodes . length ; i < il ; i ++ ) {
4516
+ if ( isSingleRoot ) {
4506
4517
4507
- scene . add ( nodes [ i ] ) ;
4518
+ scene = nodes [ 0 ] ;
4519
+
4520
+ } else {
4521
+
4522
+ for ( let i = 0 , il = nodes . length ; i < il ; i ++ ) {
4523
+
4524
+ scene . add ( nodes [ i ] ) ;
4525
+
4526
+ }
4508
4527
4509
4528
}
4510
4529
0 commit comments