|
| 1 | +import { quat, vec3 } from 'wgpu-matrix'; |
| 2 | +import type { Animation, Quat, Vec3 } from './types.ts'; |
| 3 | + |
| 4 | +export interface NodeTransform { |
| 5 | + translation: Vec3; |
| 6 | + hasTranslation: boolean; |
| 7 | + rotation: Quat; |
| 8 | + hasRotation: boolean; |
| 9 | + scale: Vec3; |
| 10 | + hasScale: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export type NodeTransformState = NodeTransform[]; |
| 14 | + |
| 15 | +export function createNodeTransformState(nodeCount: number): NodeTransformState { |
| 16 | + return Array.from({ length: nodeCount }, () => ({ |
| 17 | + translation: [0, 0, 0], |
| 18 | + hasTranslation: false, |
| 19 | + rotation: [0, 0, 0, 1], |
| 20 | + hasRotation: false, |
| 21 | + scale: [1, 1, 1], |
| 22 | + hasScale: false, |
| 23 | + })); |
| 24 | +} |
| 25 | + |
| 26 | +export function sampleAnimationInto( |
| 27 | + animation: Animation | undefined, |
| 28 | + time: number, |
| 29 | + transforms: NodeTransformState, |
| 30 | + touchedNodes: number[], |
| 31 | +): NodeTransformState { |
| 32 | + for (const nodeIndex of touchedNodes) { |
| 33 | + const transform = transforms[nodeIndex]; |
| 34 | + transform.hasTranslation = false; |
| 35 | + transform.hasRotation = false; |
| 36 | + transform.hasScale = false; |
| 37 | + } |
| 38 | + touchedNodes.length = 0; |
| 39 | + |
| 40 | + if (!animation || animation.duration <= 0) { |
| 41 | + return transforms; |
| 42 | + } |
| 43 | + |
| 44 | + const loopedTime = time % animation.duration; |
| 45 | + |
| 46 | + for (const channel of animation.channels) { |
| 47 | + const { input: times, output: values } = animation.samplers[channel.samplerIndex]; |
| 48 | + const components = channel.targetPath === 'rotation' ? 4 : 3; |
| 49 | + |
| 50 | + let keyframeIndex = 0; |
| 51 | + while (keyframeIndex < times.length - 2 && loopedTime >= times[keyframeIndex + 1]) { |
| 52 | + keyframeIndex++; |
| 53 | + } |
| 54 | + |
| 55 | + const startTime = times[keyframeIndex]; |
| 56 | + const endTime = times[keyframeIndex + 1]; |
| 57 | + const alpha = |
| 58 | + endTime > startTime |
| 59 | + ? Math.max(0, Math.min(1, (loopedTime - startTime) / (endTime - startTime))) |
| 60 | + : 0; |
| 61 | + |
| 62 | + const start = keyframeIndex * components; |
| 63 | + const end = (keyframeIndex + 1) * components; |
| 64 | + |
| 65 | + const transform = transforms[channel.targetNode]; |
| 66 | + if (!transform.hasTranslation && !transform.hasRotation && !transform.hasScale) { |
| 67 | + touchedNodes.push(channel.targetNode); |
| 68 | + } |
| 69 | + |
| 70 | + if (channel.targetPath === 'rotation') { |
| 71 | + quat.slerp( |
| 72 | + values.subarray(start, start + components), |
| 73 | + values.subarray(end, end + components), |
| 74 | + alpha, |
| 75 | + transform.rotation, |
| 76 | + ); |
| 77 | + transform.hasRotation = true; |
| 78 | + } else if (channel.targetPath === 'translation') { |
| 79 | + vec3.lerp( |
| 80 | + values.subarray(start, start + components), |
| 81 | + values.subarray(end, end + components), |
| 82 | + alpha, |
| 83 | + transform.translation, |
| 84 | + ); |
| 85 | + transform.hasTranslation = true; |
| 86 | + } else { |
| 87 | + vec3.lerp( |
| 88 | + values.subarray(start, start + components), |
| 89 | + values.subarray(end, end + components), |
| 90 | + alpha, |
| 91 | + transform.scale, |
| 92 | + ); |
| 93 | + transform.hasScale = true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return transforms; |
| 98 | +} |
0 commit comments