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: Add createNodeMesh hook #21458

Merged
merged 3 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 35 additions & 21 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,39 @@ THREE.GLTFLoader = ( function () {

};

GLTFParser.prototype.createNodeMesh = function ( nodeIndex ) {

var json = this.json;
var parser = this;
var nodeDef = json.nodes[ nodeIndex ];

return parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) {

var node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh );

// if weights are provided on the node, override weights on the mesh.
if ( nodeDef.weights !== undefined ) {

node.traverse( function ( o ) {

if ( ! o.isMesh ) return;

for ( var i = 0, il = nodeDef.weights.length; i < il; i ++ ) {

o.morphTargetInfluences[ i ] = nodeDef.weights[ i ];

}

} );

}

return node;

} );

};

/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy
* @param {number} nodeIndex
Expand All @@ -3581,28 +3614,9 @@ THREE.GLTFLoader = ( function () {

if ( nodeDef.mesh !== undefined ) {

pending.push( parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) {

var node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh );

// if weights are provided on the node, override weights on the mesh.
if ( nodeDef.weights !== undefined ) {

node.traverse( function ( o ) {
pending.push( parser._invokeOne( function ( ext ) {

if ( ! o.isMesh ) return;

for ( var i = 0, il = nodeDef.weights.length; i < il; i ++ ) {

o.morphTargetInfluences[ i ] = nodeDef.weights[ i ];

}

} );

}

return node;
return ext.createNodeMesh && ext.createNodeMesh( nodeIndex );

Copy link
Collaborator Author

@takahirox takahirox Mar 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I found a problem in this implementation after the review has been done. Plugins' createNodeMesh() should be called even if core nodeDef.mesh is undefined. e.g. GPU instancing extension mesh without fallback in core. I would update soon.

Nit: EXT_mesh_gpu_instancing doesn't define a behavior for a node with the extension that doesn't also have a mesh so the example above may not be good, anyways.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

} ) );

Expand Down
56 changes: 35 additions & 21 deletions examples/jsm/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3624,6 +3624,39 @@ var GLTFLoader = ( function () {

};

GLTFParser.prototype.createNodeMesh = function ( nodeIndex ) {

var json = this.json;
var parser = this;
var nodeDef = json.nodes[ nodeIndex ];

return parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) {

var node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh );

// if weights are provided on the node, override weights on the mesh.
if ( nodeDef.weights !== undefined ) {

node.traverse( function ( o ) {

if ( ! o.isMesh ) return;

for ( var i = 0, il = nodeDef.weights.length; i < il; i ++ ) {

o.morphTargetInfluences[ i ] = nodeDef.weights[ i ];

}

} );

}

return node;

} );

};

/**
* Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy
* @param {number} nodeIndex
Expand All @@ -3646,28 +3679,9 @@ var GLTFLoader = ( function () {

if ( nodeDef.mesh !== undefined ) {

pending.push( parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) {

var node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh );

// if weights are provided on the node, override weights on the mesh.
if ( nodeDef.weights !== undefined ) {

node.traverse( function ( o ) {
pending.push( parser._invokeOne( function ( ext ) {

if ( ! o.isMesh ) return;

for ( var i = 0, il = nodeDef.weights.length; i < il; i ++ ) {

o.morphTargetInfluences[ i ] = nodeDef.weights[ i ];

}

} );

}

return node;
return ext.createNodeMesh && ext.createNodeMesh( nodeIndex );

} ) );

Expand Down