-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathanimation.ts
More file actions
518 lines (455 loc) · 20.3 KB
/
animation.ts
File metadata and controls
518 lines (455 loc) · 20.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
import type { Nullable } from "core/types";
import type { ArcRotateCamera } from "core/Cameras/arcRotateCamera";
import type { ShadowGenerator } from "core/Lights/Shadows/shadowGenerator";
import type { Scene } from "core/scene";
import type { AnimationGroup } from "core/Animations/animationGroup";
import type { AbstractMesh } from "core/Meshes/abstractMesh";
import type { TransformNode } from "core/Meshes/transformNode";
import type { Vector3, Quaternion } from "core/Maths/math.vector";
import type { RestPoseDataUpdate } from "./avatarManager";
import { Color3 } from "core/Maths/math.color";
import { Matrix } from "core/Maths/math.vector";
import { StandardMaterial } from "core/Materials/standardMaterial";
import { MeshBuilder } from "core/Meshes/meshBuilder";
import { Mesh } from "core/Meshes/mesh";
import { Observable } from "core/Misc/observable";
import { GizmoManager } from "core/Gizmos/gizmoManager";
import { GizmoCoordinatesMode } from "core/Gizmos/gizmo";
import { UtilityLayerRenderer } from "core/Rendering/utilityLayerRenderer";
import { SkeletonViewer } from "core/Debug/skeletonViewer";
import { AppendSceneAsync } from "core/Loading/sceneLoader";
import { CreateSkeletonFromTransformNodeHierarchy } from "core/Bones/skeleton.functions";
// Side-effect import needed for scene.createPickingRay (prototype-augmented)
import "core/Culling/ray";
import { DistancePointToLine } from "./helperFunctions";
import type { GizmoType } from "./avatar";
const ShadowLayerMask = 0x20000000;
type TransformNodeTransformations = Map<TransformNode, { position: Vector3; scaling: Vector3; quaternion: Quaternion }>;
/**
* Manages the right-side animation source viewport.
* Mirrors the original animation.ts from the playground (no AnimatorAvatar — uses TransformNodes directly).
*/
export class AnimationSource {
private _animationGroup: Nullable<AnimationGroup> = null;
private _skeletonViewer: Nullable<SkeletonViewer> = null;
private _skeletonMesh: Nullable<Mesh> = null;
private _rootNode: Nullable<AbstractMesh> = null;
private _gizmoManager: GizmoManager;
private _gizmoSelectedNode: Nullable<Mesh> = null;
private _selectedTransformNode: Nullable<TransformNode> = null;
private _inRestPose = false;
private _initialTransformNodeTransformations: TransformNodeTransformations = new Map();
private _restPoseTransformNodeTransformations: TransformNodeTransformations = new Map();
private _retargetedTransformNodeTransformations: TransformNodeTransformations = new Map();
public readonly onLoadedObservable = new Observable<void>();
public readonly onGizmoNodeSelectedObservable = new Observable<string>();
public readonly onPlayingObservable = new Observable<boolean>();
public get animationGroup(): Nullable<AnimationGroup> {
return this._animationGroup;
}
public get isLoaded(): boolean {
return this._animationGroup !== null;
}
/** True when the animation is actively playing (not in rest pose). */
public get isPlaying(): boolean {
return this._animationGroup !== null && !this._inRestPose;
}
public get selectedTransformNode(): Nullable<TransformNode> {
return this._selectedTransformNode;
}
public constructor(
private readonly _scene: Scene,
private readonly _camera: ArcRotateCamera,
private readonly _shadowGenerator: ShadowGenerator
) {
this._gizmoManager = new GizmoManager(this._scene, undefined, new UtilityLayerRenderer(this._scene), new UtilityLayerRenderer(this._scene));
this._gizmoManager.keepDepthUtilityLayer.setRenderCamera(this._camera);
this._gizmoManager.utilityLayer.setRenderCamera(this._camera);
this._gizmoManager.usePointerToAttachGizmos = false;
this._gizmoManager.coordinatesMode = GizmoCoordinatesMode.Local;
}
public async loadAsync(path: string, restPoseUpdate?: RestPoseDataUpdate, skeletonRootNodeName?: string): Promise<void> {
this._cleanScene();
await this._loadFileAsync(path, restPoseUpdate, skeletonRootNodeName);
}
public clearScene(): void {
this._cleanScene();
}
/** Returns a sorted list of transform node names targeted by the animation group. */
public getTransformNodeNames(): string[] {
if (!this._animationGroup || !this._rootNode) {
return [];
}
const listUnsorted = new Set<string>();
const listSorted: string[] = [];
const listAllSorted: string[] = [];
for (const ta of this._animationGroup.targetedAnimations) {
if ((ta.target as TransformNode).getClassName?.() === "TransformNode") {
listUnsorted.add((ta.target as TransformNode).name);
}
}
this._rootNode.getChildTransformNodes(false).forEach((node) => {
if (node.getClassName() === "TransformNode") {
listAllSorted.push(node.name);
}
});
for (const name of listAllSorted) {
if (listUnsorted.has(name)) {
listSorted.push(name);
}
}
return listSorted;
}
/** Returns transform node options in hierarchy order, with indented labels for parenting. */
public getTransformNodeOptions(): { label: string; value: string }[] {
if (!this._animationGroup || !this._rootNode) {
return [];
}
const allNames = new Set(this.getTransformNodeNames());
// getChildTransformNodes(false) traverses in DFS order (parent before children)
const result: { label: string; value: string }[] = [];
this._rootNode.getChildTransformNodes(false).forEach((node) => {
if (!allNames.has(node.name)) {
return;
}
// Compute depth relative to _rootNode
let depth = 0;
let p = node.parent;
while (p && p !== this._rootNode) {
depth++;
p = p.parent;
}
const indent = "\u00A0\u00A0".repeat(depth);
result.push({ label: indent + node.name, value: node.name });
});
return result;
}
/** Called just before retargeting; saves the current rest-pose state. */
public prepareRetargeting(): void {
if (this._inRestPose) {
this._restPoseTransformNodeTransformations.clear();
this._saveTransformNodes(this._restPoseTransformNodeTransformations);
} else {
this._restoreTransformNodes(this._restPoseTransformNodeTransformations);
}
this._retargetedTransformNodeTransformations.clear();
this._saveTransformNodes();
}
/** Builds the rest-pose export data for the playground code generator. */
public buildExportData(restPoseUpdate?: RestPoseDataUpdate): RestPoseDataUpdate {
return restPoseUpdate ? [...restPoseUpdate] : [];
}
public play(speed: number): void {
if (!this._animationGroup) {
return;
}
this._animationGroup.stop();
this._animationGroup.start(true, speed);
this._inRestPose = false;
this.onPlayingObservable.notifyObservers(true);
}
public setAnimSpeed(speed: number): void {
if (this._animationGroup) {
this._animationGroup.speedRatio = speed;
}
}
public returnToRest(): void {
if (!this._animationGroup) {
return;
}
this._animationGroup.stop();
this._restoreTransformNodes(this._restPoseTransformNodeTransformations);
this._inRestPose = true;
this.onPlayingObservable.notifyObservers(false);
}
/**
* Captures the current transform node transformations (while in rest pose, possibly edited via gizmo)
* as a `RestPoseDataUpdate`. All transform node transformations are saved as absolute values.
*/
public saveAsRestPose(): RestPoseDataUpdate {
const result: RestPoseDataUpdate = [];
if (!this._rootNode) {
return result;
}
this._rootNode.getChildTransformNodes(false).forEach((node) => {
if (!node.rotationQuaternion) {
return;
}
const data: { position?: number[]; scaling?: number[]; quaternion?: number[] } = {};
data.position = node.position.asArray();
data.scaling = node.scaling.asArray();
data.quaternion = node.rotationQuaternion.asArray();
result.push({ name: node.name, data });
});
// Update internal rest pose baseline
this._restPoseTransformNodeTransformations.clear();
this._saveTransformNodes(this._restPoseTransformNodeTransformations);
return result;
}
public setSkeletonVisible(visible: boolean): void {
this._skeletonShow(visible);
}
public setSkeletonLocalAxes(visible: boolean): void {
this._skeletonShowLocalAxes(visible);
}
public setGizmo(enabled: boolean, type: GizmoType): void {
this._updateGizmoEnabled(enabled, type);
}
/** Attaches the gizmo to the transform node with the given name (called when user picks from the dropdown). */
public attachGizmoToTransformNode(nodeName: string): void {
if (!this._rootNode) {
return;
}
const node = this._rootNode.getChildTransformNodes(false, (n) => n.name === nodeName)[0];
if (!node) {
return;
}
if (this._gizmoSelectedNode) {
this._gizmoSelectedNode.position.copyFrom(node.absolutePosition);
}
this._selectedTransformNode = node;
this._gizmoManager.attachToNode(node);
}
public handleBoneClick(x: number, y: number): void {
if (!this._rootNode) {
return;
}
const ray = this._scene.createPickingRay(x, y, Matrix.Identity(), this._camera, false);
let selectedNode: Nullable<TransformNode> = null;
let minDistance = Number.POSITIVE_INFINITY;
this._rootNode.getChildTransformNodes(false).forEach((node) => {
const d = DistancePointToLine(node.absolutePosition, ray.origin, ray.direction);
if (d < minDistance) {
minDistance = d;
selectedNode = node;
if (this._gizmoSelectedNode) {
this._gizmoSelectedNode.position.copyFrom(node.absolutePosition);
}
}
});
if (selectedNode) {
this._selectedTransformNode = selectedNode as TransformNode;
this._gizmoManager.attachToNode(selectedNode as TransformNode);
this.onGizmoNodeSelectedObservable.notifyObservers((selectedNode as TransformNode).name);
}
}
public dispose(): void {
this._cleanScene();
this._gizmoSelectedNode?.dispose();
this._gizmoSelectedNode = null;
this._gizmoManager.dispose();
}
private async _loadFileAsync(path: string, restPoseUpdate?: RestPoseDataUpdate, skeletonRootNodeName?: string): Promise<void> {
if (path.startsWith("file:")) {
await AppendSceneAsync(path.substring(5), this._scene, { rootUrl: "file:" });
} else {
await AppendSceneAsync(path, this._scene);
}
this._rootNode = this._scene.getMeshByName("__root__");
if (!this._rootNode) {
return;
}
this._rootNode.name = "reference";
// Keep only our 2 cameras; remove any cameras loaded from the file
while (this._scene.cameras.length > 2) {
this._scene.cameras[2].dispose();
}
// Keep only the first animation group (not the "avatar" retargeted one)
this._animationGroup = null;
const lstAnimDelete = new Set<AnimationGroup>();
for (const animGroup of this._scene.animationGroups) {
if (animGroup.name !== "avatar") {
if (!this._animationGroup) {
this._animationGroup = animGroup;
} else {
lstAnimDelete.add(animGroup);
}
}
}
lstAnimDelete.forEach((anim) => anim.dispose());
if (!this._animationGroup) {
return;
}
this._animationGroup.name = "reference";
// Delete all skeletons loaded from the animation file (we don't need them)
const skeletonsDelete = new Set<import("core/Bones/skeleton").Skeleton>();
for (const skeleton of this._scene.skeletons) {
if (skeleton.name !== "avatar") {
skeletonsDelete.add(skeleton);
}
}
skeletonsDelete.forEach((skeleton) => skeleton.dispose());
// Delete all meshes under the root node (animation files only need transform nodes)
this._rootNode
.getChildren((node) => node instanceof Mesh, false)
.forEach((mesh) => {
(mesh as Mesh).dispose();
});
// Use the user-selected root node, or fall back to hips heuristic
let skeletonRoot: TransformNode = this._rootNode;
if (skeletonRootNodeName) {
const found = this._rootNode.getChildTransformNodes(false, (node) => node.name === skeletonRootNodeName)[0];
if (found) {
skeletonRoot = found;
}
} else {
// Legacy heuristic: find hips/pelvis node
let hipsNode: Nullable<TransformNode> = this._rootNode.getChildTransformNodes(false, (node) => node.name === "mixamorig:Hips")?.[0] ?? null;
if (!hipsNode) {
hipsNode = this._rootNode.getChildTransformNodes(false, (node) => node.name === "Hips")[0] ?? null;
}
if (!hipsNode) {
hipsNode = this._rootNode.getChildTransformNodes(false, (node) => node.name.toLowerCase().indexOf("hips") >= 0)[0] ?? null;
}
if (!hipsNode) {
hipsNode = this._rootNode.getChildTransformNodes(false, (node) => node.name.toLowerCase().indexOf("pelvis") >= 0)[0] ?? null;
}
if (hipsNode) {
skeletonRoot = hipsNode;
}
}
// Create a skeleton + visualization mesh from the transform node hierarchy
const meshOptions: { name?: string; boneMeshSize?: number; createMesh?: boolean; mesh?: Mesh } = {
name: "reference",
boneMeshSize: 0.0001,
createMesh: true,
};
const animSkeleton = CreateSkeletonFromTransformNodeHierarchy(skeletonRoot, this._scene, meshOptions);
const meshAnim = meshOptions.mesh!;
this._skeletonMesh = meshAnim;
meshAnim.layerMask = ShadowLayerMask;
meshAnim.isVisible = false;
const initialNode = skeletonRoot;
this._selectedTransformNode = initialNode;
this._gizmoManager.attachToNode(initialNode);
this.onGizmoNodeSelectedObservable.notifyObservers(initialNode.name);
const meshAnimExtendSize = meshAnim.getBoundingInfo().boundingBox.extendSizeWorld;
const skeletonViewer = new SkeletonViewer(animSkeleton, meshAnim, this._scene, undefined, 0, {
displayMode: SkeletonViewer.DISPLAY_SPHERE_AND_SPURS,
displayOptions: {
showLocalAxes: false,
localAxesSize: Math.max(meshAnimExtendSize.x, meshAnimExtendSize.y, meshAnimExtendSize.z) * 5,
},
});
this._skeletonViewer = skeletonViewer;
skeletonViewer.isEnabled = false;
skeletonViewer.utilityLayer!.setRenderCamera(this._camera);
this._skeletonDebugMeshSet();
const utilScene = skeletonViewer.utilityLayer!.utilityLayerScene!;
const mat = new StandardMaterial("selectedBoneMatAnim", utilScene);
mat.emissiveColor = Color3.Red();
mat.disableLighting = true;
this._gizmoSelectedNode?.dispose();
this._gizmoSelectedNode = MeshBuilder.CreateSphere("selectedTransformNode", { diameter: 0.07, segments: 10 }, utilScene);
this._gizmoSelectedNode.setEnabled(false);
this._gizmoSelectedNode.material = mat;
this._gizmoSelectedNode.layerMask = ShadowLayerMask;
this._gizmoSelectedNode.renderingGroupId = 3;
this._gizmoSelectedNode.position.copyFrom(initialNode.absolutePosition);
if (restPoseUpdate && restPoseUpdate.length > 0) {
this._applyRestPoseUpdate(restPoseUpdate);
}
this._initialTransformNodeTransformations.clear();
this._restPoseTransformNodeTransformations.clear();
this._saveTransformNodes(this._initialTransformNodeTransformations);
this._saveTransformNodes(this._restPoseTransformNodeTransformations);
this._animationGroup.speedRatio = 1;
this._inRestPose = false;
this.onLoadedObservable.notifyObservers();
}
private _skeletonShow(visible: boolean): void {
if (!this._skeletonViewer) {
return;
}
this._skeletonViewer.isEnabled = visible;
this._skeletonDebugMeshSet();
}
private _skeletonShowLocalAxes(visible: boolean): void {
if (!this._skeletonViewer) {
return;
}
this._skeletonViewer.changeDisplayOptions("showLocalAxes", visible);
this._skeletonDebugMeshSet();
}
private _gizmoShow(show: boolean): void {
if (this._gizmoSelectedNode) {
this._gizmoSelectedNode.setEnabled(show);
}
}
private _updateGizmoEnabled(enabled: boolean, type: GizmoType): void {
this._gizmoManager.positionGizmoEnabled = enabled && type === "Position";
this._gizmoManager.rotationGizmoEnabled = enabled && type === "Rotation";
this._gizmoManager.scaleGizmoEnabled = enabled && type === "Scale";
this._gizmoShow(enabled);
}
private _skeletonDebugMeshSet(): void {
if (!this._skeletonViewer?.debugMesh) {
return;
}
this._skeletonViewer.debugMesh.layerMask = ShadowLayerMask;
this._skeletonViewer.debugMesh.alwaysSelectAsActiveMesh = true;
this._shadowGenerator.addShadowCaster(this._skeletonViewer.debugMesh);
if (this._skeletonViewer.debugLocalAxesMesh) {
this._skeletonViewer.debugLocalAxesMesh.layerMask = ShadowLayerMask;
this._skeletonViewer.debugLocalAxesMesh.alwaysSelectAsActiveMesh = true;
}
}
private _saveTransformNodes(map?: TransformNodeTransformations): void {
if (!this._rootNode) {
return;
}
map = map ?? this._retargetedTransformNodeTransformations;
this._rootNode.getChildTransformNodes(false).forEach((node) => {
if (!node.rotationQuaternion) {
return;
}
map!.set(node, {
position: node.position.clone(),
scaling: node.scaling.clone(),
quaternion: node.rotationQuaternion.clone(),
});
});
}
private _restoreTransformNodes(map?: TransformNodeTransformations): void {
map = map ?? this._retargetedTransformNodeTransformations;
map.forEach((transformation, tn) => {
tn.position = transformation.position.clone();
tn.scaling = transformation.scaling.clone();
tn.rotationQuaternion = transformation.quaternion.clone();
tn.computeWorldMatrix(true);
});
}
private _applyRestPoseUpdate(restPoseUpdate: RestPoseDataUpdate): void {
if (!this._rootNode) {
return;
}
for (const dataBlock of restPoseUpdate) {
const node = this._rootNode.getChildTransformNodes(false, (node) => node.name === dataBlock.name)[0];
if (node) {
if (dataBlock.data.position) {
node.position.fromArray(dataBlock.data.position);
}
if (dataBlock.data.scaling) {
node.scaling.fromArray(dataBlock.data.scaling);
}
if (dataBlock.data.quaternion) {
node.rotationQuaternion!.fromArray(dataBlock.data.quaternion);
}
}
}
}
private _cleanScene(): void {
this._animationGroup?.dispose();
if (this._skeletonViewer) {
this._skeletonViewer.skeleton.dispose();
this._skeletonViewer.dispose();
this._skeletonViewer = null;
}
this._skeletonMesh?.dispose();
this._skeletonMesh = null;
this._rootNode?.dispose(false, true);
this._animationGroup = null;
this._rootNode = null;
this._selectedTransformNode = null;
}
}