From 93753006161d65133f6773de4712865dd35949fd Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Tue, 7 Mar 2023 21:27:26 -0600 Subject: [PATCH 1/2] feat: add animations inject function --- libs/angular-three-soba/misc/src/index.ts | 1 + .../misc/src/lib/animations/animations.ts | 68 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 libs/angular-three-soba/misc/src/lib/animations/animations.ts diff --git a/libs/angular-three-soba/misc/src/index.ts b/libs/angular-three-soba/misc/src/index.ts index a35af6f..6430e42 100644 --- a/libs/angular-three-soba/misc/src/index.ts +++ b/libs/angular-three-soba/misc/src/index.ts @@ -1,3 +1,4 @@ +export * from './lib/animations/animations'; export * from './lib/bake-shadows/bake-shadows'; export * from './lib/depth-buffer/depth-buffer'; export * from './lib/fbo/fbo'; diff --git a/libs/angular-three-soba/misc/src/lib/animations/animations.ts b/libs/angular-three-soba/misc/src/lib/animations/animations.ts new file mode 100644 index 0000000..6969c0a --- /dev/null +++ b/libs/angular-three-soba/misc/src/lib/animations/animations.ts @@ -0,0 +1,68 @@ +import { injectBeforeRender, injectNgtDestroy, injectNgtRef, is, NgtInjectedRef } from 'angular-three'; +import { isObservable, Observable, Subscription } from 'rxjs'; +import { AnimationMixer } from 'three'; + +type Api = { + ref: NgtInjectedRef; + clips: THREE.AnimationClip[]; + mixer: THREE.AnimationMixer; + names: T['name'][]; + actions: { [key in T['name']]: THREE.AnimationAction | null }; +}; + +export function injectNgtsAnimations( + clips: T[], + object?: Observable | THREE.Object3D | NgtInjectedRef +): Api { + let ref = injectNgtRef(); + + let sub: Subscription; + + if (object) { + if (isObservable(object)) { + sub = object.subscribe((val) => { + ref.nativeElement = val; + }); + } else if (is.ref(object)) { + ref = object; + } else { + ref.nativeElement = object; + } + } + + const mixer = new AnimationMixer(ref.nativeElement); + + let cached = {} as { [key in T['name']]: THREE.AnimationAction | null }; + const actions = {} as { [key in T['name']]: THREE.AnimationAction | null }; + const names = [] as T['name'][]; + + for (const clip of clips) { + names.push(clip.name); + Object.defineProperty(actions, clip.name, { + enumerable: true, + get: () => { + if (ref.nativeElement) { + const name = clip.name as keyof typeof cached; + return cached[name] || (cached[name] = mixer.clipAction(clip, ref.nativeElement)); + } + }, + }); + } + + const api = { ref, clips, actions, names, mixer }; + + injectNgtDestroy(() => { + if (sub) sub.unsubscribe(); + cached = {} as { [key in T['name']]: THREE.AnimationAction | null }; + Object.values(api.actions).forEach((action) => { + if (ref.nativeElement) { + mixer.uncacheAction(action as THREE.AnimationClip, ref.nativeElement); + } + }); + mixer.stopAllAction(); + }); + + injectBeforeRender(({ delta }) => mixer.update(delta)); + + return api; +} From 1d9e4f5a640163f2d35ea2d473ab5260c7f97816 Mon Sep 17 00:00:00 2001 From: Chau Tran Date: Tue, 7 Mar 2023 21:27:57 -0600 Subject: [PATCH 2/2] chore: release 1.12.0 --- CHANGELOG.md | 7 +++++++ libs/angular-three-soba/version.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d529fc..095b6b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ +## [1.12.0](https://github.com/angular-threejs/soba/compare/1.11.1...1.12.0) (2023-03-08) + + +### Features + +* add animations inject function ([9375300](https://github.com/angular-threejs/soba/commit/93753006161d65133f6773de4712865dd35949fd)) + ### [1.11.1](https://github.com/angular-threejs/soba/compare/1.11.0...1.11.1) (2023-03-08) diff --git a/libs/angular-three-soba/version.json b/libs/angular-three-soba/version.json index 03f21f1..03afcf4 100644 --- a/libs/angular-three-soba/version.json +++ b/libs/angular-three-soba/version.json @@ -1,3 +1,3 @@ { - "version": "1.11.1" + "version": "1.12.0" }