From ed042044aa3cab6bf4b22aadd25d4d725b6b22dd Mon Sep 17 00:00:00 2001 From: Don McCurdy Date: Mon, 9 Oct 2017 19:04:06 -0700 Subject: [PATCH] GLTFLoader: Disambiguate names for multiple instances of the same mesh. --- examples/js/loaders/GLTFLoader.js | 34 ++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/examples/js/loaders/GLTFLoader.js b/examples/js/loaders/GLTFLoader.js index ea37082f2b..350649dd56 100644 --- a/examples/js/loaders/GLTFLoader.js +++ b/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 ) { -- GitLab