reze-engine 0.3.4 → 0.3.5

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.
package/src/model.ts CHANGED
@@ -50,24 +50,6 @@ export interface IKLink {
50
50
  hasLimit: boolean
51
51
  minAngle?: Vec3 // Minimum Euler angles (radians)
52
52
  maxAngle?: Vec3 // Maximum Euler angles (radians)
53
- rotationOrder?: EulerRotationOrder // YXZ, ZYX, or XZY
54
- solveAxis?: SolveAxis // None, Fixed, X, Y, or Z
55
- }
56
-
57
- // Euler rotation order for angle constraints
58
- export enum EulerRotationOrder {
59
- YXZ = 0,
60
- ZYX = 1,
61
- XZY = 2,
62
- }
63
-
64
- // Solve axis optimization
65
- export enum SolveAxis {
66
- None = 0,
67
- Fixed = 1,
68
- X = 2,
69
- Y = 3,
70
- Z = 4,
71
53
  }
72
54
 
73
55
  // IK solver definition
@@ -78,7 +60,6 @@ export interface IKSolver {
78
60
  iterationCount: number
79
61
  limitAngle: number // Max rotation per iteration (radians)
80
62
  links: IKLink[] // Chain bones from effector to root
81
- canSkipWhenPhysicsEnabled: boolean
82
63
  }
83
64
 
84
65
  // IK chain info per bone (runtime state)
@@ -128,7 +109,6 @@ export interface SkeletonRuntime {
128
109
  localRotations: Float32Array // quat per bone (x,y,z,w) length = boneCount*4
129
110
  localTranslations: Float32Array // vec3 per bone length = boneCount*3
130
111
  worldMatrices: Float32Array // mat4 per bone length = boneCount*16
131
- computedBones: boolean[] // length = boneCount
132
112
  ikChainInfo?: IKChainInfo[] // IK chain info per bone (only for IK chain bones)
133
113
  ikSolvers?: IKSolver[] // All IK solvers in the model
134
114
  }
@@ -245,7 +225,6 @@ export class Model {
245
225
  acc[bone.name] = index
246
226
  return acc
247
227
  }, {} as Record<string, number>),
248
- computedBones: new Array(boneCount).fill(false),
249
228
  }
250
229
 
251
230
  const rotations = this.runtimeSkeleton.localRotations
