Skip to content

Commit c18f6a8

Browse files
authored
Merge pull request #85 from Tresjs/feature/80-useanimation-composable-for-animationmixer
feat(cientos) useanimation composable for animationmixer
2 parents 26eaab6 + f300162 commit c18f6a8

File tree

20 files changed

+7214
-15
lines changed

20 files changed

+7214
-15
lines changed

docs/.vitepress/config.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ export default defineConfig({
6363
{ text: 'Introduction', link: '/cientos/' },
6464
{
6565
text: 'Abstractions',
66-
items: [{ text: 'Text3D', link: '/cientos/abstractions/text-3d' }],
66+
items: [
67+
{ text: 'Text3D', link: '/cientos/abstractions/text-3d' },
68+
{ text: 'useAnimations', link: '/cientos/abstractions/use-animations' },
69+
],
6770
},
6871
{
6972
text: 'Controls',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# useAnimations <Badge type="warning" text="^1.5.0" />
2+
3+
`useAnimation` is a composable that returns a `shallowReactive` with all the models actions based on the animations provided. It is a wrapper around the [AnimationMixer](https://threejs.org/docs/#api/en/animation/AnimationMixer) class.
4+
5+
<StackBlitzEmbed projectId="tresjs-use-animations" />
6+
7+
## Usage
8+
9+
```ts
10+
import { useAnimations } from '@tresjs/cientos'
11+
12+
const { scene: model, animations } = await useGLTF('/models/ugly-naked-bunny.gltf')
13+
14+
// Animations [ { name: 'Greeting'}, { name: 'Idle' } ]
15+
16+
const { actions, mixer } = useAnimations(animations, model)
17+
18+
let currentAction = actions.Greeting
19+
20+
currentAction.play()
21+
```
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AnimationAction, AnimationClip, AnimationMixer, Object3D, Scene } from 'three'
2+
import { useRenderLoop } from '@tresjs/core'
3+
import { ref, Ref, shallowReactive } from 'vue'
4+
5+
export function useAnimations<T extends AnimationClip>(
6+
animations: T[],
7+
modelRef?: Scene | Ref<Object3D | undefined | null>,
8+
) {
9+
const reference: Ref<Object3D> = ref(modelRef) as Ref<Object3D>
10+
11+
const mixer = new AnimationMixer(reference.value)
12+
13+
const actions = shallowReactive<{ [key: string]: AnimationAction }>({})
14+
15+
animations.forEach(animation => {
16+
const action = mixer.clipAction(animation, reference.value)
17+
actions[animation.name] = action
18+
})
19+
20+
const { onLoop } = useRenderLoop()
21+
22+
onLoop(({ delta }) => {
23+
mixer.update(delta)
24+
})
25+
26+
return {
27+
actions,
28+
mixer,
29+
}
30+
}

packages/cientos/src/core/useGLTF/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface GLTFLoaderOptions {
88
}
99

1010
export interface GLTFResult {
11+
animations: Array<THREE.AnimationClip>
1112
nodes: Array<TresObject>
1213
materials: Array<THREE.Material>
1314
scene: THREE.Scene

packages/cientos/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import OrbitControls from './core/OrbitControls.vue'
22
import TransformControls from './core/TransformControls.vue'
33
import { useTweakPane } from './core/useTweakPane'
4+
import { useAnimations } from './core/useAnimations'
45
import { GLTFModel } from './core/useGLTF/component'
56
import { FBXModel } from './core/useFBX/component'
67
import Text3D from './core/Text3D.vue'
@@ -9,4 +10,4 @@ import Plane from './core/Plane.vue'
910
export * from './core/useGLTF'
1011
export * from './core/useFBX'
1112
export * from './types'
12-
export { OrbitControls, TransformControls, useTweakPane, GLTFModel, FBXModel, Text3D, Plane }
13+
export { OrbitControls, TransformControls, useTweakPane, GLTFModel, FBXModel, Text3D, Plane, useAnimations }
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)