Skip to content

Commit

Permalink
fix: adjust inject animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Chau Tran authored and Chau Tran committed Mar 31, 2023
1 parent 46f23c7 commit 66bd4ef
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions libs/angular-three-soba/misc/src/lib/animations/animations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { injectBeforeRender, injectNgtDestroy, injectNgtRef, NgtInjectedRef } from 'angular-three';
import { Observable, takeUntil } from 'rxjs';
import { map, Observable, switchMap, takeUntil } from 'rxjs';
import * as THREE from 'three';
import { GLTF } from 'three-stdlib';
import { AnimationClip } from 'three';

type Api = {
ref: NgtInjectedRef<THREE.Object3D>;
Expand All @@ -12,10 +12,19 @@ type Api = {
};

export function injectNgtsAnimations(
gltf$: Observable<GLTF>,
modelSelector: (gltf: GLTF) => THREE.Object3D = (gltf) => gltf.scene
animations$: Observable<AnimationClip[]>,
ref?: NgtInjectedRef<THREE.Object3D> | THREE.Object3D
): Api {
const ref = injectNgtRef<THREE.Object3D>();
let actualRef = injectNgtRef<THREE.Object3D>();

if (ref) {
if (ref instanceof THREE.Object3D) {
actualRef.nativeElement = ref;
} else {
actualRef = ref;
}
}

const mixer = new THREE.AnimationMixer(null!);
const actions = {} as Record<string, THREE.AnimationAction>;
let cached = {} as Record<string, THREE.AnimationAction>;
Expand All @@ -28,8 +37,8 @@ export function injectNgtsAnimations(
cached = {};
// uncache actions
Object.values(actions).forEach((action) => {
if (ref.nativeElement) {
mixer.uncacheAction(action as unknown as THREE.AnimationClip, ref.nativeElement);
if (actualRef.nativeElement) {
mixer.uncacheAction(action as unknown as THREE.AnimationClip, actualRef.nativeElement);
}
});
// stop all actions
Expand All @@ -38,28 +47,27 @@ export function injectNgtsAnimations(

injectBeforeRender(({ delta }) => mixer.update(delta));

gltf$.pipe(takeUntil(destroy$)).subscribe((gltf) => {
const model = modelSelector(gltf);
ref.nativeElement = model;

for (let i = 0; i < gltf.animations.length; i++) {
const clip = gltf.animations[i];
actualRef.$.pipe(takeUntil(destroy$))
.pipe(switchMap((object) => animations$.pipe(map((animations) => [object, animations] as const))))
.subscribe(([object, animations]) => {
for (let i = 0; i < animations.length; i++) {
const clip = animations[i];

names.push(clip.name);
clips.push(clip);
names.push(clip.name);
clips.push(clip);

Object.defineProperty(actions, clip.name, {
enumerable: true,
get: () => {
return cached[clip.name] || (cached[clip.name] = mixer.clipAction(clip, model));
},
});
Object.defineProperty(actions, clip.name, {
enumerable: true,
get: () => {
return cached[clip.name] || (cached[clip.name] = mixer.clipAction(clip, object));
},
});

if (i === 0) {
actions[clip.name].play();
if (i === 0) {
actions[clip.name].play();
}
}
}
});
});

return { ref, actions, mixer, names, clips };
return { ref: actualRef, actions, mixer, names, clips };
}

0 comments on commit 66bd4ef

Please sign in to comment.