Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 1x | import macro from 'vtk.js/Sources/macros'; import * as vtkMath from 'vtk.js/Sources/Common/Core/Math'; import { quat, vec3 } from 'gl-matrix'; const { vtkDebugMacro, vtkWarningMacro } = macro; /** * Create an animation channel * @param {glTFChannel} glTFChannel * @param {glTFChannel[]} glTFSamplers * @returns */ function createAnimationChannel(glTFChannel, glTFSamplers) { const path = glTFChannel.target.path; const node = glTFChannel.target.node; function applyAnimation(value) { let axisAngle; let w; let nq; switch (path) { case 'translation': node.setPosition(value[0], value[1], value[2]); break; case 'rotation': // Convert quaternion to axis-angle representation nq = quat.normalize(quat.create(), value); axisAngle = new Float64Array(3); w = quat.getAxisAngle(axisAngle, nq); // Apply rotation using rotateWXYZ node.rotateWXYZ( vtkMath.degreesFromRadians(w), axisAngle[0], axisAngle[1], axisAngle[2] ); break; case 'scale': node.setScale(value[0], value[1], value[2]); break; default: vtkWarningMacro(`Unsupported animation path: ${path}`); } } function animate(currentTime) { const sampler = glTFSamplers[glTFChannel.sampler]; const value = sampler.evaluate(currentTime, path); applyAnimation(value); } return { ...glTFChannel, animate }; } /** * Create an animation sampler * @param {glTFSampler} glTFSampler * @returns */ function createAnimationSampler(glTFSampler) { let lastKeyframeIndex = 0; function findKeyframes(time) { let i1 = lastKeyframeIndex; while (i1 < glTFSampler.input.length - 1 && glTFSampler.input[i1] <= time) { i1++; } const i0 = Math.max(0, i1 - 1); lastKeyframeIndex = i0; return [glTFSampler.input[i0], glTFSampler.input[i1], i0, i1]; } function stepInterpolate(path, i0) { const startIndex = i0 * 3; const v0 = new Array(3); for (let i = 0; i < 3; ++i) { v0[i] = glTFSampler.output[startIndex + i]; } return v0; } function linearInterpolate(path, t0, t1, i0, i1, t) { const ratio = (t - t0) / (t1 - t0); const startIndex = i0 * 4; const endIndex = i1 * 4; const v0 = new Array(4); const v1 = new Array(4); for (let i = 0; i < 4; ++i) { v0[i] = glTFSampler.output[startIndex + i]; v1[i] = glTFSampler.output[endIndex + i]; } switch (path) { case 'translation': case 'scale': return vec3.lerp(vec3.create(), v0, v1, ratio); case 'rotation': return quat.slerp(quat.create(), v0, v1, ratio); default: vtkWarningMacro(`Unsupported animation path: ${path}`); return null; } } function cubicSplineInterpolate(path, t0, t1, i0, i1, time) { const dt = t1 - t0; const t = (time - t0) / dt; const t2 = t * t; const t3 = t2 * t; const p0 = glTFSampler.output[i0 * 3 + 1]; const m0 = dt * glTFSampler.output[i0 * 3 + 2]; const p1 = glTFSampler.output[i1 * 3 + 1]; const m1 = dt * glTFSampler.output[i1 * 3]; if (Array.isArray(p0)) { return p0.map((v, j) => { const a = 2 * t3 - 3 * t2 + 1; const b = t3 - 2 * t2 + t; const c = -2 * t3 + 3 * t2; const d = t3 - t2; return a * v + b * m0[j] + c * p1[j] + d * m1[j]; }); } const a = 2 * t3 - 3 * t2 + 1; const b = t3 - 2 * t2 + t; const c = -2 * t3 + 3 * t2; const d = t3 - t2; return a * p0 + b * m0 + c * p1 + d * m1; } function evaluate(time, path) { const [t0, t1, i0, i1] = findKeyframes(time); let result; switch (glTFSampler.interpolation) { case 'STEP': result = stepInterpolate(path, i0); break; case 'LINEAR': result = linearInterpolate(path, t0, t1, i0, i1, time); break; case 'CUBICSPLINE': result = cubicSplineInterpolate(path, t0, t1, i0, i1, time); break; default: throw new Error( `Unknown interpolation method: ${glTFSampler.interpolation}` ); } return result; } return { ...glTFSampler, evaluate }; } /** * Create an animation * @param {glTFAnimation} glTFAnimation * @param {Map} nodes * @returns */ function createAnimation(glTFAnimation, nodes) { glTFAnimation.samplers = glTFAnimation.samplers.map((sampler) => createAnimationSampler(sampler) ); glTFAnimation.channels = glTFAnimation.channels.map((channel) => { channel.target.node = nodes.get(`node-${channel.target.node}`); return createAnimationChannel(channel, glTFAnimation.samplers); }); function update(currentTime) { glTFAnimation.channels.forEach((channel) => channel.animate(currentTime)); } return { ...glTFAnimation, update }; } /** * Create an animation mixer * @param {Map} nodes * @param {*} accessors * @returns */ function createAnimationMixer(nodes, accessors) { const animations = new Map(); const activeAnimations = new Map(); function addAnimation(glTFAnimation) { const annimation = createAnimation(glTFAnimation, nodes, accessors); animations.set(glTFAnimation.id, annimation); vtkDebugMacro(`Animation "${glTFAnimation.id}" added to mixer`); } function play(name, weight = 1) { if (!animations.has(name)) { vtkWarningMacro(`Animation "${name}" not found in mixer`); return; } activeAnimations.set(name, { animation: animations.get(name), weight, time: 0, }); vtkDebugMacro(`Playing animation "${name}" with weight ${weight}`); } function stop(name) { if (activeAnimations.delete(name)) { vtkWarningMacro(`Stopped animation "${name}"`); } else { vtkWarningMacro(`Animation "${name}" was not playing`); } } function stopAll() { activeAnimations.clear(); vtkWarningMacro('Stopped all animations'); } function update(deltaTime) { // Normalize weights const totalWeight = Array.from(activeAnimations.values()).reduce( (sum, { weight }) => sum + weight, 0 ); activeAnimations.forEach(({ animation, weight, time }, name) => { const normalizedWeight = totalWeight > 0 ? weight / totalWeight : 0; const newTime = time + deltaTime; activeAnimations.set(name, { animation, weight, time: newTime }); vtkDebugMacro( `Updating animation "${name}" at time ${newTime.toFixed( 3 )} with normalized weight ${normalizedWeight.toFixed(3)}` ); animation.update(newTime, normalizedWeight); }); } return { addAnimation, play, stop, stopAll, update }; } export { createAnimation, createAnimationChannel, createAnimationMixer, createAnimationSampler, }; |