reze-engine 0.3.12 → 0.3.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/README.md +66 -66
  2. package/dist/bezier-interpolate.d.ts +15 -0
  3. package/dist/bezier-interpolate.d.ts.map +1 -0
  4. package/dist/bezier-interpolate.js +40 -0
  5. package/dist/engine.d.ts +3 -8
  6. package/dist/engine.d.ts.map +1 -1
  7. package/dist/engine.js +19 -30
  8. package/dist/engine_ts.d.ts +143 -0
  9. package/dist/engine_ts.d.ts.map +1 -0
  10. package/dist/engine_ts.js +1575 -0
  11. package/dist/ik.d.ts +32 -0
  12. package/dist/ik.d.ts.map +1 -0
  13. package/dist/ik.js +337 -0
  14. package/dist/player.d.ts +64 -0
  15. package/dist/player.d.ts.map +1 -0
  16. package/dist/player.js +220 -0
  17. package/dist/pool-scene.d.ts +52 -0
  18. package/dist/pool-scene.d.ts.map +1 -0
  19. package/dist/pool-scene.js +1122 -0
  20. package/dist/pool.d.ts +38 -0
  21. package/dist/pool.d.ts.map +1 -0
  22. package/dist/pool.js +422 -0
  23. package/dist/rzm-converter.d.ts +12 -0
  24. package/dist/rzm-converter.d.ts.map +1 -0
  25. package/dist/rzm-converter.js +40 -0
  26. package/dist/rzm-loader.d.ts +24 -0
  27. package/dist/rzm-loader.d.ts.map +1 -0
  28. package/dist/rzm-loader.js +488 -0
  29. package/dist/rzm-writer.d.ts +27 -0
  30. package/dist/rzm-writer.d.ts.map +1 -0
  31. package/dist/rzm-writer.js +701 -0
  32. package/package.json +1 -1
  33. package/src/camera.ts +358 -358
  34. package/src/engine.ts +28 -28
  35. package/src/ik-solver.ts +411 -411
  36. package/src/math.ts +584 -584
  37. package/src/physics.ts +742 -742
  38. package/src/vmd-loader.ts +276 -276
  39. package/dist/audio.d.ts +0 -29
  40. package/dist/audio.d.ts.map +0 -1
  41. package/dist/audio.js +0 -116
  42. package/dist/particles.d.ts +0 -67
  43. package/dist/particles.d.ts.map +0 -1
  44. package/dist/particles.js +0 -266
