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: Support morph targets w/ interleaved attributes #12800

Merged
Merged
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: 29 additions & 4 deletions examples/js/loaders/GLTFLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,7 @@ THREE.GLTFLoader = ( function () {
// So morphTarget value will depend on mesh's position, then cloning attribute
// for the case if attribute is shared among two or more meshes.

positionAttribute = accessors[ target.POSITION ].clone();
positionAttribute = cloneBufferAttribute( accessors[ target.POSITION ] );
var position = geometry.attributes.position;

for ( var j = 0, jl = positionAttribute.count; j < jl; j ++ ) {
Expand All @@ -1118,7 +1118,7 @@ THREE.GLTFLoader = ( function () {

// Copying the original position not to affect the final position.
// See the formula above.
positionAttribute = geometry.attributes.position.clone();
positionAttribute = cloneBufferAttribute( geometry.attributes.position );

}

Expand All @@ -1135,7 +1135,7 @@ THREE.GLTFLoader = ( function () {

// see target.POSITION's comment

normalAttribute = accessors[ target.NORMAL ].clone();
normalAttribute = cloneBufferAttribute( accessors[ target.NORMAL ] );
var normal = geometry.attributes.normal;

for ( var j = 0, jl = normalAttribute.count; j < jl; j ++ ) {
Expand All @@ -1151,7 +1151,7 @@ THREE.GLTFLoader = ( function () {

} else if ( geometry.attributes.normal !== undefined ) {

normalAttribute = geometry.attributes.normal.clone();
normalAttribute = cloneBufferAttribute( geometry.attributes.normal );

}

Expand Down Expand Up @@ -1231,6 +1231,31 @@ THREE.GLTFLoader = ( function () {

}

function cloneBufferAttribute( attribute ) {

if ( attribute.isInterleavedBufferAttribute ) {

var count = attribute.count;
var itemSize = attribute.itemSize;
var array = attribute.array.slice( 0, count * itemSize );

for ( var i = 0; i < count; ++ i ) {

array[ i ] = attribute.getX( i );
if ( itemSize >= 2 ) array[ i + 1 ] = attribute.getY( i );
if ( itemSize >= 3 ) array[ i + 2 ] = attribute.getZ( i );
if ( itemSize >= 4 ) array[ i + 3 ] = attribute.getW( i );
Copy link
Collaborator

@Mugen87 Mugen87 Dec 11, 2018

Choose a reason for hiding this comment

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

@donmccurdy Um, I'm not sure this section is correct. If you use i in order to refer to the current array position, you are going to overwrite values after array[ i ] = attribute.getX( i ); since you raise the variable in the for loop. I think it should look like this:

var j = 0;

for ( var i = 0; i < count; ++ i ) {

    array[ j ++ ] = attribute.getX( i );
    if ( itemSize >= 2 ) array[ j ++ ] = attribute.getY( i );
    if ( itemSize >= 3 ) array[ j ++ ] = attribute.getZ( i );
    if ( itemSize >= 4 ) array[ j ++ ] = attribute.getW( i );

}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oops, yes. There's a sample file that uses interleaved morph targets in donmccurdy/three-gltf-viewer#56, but it looks like I never got an answer back from the OP about whether the result was what they expected.

Do you think we should fix this directly, or change something in a base class per #15395?

Copy link
Collaborator

@Mugen87 Mugen87 Dec 11, 2018

Choose a reason for hiding this comment

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

I would change it directly. I've actually added this code section in #15413. I'm not sure how much effort it takes not enable copy/clone and serialization/deserialization of interleaved buffer attributes. For now, I vote to create "normal" buffer attributes if we have to clone or serialization them.


}

return new THREE.BufferAttribute( array, itemSize, attribute.normalized );

}

return attribute.clone();

}

/* GLTF PARSER */

function GLTFParser( json, extensions, options ) {
Expand Down