Skip to content

GLTFExporter uses wrong cache value baseAttribute instead of relativeAttribute #24074

@sotrh

Description

@sotrh

Describe the bug

We have a model that is multiple cubes with the same POSITION attributes. One of the cubes has a morph target. When we export this model the mesh with the morph target came out correct, but the other meshes had their position data messed up.

I did some digging and realized that the POSITION attribute for the cubes w/o morph targets were using the attribute for the morph target. We're not creating/editing attributes at all, and the scene looked fine before export, so I started digging into the GLTFExporter code. I tracked the issue down to this code:

// Clones attribute not to override
const relativeAttribute = attribute.clone();

if ( ! geometry.morphTargetsRelative ) {

for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {

	relativeAttribute.setXYZ(
		j,
		attribute.getX( j ) - baseAttribute.getX( j ),
		attribute.getY( j ) - baseAttribute.getY( j ),
		attribute.getZ( j ) - baseAttribute.getZ( j )
	);

}

}

target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
cache.attributes.set( this.getUID( baseAttribute ), target[ gltfAttributeName ] );

The last line is the culprit, but I included more for context. Basically the baseAttribute is used to update cache.attributes instead of relativeAttribute which caused subsequent meshes to processed incorrectly as the would use the morph attribute instead of the position one.

To Reproduce

Steps to reproduce the behavior:

  1. Load the following model using the GLTFLoader
    model(13).zip

  2. Export the model using GLTFExporter.parse using the following options:

{
  binary: true,
}
  1. If you inspect the exported model you should see that one of the cubes (the one with the morph target) is correct, while the others are using the accessor id for the morph target data.

Expected behavior

The exporter should cache the morph target attribute using relativeAttribute instead of baseAttribute.

Platform:

  • Device: [Desktop, Mobile]
  • OS: [Windows, MacOS, Linux, Android, iOS]
  • Browser: [Chrome, Firefox, Safari, Edge]
  • Three.js version: [dev, r140]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions