Skip to content

feat(packages/view): support to render MorphTargets#1427

Open
prince7688 wants to merge 4 commits intodonmccurdy:mainfrom
prince7688:transform-view/morphtargets
Open

feat(packages/view): support to render MorphTargets#1427
prince7688 wants to merge 4 commits intodonmccurdy:mainfrom
prince7688:transform-view/morphtargets

Conversation

@prince7688
Copy link
Copy Markdown

This will add support for Morph Targets in the @gltf-transform/view package, enabling animations for the weights channel target path.

Copy link
Copy Markdown
Owner

@donmccurdy donmccurdy left a comment

Choose a reason for hiding this comment

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

Thank you @prince7688!

Comment on lines +127 to +131
const meshValue = this.mesh.value as Group;

if (meshValue && !(this.value as Mesh).morphTargetInfluences) {
(this.value as Mesh).morphTargetInfluences = (meshValue.children?.[0] as Mesh)?.morphTargetInfluences;
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'd expect this.value to have either type THREE.Object3D or THREE.Bone here, depending on context — any THREE.Mesh types would be nested two levels down. Are you sure this part works as it should?

A unit test in NodeSubject.test.ts would be very welcome, though not required. :)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@donmccurdy The thing is, we need the morphTargetInfluences on the node object for the Animation (with Weights target path) to work properly. When THREE.JS applies the AnimationClip, it searches for the morphTargetInfluences property on that particular node that has the animation. I don't know if there's any other way to do this, but I've tested it with my current project, and it works properly. I've also added a test case in the NodeSubject.test.ts.

Comment on lines +34 to +45
// Convert geometry attributes
for (const accessor of def.listAttributes()) {
const array = accessor.getArray() as TypedArray;
geometry.setAttribute(accessor.getName(), new BufferAttribute(array, accessor.getElementSize()));
}

// Convert indices if available
if (def.getIndices()) {
const accessor = def.getIndices() as AccessorDef;
const array = accessor.getArray() as TypedArray;
geometry.setIndex(new BufferAttribute(array, 1));
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Attributes and indices are set up below, with a subscription callback to update them when definitions change — is it required to set them up a second time here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@donmccurdy You're right, there is no need to set them up a second time. I didn't look at the subscription callback; I've removed the code for Attributes and indices. Thanks.

Comment on lines +47 to +65
// Convert morph targets
const targets = def.listTargets();
if (targets.length > 0) {
geometry.morphAttributes = {};

for (const [index, target] of targets.entries()) {
for (const semantic of target.listSemantics()) {
const attriName = semantic.toLowerCase();
if (!geometry.morphAttributes[attriName]) {
geometry.morphAttributes[attriName] = [];
}
const accessor = target.getAttribute(semantic) as AccessorDef;
const array = accessor.getArray() as TypedArray;
geometry.morphAttributes[attriName][index] = new BufferAttribute(array, accessor.getElementSize());
}
}

geometry.morphTargetsRelative = true;
}
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

At some point, it will probably be necessary to create a PrimitiveTargetSubject class that handles listening for changes to each morph target definition, and emits events so that the BufferGeometry can be updated when things change.

It's OK with me if the morph targets are readonly (just one-time setup, no live updates) for now though. Perhaps this code for adding targets to the geometry could just be moved into PrimitiveSubject.createValue, or a helper function, to keep the constructor concise?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@donmccurdy For now, I've moved the code to PrimitiveSubject.createValue.

@surya-purohit
Copy link
Copy Markdown

@donmccurdy @prince7688 Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New enhancement or request package:view

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants