Просмотр исходного кода

GLTFLoader: Disambiguate names for multiple instances of the same mesh.

Don McCurdy 7 лет назад
Родитель
Сommit
ed042044aa
1 измененных файлов с 33 добавлено и 1 удалено
  1. 33 1
      examples/js/loaders/GLTFLoader.js

+ 33 - 1
examples/js/loaders/GLTFLoader.js

@@ -2225,6 +2225,9 @@ THREE.GLTFLoader = ( function () {
 		var nodes = json.nodes || [];
 		var skins = json.skins || [];
 
+		var meshReferences = {};
+		var meshUses = {};
+
 		// Nothing in the node definition indicates whether it is a Bone or an
 		// Object3D. Use the skins' joint references to mark bones.
 		skins.forEach( function ( skin ) {
@@ -2237,6 +2240,27 @@ THREE.GLTFLoader = ( function () {
 
 		} );
 
+		// Meshes can (and should) be reused by multiple nodes in a glTF asset. To
+		// avoid having more than one THREE.Mesh with the same name, count
+		// references and rename instances below.
+		//
+		// Example: CesiumMilkTruck sample model reuses "Wheel" meshes.
+		nodes.forEach( function ( nodeDef ) {
+
+			if ( nodeDef.mesh ) {
+
+				if ( meshReferences[ nodeDef.mesh ] === undefined ) {
+
+					meshReferences[ nodeDef.mesh ] = meshUses[ nodeDef.mesh ] = 0;
+
+				}
+
+				meshReferences[ nodeDef.mesh ]++;
+
+			}
+
+		} );
+
 		return scope._withDependencies( [
 
 			'meshes',
@@ -2253,7 +2277,15 @@ THREE.GLTFLoader = ( function () {
 
 				} else if ( nodeDef.mesh !== undefined ) {
 
-					return dependencies.meshes[ nodeDef.mesh ].clone();
+					var mesh = dependencies.meshes[ nodeDef.mesh ].clone();
+
+					if ( meshReferences[ nodeDef.mesh ] > 1 ) {
+
+						mesh.name += '_instance_' + meshUses[ nodeDef.mesh ]++;
+
+					}
+
+					return mesh;
 
 				} else if ( nodeDef.camera !== undefined ) {