Skip to content

Commit 3cb63a5

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
fix(soba): adjust ngtsAnimations
1 parent 51b5062 commit 3cb63a5

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

libs/soba/misc/src/animations/animations.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as THREE from 'three';
44

55
export function injectNgtsAnimations(
66
animationsFactory: () => THREE.AnimationClip[],
7-
{ ref, injector }: { ref?: NgtInjectedRef<THREE.Object3D> | THREE.Object3D; injector?: Injector }
7+
{ ref, injector }: { ref?: NgtInjectedRef<THREE.Object3D> | THREE.Object3D; injector?: Injector } = {}
88
) {
99
injector = assertInjectionContext(injectNgtsAnimations, injector);
1010
return runInInjectionContext(injector, () => {
@@ -38,32 +38,28 @@ export function injectNgtsAnimations(
3838

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

41-
requestAnimationFrame(() => {
42-
effect(
43-
() => {
44-
const actual = actualRef.nativeElement;
45-
const animations = animationsFactory();
41+
effect(() => {
42+
const actual = actualRef.nativeElement;
43+
if (!actual) return;
44+
const animations = animationsFactory();
4645

47-
for (let i = 0; i < animations.length; i++) {
48-
const clip = animations[i];
46+
for (let i = 0; i < animations.length; i++) {
47+
const clip = animations[i];
4948

50-
names.push(clip.name);
51-
clips.push(clip);
49+
names.push(clip.name);
50+
clips.push(clip);
5251

53-
Object.defineProperty(actions, clip.name, {
54-
enumerable: true,
55-
get: () => {
56-
return cached[clip.name] || (cached[clip.name] = mixer.clipAction(clip, actual));
57-
},
58-
});
52+
Object.defineProperty(actions, clip.name, {
53+
enumerable: true,
54+
get: () => {
55+
return cached[clip.name] || (cached[clip.name] = mixer.clipAction(clip, actual));
56+
},
57+
});
5958

60-
if (i === 0) {
61-
actions[clip.name].play();
62-
}
63-
}
64-
},
65-
{ injector }
66-
);
59+
if (i === 0) {
60+
actions[clip.name].play();
61+
}
62+
}
6763
});
6864

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

libs/soba/src/abstractions/gizmo-helper.stories.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from 'angular-three-soba/abstractions';
1111
import { NgtsOrbitControls } from 'angular-three-soba/controls';
1212
import { injectNgtsGLTFLoader } from 'angular-three-soba/loaders';
13+
import { injectNgtsAnimations } from 'angular-three-soba/misc';
1314
import { makeStoryObject, StorybookSetup } from '../setup-canvas';
1415

1516
const alignment = [
@@ -49,7 +50,7 @@ const args = {
4950
@Component({
5051
standalone: true,
5152
template: `
52-
<ngt-primitive *args="[model()]" [scale]="0.01" />
53+
<ngt-primitive *args="[model()]" [scale]="0.01" [ref]="animations.ref" />
5354
<ngts-gizmo-helper [alignment]="alignment" [margin]="[marginX, marginY]">
5455
<ng-template ngtsGizmoHelperContent>
5556
<ngts-gizmo-viewcube
@@ -85,11 +86,10 @@ const args = {
8586
})
8687
class DefaultGizmoHelperStory {
8788
readonly #gltf = injectNgtsGLTFLoader(() => 'soba/assets/LittlestTokyo.glb');
88-
readonly model = computed(() => {
89-
const gltf = this.#gltf();
90-
if (!gltf) return null;
91-
return gltf.scene;
92-
});
89+
90+
readonly model = computed(() => this.#gltf()?.scene || null);
91+
readonly animations = injectNgtsAnimations(() => this.#gltf()?.animations || []);
92+
9393
@Input() alignment = args.alignment;
9494
@Input() marginX = args.marginX;
9595
@Input() marginY = args.marginY;

0 commit comments

Comments
 (0)