Browse Source

GLTFLoader: Fix empty groups when multiple scenes reference same nodes (#32567)

David Wu 2 months ago
parent
commit
9488c011f4
1 changed files with 15 additions and 1 deletions
  1. 15 1
      examples/jsm/loaders/GLTFLoader.js

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

@@ -66,6 +66,7 @@ import {
 	InstancedBufferAttribute
 } from 'three';
 import { toTrianglesDrawMode } from '../utils/BufferGeometryUtils.js';
+import { clone } from '../utils/SkeletonUtils.js';
 
 /**
  * A loader for the glTF 2.0 format.
@@ -4510,7 +4511,20 @@ class GLTFParser {
 
 			for ( let i = 0, il = nodes.length; i < il; i ++ ) {
 
-				scene.add( nodes[ i ] );
+				const node = nodes[ i ];
+
+				// If the node already has a parent, it means it's being reused across multiple scenes.
+				// Clone it to avoid the second scene's add() removing it from the first scene.
+				// See: https://github.com/mrdoob/three.js/issues/27993
+				if ( node.parent !== null ) {
+
+					scene.add( clone( node ) );
+
+				} else {
+
+					scene.add( node );
+
+				}
 
 			}
 

粤ICP备19079148号