@@ -283,18 +262,6 @@ export class Model {
283
262
  for (let i = 0; i < boneCount; i++) {
284
263
  const bone = bones[i]
285
264
  if (bone.ikTargetIndex !== undefined && bone.ikLinks && bone.ikLinks.length > 0) {
286
- // Check if all links are affected by physics (for optimization)
287
- let canSkipWhenPhysicsEnabled = true
288
- for (const link of bone.ikLinks) {
289
- // For now, assume no bones are physics-controlled (can be enhanced later)
290
- // If a bone has a rigidbody attached, it's physics-controlled
291
- const hasPhysics = this.rigidbodies.some((rb) => rb.boneIndex === link.boneIndex)
292
- if (!hasPhysics) {
293
- canSkipWhenPhysicsEnabled = false
294
- break
295
- }
296
- }
297
-
298
265
  const solver: IKSolver = {
299
266
  index: solverIndex++,
300
267
  ikBoneIndex: i,
@@ -302,7 +269,6 @@ export class Model {
302
269
  iterationCount: bone.ikIteration ?? 1,
303
270
  limitAngle: bone.ikLimitAngle ?? Math.PI,
304
271
  links: bone.ikLinks,
305
- canSkipWhenPhysicsEnabled,
306
272
  }
307
273
  ikSolvers.push(solver)
308
274
  }
@@ -550,14 +516,10 @@ export class Model {
550
516
  state.targetQuat[qi + 3]
551
517
  )
552
518
  const result = Quat.slerp(startQuat, targetQuat, e)
553
- const cx = result.x
554
- const cy = result.y
555
- const cz = result.z
556
- const cw = result.w
557
- sx = cx
558
- sy = cy
559
- sz = cz
560
- sw = cw
519
+ sx = result.x
520
+ sy = result.y
521
+ sz = result.z
522
+ sw = result.w
561
523
  }
562
524
 
563
525
  state.startQuat[qi] = sx
@@ -706,10 +668,9 @@ export class Model {
706
668
  // Animated change
707
669
  const state = this.morphTweenState
708
670
  const now = performance.now()
709
- const currentWeight = this.runtimeMorph.weights[idx]
710
671
 
711
672
  // If already tweening, start from current interpolated value
712
- let startWeight = currentWeight
673
+ let startWeight = this.runtimeMorph.weights[idx]
713
674
  if (state.active[idx] === 1) {
714
675
  const startMs = state.startTimeMs[idx]
715
676
  const prevDur = Math.max(1, state.durationMs[idx])
@@ -802,13 +763,13 @@ export class Model {
802
763
  this.applyMorphs()
803
764
  }
804
765
 
805
- // Compute initial world matrices (needed for IK solving)
766
+ // Compute initial world matrices (needed for IK solving to read bone positions)
806
767
  this.computeWorldMatrices()
807
768
 
808
- // Solve IK chains (modifies localRotations)
769
+ // Solve IK chains (modifies localRotations with final IK rotations)
809
770
  this.solveIKChains()
810
771
 
811
- // Recompute world matrices with IK rotations applied
772
+ // Recompute world matrices with final IK rotations applied to localRotations
812
773
  this.computeWorldMatrices()
813
774
 
814
775
  return hasActiveMorphTweens
@@ -827,8 +788,7 @@ export class Model {
827
788
  this.runtimeSkeleton.localRotations,
828
789
  this.runtimeSkeleton.localTranslations,
829
790
  this.runtimeSkeleton.worldMatrices,
830
- ikChainInfo,
831
- false // usePhysics - can be enhanced later
791
+ ikChainInfo
832
792
  )
833
793
  }
834
794
 
@@ -837,11 +797,13 @@ export class Model {
837
797
  const localRot = this.runtimeSkeleton.localRotations
838
798
  const localTrans = this.runtimeSkeleton.localTranslations
839
799
  const worldBuf = this.runtimeSkeleton.worldMatrices
840
- const computed = this.runtimeSkeleton.computedBones.fill(false)
841
800
  const boneCount = bones.length
842
801
 
843
802
  if (boneCount === 0) return
844
803
 
804
+ // Local computed array (avoids instance field overhead)
805
+ const computed = new Array<boolean>(boneCount).fill(false)
806
+
845
807
  const computeWorld = (i: number): void => {
846
808
  if (computed[i]) return
847
809
 
@@ -880,14 +842,9 @@ export class Model {
880
842
  ay = -ay
881
843
  az = -az
882
844
  }
883
- const identityQuat = new Quat(0, 0, 0, 1)
884
845
  const appendQuat = new Quat(ax, ay, az, aw)
885
- const result = Quat.slerp(identityQuat, appendQuat, absRatio)
886
- const rx = result.x
887
- const ry = result.y
888
- const rz = result.z
889
- const rw = result.w
890
- rotateM = Mat4.fromQuat(rx, ry, rz, rw).multiply(rotateM)
846
+ const result = Quat.slerp(new Quat(0, 0, 0, 1), appendQuat, absRatio)
847
+ rotateM = Mat4.fromQuat(result.x, result.y, result.z, result.w).multiply(rotateM)
891
848
  }
892
849
 
893
850
  if (b.appendMove) {
package/src/player.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { bezierInterpolate } from "./bezier-interpolate"
2
- import { Quat, Vec3 } from "./math"
1
+ import { Quat, Vec3, bezierInterpolate } from "./math"
3
2
  import { BoneFrame, MorphFrame, VMDKeyFrame, VMDLoader } from "./vmd-loader"
4
3
 
5
4
  export interface AnimationPose {
package/dist/ik.d.ts DELETED
@@ -1,32 +0,0 @@
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
package/dist/ik.d.ts.map DELETED
@@ -1 +0,0 @@
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 DELETED
@@ -1,337 +0,0 @@
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
- }
@@ -1,52 +0,0 @@
1
- export interface PoolSceneConfig {
2
- cloudSharpness?: number;
3
- windSpeed?: [number, number];
4
- bumpFactor?: number;
5
- bumpDistance?: number;
6
- skyColor?: [number, number, number];
7
- moonlightColor?: [number, number, number];
8
- skyByMoonlightColor?: [number, number, number];
9
- waterColor?: [number, number, number];
10
- exposure?: number;
11
- epsilon?: number;
12
- marchSteps?: number;
13
- moonPosition?: [number, number, number];
14
- }
15
- export declare class PoolScene {
16
- private device;
17
- private pipeline;
18
- private computePipeline;
19
- private uniformBuffer;
20
- private computeUniformBuffer;
21
- private bindGroup;
22
- private computeBindGroup;
23
- private startTime;
24
- private config;
25
- private format;
26
- private sampleCount;
27
- private cameraUniformBuffer;
28
- private waterHeightTexture;
29
- private waterHeightTextureView;
30
- private waterHeightSampler;
31
- private waterHeightTextureSize;
32
- private waterPlanePipeline;
33
- private waterPlaneVertexBuffer;
34
- private waterPlaneIndexBuffer;
35
- private waterPlaneBindGroup;
36
- private waterPlaneSize;
37
- private waterPlaneSubdivisions;
38
- private moonPosition;
39
- private moonRadius;
40
- constructor(device: GPUDevice, format: GPUTextureFormat, sampleCount: number, cameraUniformBuffer: GPUBuffer, config?: PoolSceneConfig);
41
- private createWaterHeightTexture;
42
- private createComputePipeline;
43
- private createPipeline;
44
- private createUniformBuffer;
45
- dispatchCompute(encoder: GPUCommandEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
46
- private createWaterPlane;
47
- private createWaterPlanePipeline;
48
- renderWaterPlane(pass: GPURenderPassEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
49
- render(pass: GPURenderPassEncoder, width: number, height: number, modelPos?: [number, number, number]): void;
50
- getSkyColor(): [number, number, number];
51
- }
52
- //# sourceMappingURL=pool-scene.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"pool-scene.d.ts","sourceRoot":"","sources":["../src/pool-scene.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,eAAe;IAC9B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACnC,cAAc,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACzC,mBAAmB,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IAC9C,UAAU,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;CACxC;AAED,qBAAa,SAAS;IACpB,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,QAAQ,CAAoB;IACpC,OAAO,CAAC,eAAe,CAAqB;IAC5C,OAAO,CAAC,aAAa,CAAY;IACjC,OAAO,CAAC,oBAAoB,CAAY;IACxC,OAAO,CAAC,SAAS,CAAe;IAChC,OAAO,CAAC,gBAAgB,CAAe;IACvC,OAAO,CAAC,SAAS,CAA4B;IAC7C,OAAO,CAAC,MAAM,CAA2B;IACzC,OAAO,CAAC,MAAM,CAAkB;IAChC,OAAO,CAAC,WAAW,CAAQ;IAC3B,OAAO,CAAC,mBAAmB,CAAY;IACvC,OAAO,CAAC,kBAAkB,CAAa;IACvC,OAAO,CAAC,sBAAsB,CAAiB;IAC/C,OAAO,CAAC,kBAAkB,CAAa;IACvC,OAAO,CAAC,sBAAsB,CAAc;IAC5C,OAAO,CAAC,kBAAkB,CAAoB;IAC9C,OAAO,CAAC,sBAAsB,CAAY;IAC1C,OAAO,CAAC,qBAAqB,CAAY;IACzC,OAAO,CAAC,mBAAmB,CAAe;IAC1C,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,sBAAsB,CAAc;IAC5C,OAAO,CAAC,YAAY,CAA0B;IAC9C,OAAO,CAAC,UAAU,CAAe;gBAG/B,MAAM,EAAE,SAAS,EACjB,MAAM,EAAE,gBAAgB,EACxB,WAAW,EAAE,MAAM,EACnB,mBAAmB,EAAE,SAAS,EAC9B,MAAM,GAAE,eAAoB;IA6B9B,OAAO,CAAC,wBAAwB;IAmBhC,OAAO,CAAC,qBAAqB;IA8G7B,OAAO,CAAC,cAAc;IAqatB,OAAO,CAAC,mBAAmB;IAgCpB,eAAe,CACpB,OAAO,EAAE,iBAAiB,EAC1B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAa;IA2ChD,OAAO,CAAC,gBAAgB;IA4DxB,OAAO,CAAC,wBAAwB;IAmazB,gBAAgB,CACrB,IAAI,EAAE,oBAAoB,EAC1B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAa;IAiBzC,MAAM,CACX,IAAI,EAAE,oBAAoB,EAC1B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,MAAM,EACd,QAAQ,GAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAa;IAezC,WAAW,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC;CAG/C"}