-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
a5afe24
5393ca2
ec4b92e
df39d43
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -27,10 +28,46 @@ export class PrimitiveSubject extends Subject<PrimitiveDef, MeshLike> { | |
protected attributes = new RefMapObserver<AccessorDef, BufferAttribute>('attributes', this._documentView); | ||
|
||
constructor(documentView: DocumentViewSubjectAPI, def: PrimitiveDef) { | ||
// Add Morph Targets | ||
const geometry = new BufferGeometry(); | ||
|
||
// 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)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
// 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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At some point, it will probably be necessary to create a 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @donmccurdy For now, I've moved the code to |
||
|
||
super( | ||
documentView, | ||
def, | ||
PrimitiveSubject.createValue(def, new BufferGeometry(), DEFAULT_MATERIAL, documentView.primitivePool), | ||
PrimitiveSubject.createValue(def, geometry, DEFAULT_MATERIAL, documentView.primitivePool), | ||
documentView.primitivePool, | ||
); | ||
|
||
|
There was a problem hiding this comment.
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 — anyTHREE.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. :)There was a problem hiding this comment.
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 (withWeights
target path) to work properly. WhenTHREE.JS
applies the AnimationClip, it searches for themorphTargetInfluences
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 theNodeSubject.test.ts
.