Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GLTFLoader: Deduplicate node names. #16639

Merged
merged 3 commits into from
Sep 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ THREE.GLTFLoader = ( function () {

if ( lightDef.intensity !== undefined ) lightNode.intensity = lightDef.intensity;

lightNode.name = lightDef.name || ( 'light_' + lightIndex );
lightNode.name = parser.createUniqueName( lightDef.name || ( 'light_' + lightIndex ) );

dependency = Promise.resolve( lightNode );

Expand Down Expand Up @@ -1674,6 +1674,9 @@ THREE.GLTFLoader = ( function () {
this.cameraCache = { refs: {}, uses: {} };
this.lightCache = { refs: {}, uses: {} };

// Track node names, to ensure no duplicates
this.nodeNamesUsed = {};

// Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the
// expensive work of uploading a texture to the GPU off the main thread.
if ( typeof createImageBitmap !== 'undefined' && /Firefox/.test( navigator.userAgent ) === false ) {
Expand Down Expand Up @@ -2668,6 +2671,23 @@ THREE.GLTFLoader = ( function () {

};

/** When Object3D instances are targeted by animation, they need unique names. */
GLTFParser.prototype.createUniqueName = function ( originalName ) {

var name = THREE.PropertyBinding.sanitizeNodeName( originalName || '' );

for ( var i = 1; this.nodeNamesUsed[ name ]; ++ i ) {

name = originalName + '_' + i;

}

this.nodeNamesUsed[ name ] = true;

return name;

};

donmccurdy marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param {THREE.BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
Expand Down Expand Up @@ -3091,7 +3111,7 @@ THREE.GLTFLoader = ( function () {

}

mesh.name = meshDef.name || ( 'mesh_' + meshIndex );
mesh.name = parser.createUniqueName( meshDef.name || ( 'mesh_' + meshIndex ) );

if ( geometries.length > 1 ) mesh.name += '_' + i;

Expand Down Expand Up @@ -3151,7 +3171,7 @@ THREE.GLTFLoader = ( function () {

}

if ( cameraDef.name ) camera.name = cameraDef.name;
if ( cameraDef.name ) camera.name = this.createUniqueName( cameraDef.name );

assignExtrasToUserData( camera, cameraDef );

Expand Down Expand Up @@ -3393,6 +3413,9 @@ THREE.GLTFLoader = ( function () {

var nodeDef = json.nodes[ nodeIndex ];

// reserve node's name before its dependencies, so the root has the intended name.
var nodeName = nodeDef.name ? parser.createUniqueName( nodeDef.name ) : '';

return ( function () {

var pending = [];
Expand Down Expand Up @@ -3484,7 +3507,7 @@ THREE.GLTFLoader = ( function () {
if ( nodeDef.name ) {

node.userData.name = nodeDef.name;
node.name = THREE.PropertyBinding.sanitizeNodeName( nodeDef.name );
node.name = nodeName;

}

Expand Down Expand Up @@ -3643,7 +3666,7 @@ THREE.GLTFLoader = ( function () {
// Loader returns Group, not Scene.
// See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172
var scene = new THREE.Group();
if ( sceneDef.name ) scene.name = sceneDef.name;
if ( sceneDef.name ) scene.name = parser.createUniqueName( sceneDef.name );

assignExtrasToUserData( scene, sceneDef );

Expand Down
33 changes: 28 additions & 5 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ var GLTFLoader = ( function () {

if ( lightDef.intensity !== undefined ) lightNode.intensity = lightDef.intensity;

lightNode.name = lightDef.name || ( 'light_' + lightIndex );
lightNode.name = parser.createUniqueName( lightDef.name || ( 'light_' + lightIndex ) );

dependency = Promise.resolve( lightNode );

Expand Down Expand Up @@ -1737,6 +1737,9 @@ var GLTFLoader = ( function () {
this.cameraCache = { refs: {}, uses: {} };
this.lightCache = { refs: {}, uses: {} };

// Track node names, to ensure no duplicates
this.nodeNamesUsed = {};

// Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the
// expensive work of uploading a texture to the GPU off the main thread.
if ( typeof createImageBitmap !== 'undefined' && /Firefox/.test( navigator.userAgent ) === false ) {
Expand Down Expand Up @@ -2731,6 +2734,23 @@ var GLTFLoader = ( function () {

};

/** When Object3D instances are targeted by animation, they need unique names. */
GLTFParser.prototype.createUniqueName = function ( originalName ) {

var name = PropertyBinding.sanitizeNodeName( originalName || '' );

for ( var i = 1; this.nodeNamesUsed[ name ]; ++ i ) {

name = originalName + '_' + i;

}

this.nodeNamesUsed[ name ] = true;

return name;

};

/**
* @param {BufferGeometry} geometry
* @param {GLTF.Primitive} primitiveDef
Expand Down Expand Up @@ -3154,7 +3174,7 @@ var GLTFLoader = ( function () {

}

mesh.name = meshDef.name || ( 'mesh_' + meshIndex );
mesh.name = parser.createUniqueName( meshDef.name || ( 'mesh_' + meshIndex ) );

if ( geometries.length > 1 ) mesh.name += '_' + i;

Expand Down Expand Up @@ -3214,7 +3234,7 @@ var GLTFLoader = ( function () {

}

if ( cameraDef.name ) camera.name = cameraDef.name;
if ( cameraDef.name ) camera.name = this.createUniqueName( cameraDef.name );

assignExtrasToUserData( camera, cameraDef );

Expand Down Expand Up @@ -3456,6 +3476,9 @@ var GLTFLoader = ( function () {

var nodeDef = json.nodes[ nodeIndex ];

// reserve node's name before its dependencies, so the root has the intended name.
var nodeName = nodeDef.name ? parser.createUniqueName( nodeDef.name ) : '';

return ( function () {

var pending = [];
Expand Down Expand Up @@ -3547,7 +3570,7 @@ var GLTFLoader = ( function () {
if ( nodeDef.name ) {

node.userData.name = nodeDef.name;
node.name = PropertyBinding.sanitizeNodeName( nodeDef.name );
node.name = nodeName;

}

Expand Down Expand Up @@ -3706,7 +3729,7 @@ var GLTFLoader = ( function () {
// Loader returns Group, not Scene.
// See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172
var scene = new Group();
if ( sceneDef.name ) scene.name = sceneDef.name;
if ( sceneDef.name ) scene.name = parser.createUniqueName( sceneDef.name );

assignExtrasToUserData( scene, sceneDef );

Expand Down