package/dist/ik.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { Vec3 } from "./math";
2
+ import type { Skeleton } from "./model";
3
+ export interface IKLink {
4
+ boneIndex: number;
5
+ hasLimit: boolean;
6
+ minAngle?: Vec3;
7
+ maxAngle?: Vec3;
8
+ }
9
+ export interface IKChain {
10
+ targetBoneIndex: number;
11
+ effectorBoneIndex: number;
12
+ iterationCount: number;
13
+ rotationConstraint: number;
14
+ links: IKLink[];
15
+ enabled: boolean;
16
+ }
17
+ export declare class IK {
18
+ private chains;
19
+ private computeWorldMatricesCallback?;
20
+ private lastTargetPositions;
21
+ private convergedChains;
22
+ private ikRotations;
23
+ constructor(chains?: IKChain[]);
24
+ setComputeWorldMatricesCallback(callback: () => void): void;
25
+ getChains(): IKChain[];
26
+ enableChain(targetBoneName: string, enabled: boolean): void;
27
+ solve(skeleton: Skeleton, localRotations: Float32Array, localTranslations: Float32Array, worldMatrices: Float32Array): void;
28
+ private solveCCD;
29
+ private applyAngleConstraints;
30
+ private computeWorldMatrices;
31
+ }
32
+ //# sourceMappingURL=ik.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ik.d.ts","sourceRoot":"","sources":["../src/ik.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,IAAI,EAAQ,MAAM,QAAQ,CAAA;AACzC,OAAO,KAAK,EAAQ,QAAQ,EAAE,MAAM,SAAS,CAAA;AAE7C,MAAM,WAAW,MAAM;IACrB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,OAAO,CAAA;IACjB,QAAQ,CAAC,EAAE,IAAI,CAAA;IACf,QAAQ,CAAC,EAAE,IAAI,CAAA;CAChB;AAED,MAAM,WAAW,OAAO;IACtB,eAAe,EAAE,MAAM,CAAA;IACvB,iBAAiB,EAAE,MAAM,CAAA;IACzB,cAAc,EAAE,MAAM,CAAA;IACtB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;CACjB;AAED,qBAAa,EAAE;IACb,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,4BAA4B,CAAC,CAAY;IAEjD,OAAO,CAAC,mBAAmB,CAA+B;IAE1D,OAAO,CAAC,eAAe,CAAyB;IAEhD,OAAO,CAAC,WAAW,CAA+B;gBAEtC,MAAM,GAAE,OAAO,EAAO;IAMlC,+BAA+B,CAAC,QAAQ,EAAE,MAAM,IAAI,GAAG,IAAI;IAI3D,SAAS,IAAI,OAAO,EAAE;IAKtB,WAAW,CAAC,cAAc,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAU3D,KAAK,CACH,QAAQ,EAAE,QAAQ,EAClB,cAAc,EAAE,YAAY,EAC5B,iBAAiB,EAAE,YAAY,EAC/B,aAAa,EAAE,YAAY,GAC1B,IAAI;IA2FP,OAAO,CAAC,QAAQ;IAqLhB,OAAO,CAAC,qBAAqB;IAe7B,OAAO,CAAC,oBAAoB;CAsG7B"}
package/dist/ik.js ADDED
@@ -0,0 +1,337 @@
1
+ import { Quat, Vec3, Mat4 } from "./math";
2
+ export class IK {
3
+ constructor(chains = []) {
4
+ // Track last target positions to detect movement
5
+ this.lastTargetPositions = new Map();
6
+ // Track convergence state for each chain (true = converged, skip solving)
7
+ this.convergedChains = new Set();
8
+ // Accumulated IK rotations for each bone (boneIndex -> quaternion)
9
+ this.ikRotations = new Map();
10
+ this.chains = chains;
11
+ }
12
+ // Set the callback to use Model's computeWorldMatrices
13
+ // The callback doesn't need parameters since Model uses its own runtime state
14
+ setComputeWorldMatricesCallback(callback) {
15
+ this.computeWorldMatricesCallback = callback;
16
+ }
17
+ getChains() {
18
+ return this.chains;
19
+ }
20
+ // Enable/disable IK chain by target bone name
21
+ enableChain(targetBoneName, enabled) {
22
+ // Chains are identified by target bone index, but we need to find by name
23
+ // This will be called from Model which has bone name lookup
24
+ for (const chain of this.chains) {
25
+ // We'll need to pass bone names or use indices - for now, this is a placeholder
26
+ // The actual implementation will be in Model class
27
+ }
28
+ }
29
+ // Main IK solve method - modifies bone local rotations in-place
30
+ solve(skeleton, localRotations, localTranslations, worldMatrices) {
31
+ if (this.chains.length === 0)
32
+ return;
33
+ const boneCount = skeleton.bones.length;
34
+ // Reset accumulated IK rotations for all chain bones (as per reference)
35
+ for (const chain of this.chains) {
36
+ for (const link of chain.links) {
37
+ const boneIdx = link.boneIndex;
38
+ if (boneIdx >= 0 && boneIdx < boneCount) {
39
+ this.ikRotations.set(boneIdx, new Quat(0, 0, 0, 1));
40
+ }
41
+ }
42
+ }
43
+ // Use Model's computeWorldMatrices if available (it uses the same arrays)
44
+ // Otherwise fall back to simplified version
45
+ if (this.computeWorldMatricesCallback) {
46
+ this.computeWorldMatricesCallback();
47
+ }
48
+ else {
49
+ // Fallback to simplified version (shouldn't happen in normal usage)
50
+ this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
51
+ }
52
+ // Solve each IK chain
53
+ for (const chain of this.chains) {
54
+ if (!chain.enabled)
55
+ continue;
56
+ const targetBoneIdx = chain.targetBoneIndex;
57
+ const effectorBoneIdx = chain.effectorBoneIndex;
58
+ if (targetBoneIdx < 0 || targetBoneIdx >= boneCount || effectorBoneIdx < 0 || effectorBoneIdx >= boneCount) {
59
+ continue;
60
+ }
61
+ // Get target position (world position of target bone)
62
+ // In MMD, the target bone is the bone that should reach a specific position
63
+ // The target bone's current world position is where we want the effector to reach
64
+ const targetWorldMatIdx = targetBoneIdx * 16;
65
+ const targetWorldMat = new Mat4(worldMatrices.subarray(targetWorldMatIdx, targetWorldMatIdx + 16));
66
+ const targetPos = targetWorldMat.getPosition();
67
+ // Check if target has moved (detect any movement, even small)
68
+ const lastTargetPos = this.lastTargetPositions.get(targetBoneIdx);
69
+ let targetMoved = false;
70
+ if (!lastTargetPos) {
71
+ // First time seeing this target, initialize position and always solve
72
+ this.lastTargetPositions.set(targetBoneIdx, new Vec3(targetPos.x, targetPos.y, targetPos.z));
73
+ targetMoved = true;
74
+ }
75
+ else {
76
+ const targetMoveDistance = targetPos.subtract(lastTargetPos).length();
77
+ targetMoved = targetMoveDistance > 0.001; // Detect any movement > 0.001 units (0.1mm)
78
+ }
79
+ // Get current effector position
80
+ const effectorWorldMatIdx = effectorBoneIdx * 16;
81
+ const effectorWorldMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
82
+ const effectorPos = effectorWorldMat.getPosition();
83
+ // Check distance to target
84
+ const distanceToTarget = effectorPos.subtract(targetPos).length();
85
+ // If target moved, always clear convergence and solve
86
+ if (targetMoved) {
87
+ this.convergedChains.delete(targetBoneIdx);
88
+ this.lastTargetPositions.set(targetBoneIdx, new Vec3(targetPos.x, targetPos.y, targetPos.z));
89
+ // Always solve when target moves, regardless of distance
90
+ }
91
+ else if (distanceToTarget < 0.1) {
92
+ // Target hasn't moved and we're already close, skip solving
93
+ if (!this.convergedChains.has(targetBoneIdx)) {
94
+ this.convergedChains.add(targetBoneIdx);
95
+ }
96
+ continue;
97
+ }
98
+ // Otherwise, solve (target hasn't moved but effector is far from target)
99
+ // Solve using CCD
100
+ // Note: In PMX, links are stored from effector toward root
101
+ // So links[0] is the effector, links[links.length-1] is closest to root
102
+ this.solveCCD(chain, skeleton, localRotations, localTranslations, worldMatrices, targetPos);
103
+ // Recompute world matrices after IK adjustments
104
+ if (this.computeWorldMatricesCallback) {
105
+ this.computeWorldMatricesCallback();
106
+ }
107
+ else {
108
+ this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
109
+ }
110
+ }
111
+ }
112
+ // Cyclic Coordinate Descent IK solver (based on saba MMD implementation)
113
+ solveCCD(chain, skeleton, localRotations, localTranslations, worldMatrices, targetPos) {
114
+ const bones = skeleton.bones;
115
+ const iterationCount = chain.iterationCount;
116
+ const rotationConstraint = chain.rotationConstraint;
117
+ const links = chain.links;
118
+ if (links.length === 0)
119
+ return;
120
+ const effectorBoneIdx = chain.effectorBoneIndex;
121
+ // Get effector position
122
+ const effectorWorldMatIdx = effectorBoneIdx * 16;
123
+ const effectorWorldMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
124
+ let effectorPos = effectorWorldMat.getPosition();
125
+ // Check initial distance - only skip if extremely close (numerical precision threshold)
126
+ const initialDistanceSq = effectorPos.subtract(targetPos).lengthSquared();
127
+ if (initialDistanceSq < 1.0e-10) {
128
+ this.convergedChains.add(chain.targetBoneIndex);
129
+ return;
130
+ }
131
+ const halfIteration = iterationCount >> 1;
132
+ for (let iter = 0; iter < iterationCount; iter++) {
133
+ const useAxis = iter < halfIteration;
134
+ for (let linkIdx = 0; linkIdx < links.length; linkIdx++) {
135
+ const link = links[linkIdx];
136
+ const jointBoneIdx = link.boneIndex;
137
+ if (jointBoneIdx < 0 || jointBoneIdx >= bones.length)
138
+ continue;
139
+ const bone = bones[jointBoneIdx];
140
+ // Get joint world position
141
+ const jointWorldMatIdx = jointBoneIdx * 16;
142
+ const jointWorldMat = new Mat4(worldMatrices.subarray(jointWorldMatIdx, jointWorldMatIdx + 16));
143
+ const jointPos = jointWorldMat.getPosition();
144
+ // Vectors: from joint to target and effector (REVERSED from typical CCD!)
145
+ // This matches the reference implementation
146
+ const chainTargetVector = jointPos.subtract(targetPos).normalize();
147
+ const chainIkVector = jointPos.subtract(effectorPos).normalize();
148
+ // Rotation axis: cross product
149
+ const chainRotationAxis = chainTargetVector.cross(chainIkVector);
150
+ const axisLenSq = chainRotationAxis.lengthSquared();
151
+ // Skip if axis is too small (vectors are parallel)
152
+ if (axisLenSq < 1.0e-8)
153
+ continue;
154
+ const chainRotationAxisNorm = chainRotationAxis.normalize();
155
+ // Get parent's world rotation matrix (rotation part only)
156
+ let parentWorldRot;
157
+ if (bone.parentIndex >= 0 && bone.parentIndex < bones.length) {
158
+ const parentWorldMatIdx = bone.parentIndex * 16;
159
+ const parentWorldMat = new Mat4(worldMatrices.subarray(parentWorldMatIdx, parentWorldMatIdx + 16));
160
+ parentWorldRot = parentWorldMat.toQuat();
161
+ }
162
+ else {
163
+ parentWorldRot = new Quat(0, 0, 0, 1);
164
+ }
165
+ // Transform rotation axis to parent's local space
166
+ // Invert parent rotation: parentWorldRot^-1
167
+ const parentWorldRotInv = parentWorldRot.conjugate();
168
+ // Transform axis: parentWorldRotInv * axis (as vector)
169
+ const localAxis = parentWorldRotInv.rotateVec(chainRotationAxisNorm).normalize();
170
+ // Calculate angle between vectors
171
+ const dot = Math.max(-1.0, Math.min(1.0, chainTargetVector.dot(chainIkVector)));
172
+ const angle = Math.min(rotationConstraint * (linkIdx + 1), Math.acos(dot));
173
+ // Create rotation quaternion from axis and angle
174
+ // q = (sin(angle/2) * axis, cos(angle/2))
175
+ const halfAngle = angle * 0.5;
176
+ const sinHalf = Math.sin(halfAngle);
177
+ const cosHalf = Math.cos(halfAngle);
178
+ const rotationFromAxis = new Quat(localAxis.x * sinHalf, localAxis.y * sinHalf, localAxis.z * sinHalf, cosHalf).normalize();
179
+ // Get accumulated ikRotation for this bone (or identity if first time)
180
+ let accumulatedIkRot = this.ikRotations.get(jointBoneIdx) || new Quat(0, 0, 0, 1);
181
+ // Accumulate rotation: ikRotation = rotationFromAxis * ikRotation
182
+ // Reference: ikRotation.multiplyToRef(chainBone.ikChainInfo!.ikRotation, chainBone.ikChainInfo!.ikRotation)
183
+ // This means: ikRotation = rotationFromAxis * accumulatedIkRot
184
+ accumulatedIkRot = rotationFromAxis.multiply(accumulatedIkRot);
185
+ this.ikRotations.set(jointBoneIdx, accumulatedIkRot);
186
+ // Get current local rotation
187
+ const qi = jointBoneIdx * 4;
188
+ const currentLocalRot = new Quat(localRotations[qi], localRotations[qi + 1], localRotations[qi + 2], localRotations[qi + 3]);
189
+ // Reference: ikRotation.multiplyToRef(chainBone.ikChainInfo!.localRotation, ikRotation)
190
+ // This means: tempRot = accumulatedIkRot * currentLocalRot
191
+ let tempRot = accumulatedIkRot.multiply(currentLocalRot);
192
+ // Apply angle constraints if specified (on the combined rotation)
193
+ if (link.hasLimit && link.minAngle && link.maxAngle) {
194
+ tempRot = this.applyAngleConstraints(tempRot, link.minAngle, link.maxAngle);
195
+ }
196
+ // Reference: ikRotation.multiplyToRef(invertedLocalRotation, ikRotation)
197
+ // This means: accumulatedIkRot = tempRot * currentLocalRot^-1
198
+ // But we need the new local rotation, not the accumulated IK rotation
199
+ // The new local rotation should be: newLocalRot such that accumulatedIkRot * newLocalRot = tempRot
200
+ // So: newLocalRot = accumulatedIkRot^-1 * tempRot
201
+ // But wait, the reference updates ikRotation, not localRotation directly...
202
+ // Actually, looking at the reference, it seems like ikRotation is used to compute the final rotation
203
+ // Let me try a different approach: the reference applies constraints to (ikRotation * localRotation)
204
+ // then extracts the new ikRotation, but we need the new localRotation
205
+ // Actually, I think the issue is that we should apply: newLocalRot = tempRot (the constrained result)
206
+ // But we need to extract what the new local rotation should be
207
+ // If tempRot = accumulatedIkRot * currentLocalRot (after constraints)
208
+ // Then: newLocalRot = accumulatedIkRot^-1 * tempRot
209
+ const accumulatedIkRotInv = accumulatedIkRot.conjugate();
210
+ let newLocalRot = accumulatedIkRotInv.multiply(tempRot);
211
+ // Update local rotation
212
+ const normalized = newLocalRot.normalize();
213
+ localRotations[qi] = normalized.x;
214
+ localRotations[qi + 1] = normalized.y;
215
+ localRotations[qi + 2] = normalized.z;
216
+ localRotations[qi + 3] = normalized.w;
217
+ // Update accumulated IK rotation as per reference
218
+ const localRotInv = currentLocalRot.conjugate();
219
+ accumulatedIkRot = tempRot.multiply(localRotInv);
220
+ this.ikRotations.set(jointBoneIdx, accumulatedIkRot);
221
+ // Update world matrices after this link adjustment (only once per link, not per bone)
222
+ if (this.computeWorldMatricesCallback) {
223
+ this.computeWorldMatricesCallback();
224
+ }
225
+ else {
226
+ this.computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices);
227
+ }
228
+ // Update effector position for next link
229
+ const updatedEffectorMat2 = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
230
+ effectorPos = updatedEffectorMat2.getPosition();
231
+ // Early exit if converged (check against original target position)
232
+ const currentDistanceSq = effectorPos.subtract(targetPos).lengthSquared();
233
+ if (currentDistanceSq < 1.0e-10) {
234
+ this.convergedChains.add(chain.targetBoneIndex);
235
+ return;
236
+ }
237
+ }
238
+ // Check convergence at end of iteration
239
+ const finalEffectorMat = new Mat4(worldMatrices.subarray(effectorWorldMatIdx, effectorWorldMatIdx + 16));
240
+ const finalEffectorPos = finalEffectorMat.getPosition();
241
+ const finalDistanceSq = finalEffectorPos.subtract(targetPos).lengthSquared();
242
+ if (finalDistanceSq < 1.0e-10) {
243
+ this.convergedChains.add(chain.targetBoneIndex);
244
+ break;
245
+ }
246
+ }
247
+ }
248
+ // Apply angle constraints to local rotation (Euler angle limits)
249
+ applyAngleConstraints(localRot, minAngle, maxAngle) {
250
+ // Convert quaternion to Euler angles
251
+ const euler = localRot.toEuler();
252
+ // Clamp each Euler angle component
253
+ let clampedX = Math.max(minAngle.x, Math.min(maxAngle.x, euler.x));
254
+ let clampedY = Math.max(minAngle.y, Math.min(maxAngle.y, euler.y));
255
+ let clampedZ = Math.max(minAngle.z, Math.min(maxAngle.z, euler.z));
256
+ // Convert back to quaternion (ZXY order, left-handed)
257
+ return Quat.fromEuler(clampedX, clampedY, clampedZ);
258
+ }
259
+ // Compute world matrices from local rotations and translations
260
+ // This matches Model.computeWorldMatrices logic (including append transforms)
261
+ computeWorldMatrices(skeleton, localRotations, localTranslations, worldMatrices) {
262
+ const bones = skeleton.bones;
263
+ const boneCount = bones.length;
264
+ const computed = new Array(boneCount).fill(false);
265
+ const computeWorld = (i) => {
266
+ if (computed[i])
267
+ return;
268
+ const bone = bones[i];
269
+ if (bone.parentIndex >= boneCount) {
270
+ console.warn(`[IK] bone ${i} parent out of range: ${bone.parentIndex}`);
271
+ }
272
+ const qi = i * 4;
273
+ const ti = i * 3;
274
+ // Get local rotation
275
+ let rotateM = Mat4.fromQuat(localRotations[qi], localRotations[qi + 1], localRotations[qi + 2], localRotations[qi + 3]);
276
+ let addLocalTx = 0, addLocalTy = 0, addLocalTz = 0;
277
+ // Handle append transforms (same as Model.computeWorldMatrices)
278
+ const appendParentIdx = bone.appendParentIndex;
279
+ const hasAppend = bone.appendRotate && appendParentIdx !== undefined && appendParentIdx >= 0 && appendParentIdx < boneCount;
280
+ if (hasAppend) {
281
+ const ratio = bone.appendRatio === undefined ? 1 : Math.max(-1, Math.min(1, bone.appendRatio));
282
+ const hasRatio = Math.abs(ratio) > 1e-6;
283
+ if (hasRatio) {
284
+ const apQi = appendParentIdx * 4;
285
+ const apTi = appendParentIdx * 3;
286
+ if (bone.appendRotate) {
287
+ let ax = localRotations[apQi];
288
+ let ay = localRotations[apQi + 1];
289
+ let az = localRotations[apQi + 2];
290
+ const aw = localRotations[apQi + 3];
291
+ const absRatio = ratio < 0 ? -ratio : ratio;
292
+ if (ratio < 0) {
293
+ ax = -ax;
294
+ ay = -ay;
295
+ az = -az;
296
+ }
297
+ const identityQuat = new Quat(0, 0, 0, 1);
298
+ const appendQuat = new Quat(ax, ay, az, aw);
299
+ const result = Quat.slerp(identityQuat, appendQuat, absRatio);
300
+ rotateM = Mat4.fromQuat(result.x, result.y, result.z, result.w).multiply(rotateM);
301
+ }
302
+ if (bone.appendMove) {
303
+ const appendRatio = bone.appendRatio ?? 1;
304
+ addLocalTx = localTranslations[apTi] * appendRatio;
305
+ addLocalTy = localTranslations[apTi + 1] * appendRatio;
306
+ addLocalTz = localTranslations[apTi + 2] * appendRatio;
307
+ }
308
+ }
309
+ }
310
+ // Get bone's own translation
311
+ const boneTx = localTranslations[ti];
312
+ const boneTy = localTranslations[ti + 1];
313
+ const boneTz = localTranslations[ti + 2];
314
+ // Build local matrix: bindTranslation + rotation + (bone translation + append translation)
315
+ const localM = Mat4.identity().translateInPlace(bone.bindTranslation[0], bone.bindTranslation[1], bone.bindTranslation[2]);
316
+ const transM = Mat4.identity().translateInPlace(boneTx + addLocalTx, boneTy + addLocalTy, boneTz + addLocalTz);
317
+ const localMatrix = localM.multiply(rotateM).multiply(transM);
318
+ const worldOffset = i * 16;
319
+ if (bone.parentIndex >= 0) {
320
+ const p = bone.parentIndex;
321
+ if (!computed[p])
322
+ computeWorld(p);
323
+ const parentOffset = p * 16;
324
+ const parentWorldMat = new Mat4(worldMatrices.subarray(parentOffset, parentOffset + 16));
325
+ const worldMat = parentWorldMat.multiply(localMatrix);
326
+ worldMatrices.set(worldMat.values, worldOffset);
327
+ }
328
+ else {
329
+ worldMatrices.set(localMatrix.values, worldOffset);
330
+ }
331
+ computed[i] = true;
332
+ };
333
+ // Process all bones
334
+ for (let i = 0; i < boneCount; i++)
335
+ computeWorld(i);
336
+ }
337
+ }
@@ -0,0 +1,64 @@
1
+ import { Quat, Vec3 } from "./math";
2
+ export interface AnimationPose {
3
+ boneRotations: Map<string, Quat>;
4
+ boneTranslations: Map<string, Vec3>;
5
+ morphWeights: Map<string, number>;
6
+ }
7
+ export interface AnimationProgress {
8
+ current: number;
9
+ duration: number;
10
+ percentage: number;
11
+ }
12
+ export declare class Player {
13
+ private frames;
14
+ private boneTracks;
15
+ private morphTracks;
16
+ private _duration;
17
+ private isPlaying;
18
+ private isPaused;
19
+ private _currentTime;
20
+ private startTime;
21
+ /**
22
+ * Load VMD animation file
23
+ */
24
+ loadVmd(vmdUrl: string): Promise<void>;
25
+ /**
26
+ * Process frames into tracks
27
+ */
28
+ private processFrames;
29
+ /**
30
+ * Start or resume playback
31
+ * Note: For iOS, this should be called synchronously from a user interaction event
32
+ */
33
+ play(): void;
34
+ /**
35
+ * Pause playback
36
+ */
37
+ pause(): void;
38
+ /**
39
+ * Stop playback and reset to beginning
40
+ */
41
+ stop(): void;
42
+ /**
43
+ * Seek to specific time
44
+ */
45
+ seek(time: number): void;
46
+ /**
47
+ * Update playback and return current pose
48
+ * Returns null if not playing, but returns current pose if paused
49
+ */
50
+ update(currentRealTime: number): AnimationPose | null;
51
+ /**
52
+ * Get pose at specific time (pure function)
53
+ */
54
+ getPoseAtTime(time: number): AnimationPose;
55
+ /**
56
+ * Get current playback progress
57
+ */
58
+ getProgress(): AnimationProgress;
59
+ get currentTime(): number;
60
+ get duration(): number;
61
+ get isPlayingState(): boolean;
62
+ get isPausedState(): boolean;
63
+ }
64
+ //# sourceMappingURL=player.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"player.d.ts","sourceRoot":"","sources":["../src/player.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,IAAI,EAAqB,MAAM,QAAQ,CAAA;AAGtD,MAAM,WAAW,aAAa;IAC5B,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IAChC,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IACnC,YAAY,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAClC;AAED,MAAM,WAAW,iBAAiB;IAChC,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,UAAU,EAAE,MAAM,CAAA;CACnB;AAED,qBAAa,MAAM;IAEjB,OAAO,CAAC,MAAM,CAAoB;IAClC,OAAO,CAAC,UAAU,CAAwE;IAC1F,OAAO,CAAC,WAAW,CAA0E;IAC7F,OAAO,CAAC,SAAS,CAAY;IAG7B,OAAO,CAAC,SAAS,CAAiB;IAClC,OAAO,CAAC,QAAQ,CAAiB;IACjC,OAAO,CAAC,YAAY,CAAY;IAGhC,OAAO,CAAC,SAAS,CAAY;IAE7B;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM5C;;OAEG;IACH,OAAO,CAAC,aAAa;IAsDrB;;;OAGG;IACH,IAAI,IAAI,IAAI;IASZ;;OAEG;IACH,KAAK,IAAI,IAAI;IAKb;;OAEG;IACH,IAAI,IAAI,IAAI;IAOZ;;OAEG;IACH,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IASxB;;;OAGG;IACH,MAAM,CAAC,eAAe,EAAE,MAAM,GAAG,aAAa,GAAG,IAAI;IAwBrD;;OAEG;IACH,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa;IA6F1C;;OAEG;IACH,WAAW,IAAI,iBAAiB;IAQhC,IAAI,WAAW,IAAI,MAAM,CAExB;IAED,IAAI,QAAQ,IAAI,MAAM,CAErB;IAED,IAAI,cAAc,IAAI,OAAO,CAE5B;IAED,IAAI,aAAa,IAAI,OAAO,CAE3B;CACF"}
package/dist/player.js ADDED
@@ -0,0 +1,220 @@
1
+ import { Quat, Vec3, bezierInterpolate } from "./math";
2
+ import { VMDLoader } from "./vmd-loader";
3
+ export class Player {
4
+ constructor() {
5
+ // Animation data
6
+ this.frames = [];
7
+ this.boneTracks = new Map();
8
+ this.morphTracks = new Map();
9
+ this._duration = 0;
10
+ // Playback state
11
+ this.isPlaying = false;
12
+ this.isPaused = false;
13
+ this._currentTime = 0;
14
+ // Timing
15
+ this.startTime = 0; // Real-time when playback started
16
+ }
17
+ /**
18
+ * Load VMD animation file
19
+ */
20
+ async loadVmd(vmdUrl) {
21
+ // Load animation
22
+ this.frames = await VMDLoader.load(vmdUrl);
23
+ this.processFrames();
24
+ }
25
+ /**
26
+ * Process frames into tracks
27
+ */
28
+ processFrames() {
29
+ // Helper to group frames by name and sort by time
30
+ const groupFrames = (items) => {
31
+ const tracks = new Map();
32
+ for (const { item, name, time } of items) {
33
+ if (!tracks.has(name))
34
+ tracks.set(name, []);
35
+ tracks.get(name).push({ item, time });
36
+ }
37
+ for (const keyFrames of tracks.values()) {
38
+ keyFrames.sort((a, b) => a.time - b.time);
39
+ }
40
+ return tracks;
41
+ };
42
+ // Collect all bone and morph frames
43
+ const boneItems = [];
44
+ const morphItems = [];
45
+ for (const keyFrame of this.frames) {
46
+ for (const boneFrame of keyFrame.boneFrames) {
47
+ boneItems.push({ item: boneFrame, name: boneFrame.boneName, time: keyFrame.time });
48
+ }
49
+ for (const morphFrame of keyFrame.morphFrames) {
50
+ morphItems.push({ item: morphFrame, name: morphFrame.morphName, time: keyFrame.time });
51
+ }
52
+ }
53
+ // Transform to expected format
54
+ this.boneTracks = new Map();
55
+ for (const [name, frames] of groupFrames(boneItems).entries()) {
56
+ this.boneTracks.set(name, frames.map((f) => ({ boneFrame: f.item, time: f.time })));
57
+ }
58
+ this.morphTracks = new Map();
59
+ for (const [name, frames] of groupFrames(morphItems).entries()) {
60
+ this.morphTracks.set(name, frames.map((f) => ({ morphFrame: f.item, time: f.time })));
61
+ }
62
+ // Calculate duration from all tracks
63
+ const allTracks = [...this.boneTracks.values(), ...this.morphTracks.values()];
64
+ this._duration = allTracks.reduce((max, keyFrames) => {
65
+ const lastTime = keyFrames[keyFrames.length - 1]?.time ?? 0;
66
+ return Math.max(max, lastTime);
67
+ }, 0);
68
+ }
69
+ /**
70
+ * Start or resume playback
71
+ * Note: For iOS, this should be called synchronously from a user interaction event
72
+ */
73
+ play() {
74
+ if (this.frames.length === 0)
75
+ return;
76
+ this.isPaused = false;
77
+ this.startTime = performance.now() - this._currentTime * 1000;
78
+ this.isPlaying = true;
79
+ }
80
+ /**
81
+ * Pause playback
82
+ */
83
+ pause() {
84
+ if (!this.isPlaying || this.isPaused)
85
+ return;
86
+ this.isPaused = true;
87
+ }
88
+ /**
89
+ * Stop playback and reset to beginning
90
+ */
91
+ stop() {
92
+ this.isPlaying = false;
93
+ this.isPaused = false;
94
+ this._currentTime = 0;
95
+ this.startTime = 0;
96
+ }
97
+ /**
98
+ * Seek to specific time
99
+ */
100
+ seek(time) {
101
+ const clampedTime = Math.max(0, Math.min(time, this._duration));
102
+ this._currentTime = clampedTime;
103
+ if (this.isPlaying && !this.isPaused) {
104
+ this.startTime = performance.now() - clampedTime * 1000;
105
+ }
106
+ }
107
+ /**
108
+ * Update playback and return current pose
109
+ * Returns null if not playing, but returns current pose if paused
110
+ */
111
+ update(currentRealTime) {
112
+ if (!this.isPlaying || this.frames.length === 0) {
113
+ return null;
114
+ }
115
+ // If paused, return current pose at paused time (no time update)
116
+ if (this.isPaused) {
117
+ return this.getPoseAtTime(this._currentTime);
118
+ }
119
+ // Calculate current animation time
120
+ const elapsedSeconds = (currentRealTime - this.startTime) / 1000;
121
+ this._currentTime = elapsedSeconds;
122
+ // Check if animation ended
123
+ if (this._currentTime >= this._duration) {
124
+ this._currentTime = this._duration;
125
+ this.pause(); // Auto-pause at end
126
+ return this.getPoseAtTime(this._currentTime);
127
+ }
128
+ return this.getPoseAtTime(this._currentTime);
129
+ }
130
+ /**
131
+ * Get pose at specific time (pure function)
132
+ */
133
+ getPoseAtTime(time) {
134
+ const pose = {
135
+ boneRotations: new Map(),
136
+ boneTranslations: new Map(),
137
+ morphWeights: new Map(),
138
+ };
139
+ // Generic binary search for upper bound
140
+ const upperBound = (time, keyFrames) => {
141
+ let left = 0, right = keyFrames.length;
142
+ while (left < right) {
143
+ const mid = Math.floor((left + right) / 2);
144
+ if (keyFrames[mid].time <= time)
145
+ left = mid + 1;
146
+ else
147
+ right = mid;
148
+ }
149
+ return left;
150
+ };
151
+ // Process bone tracks
152
+ for (const [boneName, keyFrames] of this.boneTracks.entries()) {
153
+ if (keyFrames.length === 0)
154
+ continue;
155
+ const clampedTime = Math.max(keyFrames[0].time, Math.min(keyFrames[keyFrames.length - 1].time, time));
156
+ const idx = upperBound(clampedTime, keyFrames) - 1;
157
+ if (idx < 0)
158
+ continue;
159
+ const frameA = keyFrames[idx].boneFrame;
160
+ const frameB = keyFrames[idx + 1]?.boneFrame;
161
+ if (!frameB) {
162
+ pose.boneRotations.set(boneName, frameA.rotation);
163
+ pose.boneTranslations.set(boneName, frameA.translation);
164
+ }
165
+ else {
166
+ const timeA = keyFrames[idx].time;
167
+ const timeB = keyFrames[idx + 1].time;
168
+ const gradient = (clampedTime - timeA) / (timeB - timeA);
169
+ const interp = frameB.interpolation;
170
+ pose.boneRotations.set(boneName, Quat.slerp(frameA.rotation, frameB.rotation, bezierInterpolate(interp[0] / 127, interp[1] / 127, interp[2] / 127, interp[3] / 127, gradient)));
171
+ const lerp = (a, b, w) => a + (b - a) * w;
172
+ const getWeight = (offset) => bezierInterpolate(interp[offset] / 127, interp[offset + 8] / 127, interp[offset + 4] / 127, interp[offset + 12] / 127, gradient);
173
+ pose.boneTranslations.set(boneName, new Vec3(lerp(frameA.translation.x, frameB.translation.x, getWeight(0)), lerp(frameA.translation.y, frameB.translation.y, getWeight(16)), lerp(frameA.translation.z, frameB.translation.z, getWeight(32))));
174
+ }
175
+ }
176
+ // Process morph tracks
177
+ for (const [morphName, keyFrames] of this.morphTracks.entries()) {
178
+ if (keyFrames.length === 0)
179
+ continue;
180
+ const clampedTime = Math.max(keyFrames[0].time, Math.min(keyFrames[keyFrames.length - 1].time, time));
181
+ const idx = upperBound(clampedTime, keyFrames) - 1;
182
+ if (idx < 0)
183
+ continue;
184
+ const frameA = keyFrames[idx].morphFrame;
185
+ const frameB = keyFrames[idx + 1]?.morphFrame;
186
+ if (!frameB) {
187
+ pose.morphWeights.set(morphName, frameA.weight);
188
+ }
189
+ else {
190
+ const timeA = keyFrames[idx].time;
191
+ const timeB = keyFrames[idx + 1].time;
192
+ const gradient = (clampedTime - timeA) / (timeB - timeA);
193
+ pose.morphWeights.set(morphName, frameA.weight + (frameB.weight - frameA.weight) * gradient);
194
+ }
195
+ }
196
+ return pose;
197
+ }
198
+ /**
199
+ * Get current playback progress
200
+ */
201
+ getProgress() {
202
+ return {
203
+ current: this._currentTime,
204
+ duration: this._duration,
205
+ percentage: this._duration > 0 ? (this._currentTime / this._duration) * 100 : 0,
206
+ };
207
+ }
208
+ get currentTime() {
209
+ return this._currentTime;
210
+ }
211
+ get duration() {
212
+ return this._duration;
213
+ }
214
+ get isPlayingState() {
215
+ return this.isPlaying && !this.isPaused;
216
+ }
217
+ get isPausedState() {
218
+ return this.isPaused;
219
+ }
220
+ }