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

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions packages/view/src/subjects/NodeSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ export class NodeSubject extends Subject<NodeDef, Object3D> {
this.skin.update(def.getSkin());
this.light.update(def.getExtension('KHR_lights_punctual'));
this.instancedMesh.update(def.getExtension('EXT_mesh_gpu_instancing'));

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;
}
Comment on lines +127 to +131
Copy link
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
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.

}

dispose() {
Expand Down
23 changes: 23 additions & 0 deletions packages/view/src/subjects/PrimitiveSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Mesh,
Points,
SkinnedMesh,
TypedArray,
} from 'three';
import { Accessor as AccessorDef, Material as MaterialDef, Primitive as PrimitiveDef } from '@gltf-transform/core';
import type { DocumentViewSubjectAPI } from '../DocumentViewImpl.js';
Expand Down Expand Up @@ -88,6 +89,28 @@ export class PrimitiveSubject extends Subject<PrimitiveDef, MeshLike> {
material: Material,
pool: ValuePool<MeshLike>,
): MeshLike {
// Add Morph targets
const morphTargets = def.listTargets();
if (morphTargets.length) {
geometry.morphAttributes = {};

for (const [index, morphTarget] of morphTargets.entries()) {
for (const semantic of morphTarget.listSemantics()) {
const attributeName = semanticToAttributeName(semantic);
if (!geometry.morphAttributes[attributeName]) {
geometry.morphAttributes[attributeName] = [];
}
const accessor = morphTarget.getAttribute(semantic) as AccessorDef;
const array = accessor.getArray() as TypedArray;
geometry.morphAttributes[attributeName][index] = new BufferAttribute(
array,
accessor.getElementSize(),
);
}
}
geometry.morphTargetsRelative = true;
}

switch (def.getMode()) {
case PrimitiveDef.Mode.TRIANGLES:
case PrimitiveDef.Mode.TRIANGLE_FAN:
Expand Down
35 changes: 34 additions & 1 deletion packages/view/test/NodeSubject.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import test from 'ava';
import { JSDOM } from 'jsdom';
import { Document } from '@gltf-transform/core';
import { Document, Accessor } from '@gltf-transform/core';
import { DocumentView, NullImageProvider } from '@gltf-transform/view';
import { Mesh } from 'three';

global.document = new JSDOM().window.document;
const imageProvider = new NullImageProvider();
Expand Down Expand Up @@ -87,3 +88,35 @@ test('NodeSubject | update in place', async (t) => {
'node2.mesh.name',
);
});

test.only('NodeSubject | check morphTargetInfluences', async (t) => {
const document = new Document();

const AccessorDef = document
.createAccessor('myData')
.setArray(new Float32Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]))
.setType(Accessor.Type.VEC3)
.setBuffer(document.getRoot().listBuffers()[0]);

const primitiveTarget = document.createPrimitiveTarget('myTarget').setAttribute('POSITION', AccessorDef);

const primitiveDef = document.createPrimitive().setAttribute('POSITION', AccessorDef).addTarget(primitiveTarget);

const meshDef = document.createMesh().setName('Mesh.v1').addPrimitive(primitiveDef).setWeights([0.5]);
const nodeDef1 = document
.createNode('Node1')
.setTranslation([0, 2, 0])
.setRotation([0, 0, 0.707, 0.707])
.setScale([0.5, 0.5, 0.5])
.setMesh(meshDef);

const documentView = new DocumentView(document, { imageProvider });
const node1 = documentView.view(nodeDef1);

t.is(node1.name, 'Node1', 'node1 → name');
t.is(node1.children.length, 1, 'node1 → children');
t.deepEqual(node1.position.toArray(), [0, 2, 0], 'node1 → position');
t.deepEqual(node1.quaternion.toArray(), [0, 0, 0.707, 0.707], 'node1 → quaternion');
t.deepEqual(node1.scale.toArray(), [0.5, 0.5, 0.5], 'node1 → scale');
t.deepEqual((node1 as Mesh).morphTargetInfluences, [0], 'node1 → morphTargetInfluences');
});