@needle-tools/three-mesh-effects 0.1.0-alpha → 0.1.0-alpha.0
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/LICENSE +21 -21
- package/README.md +94 -88
- package/build/deformation-runtime.js.map +1 -1
- package/build/impostor-runtime-needle-engine.js.map +1 -1
- package/build/impostor-runtime.js.map +1 -1
- package/build/internal/deformation/cloth-surface.js.map +1 -1
- package/build/internal/deformation/fur-surface.js.map +1 -1
- package/build/internal/deformation/ik-constraints.js.map +1 -1
- package/build/internal/deformation/joints-json.js.map +1 -1
- package/build/internal/deformation/limits.js.map +1 -1
- package/build/internal/deformation/motion-settings.js.map +1 -1
- package/build/internal/deformation/pin-cloth.js.map +1 -1
- package/build/internal/deformation/pin-ik.js.map +1 -1
- package/build/internal/deformation/pin-types.js.map +1 -1
- package/build/internal/deformation/pin-wiggle.js.map +1 -1
- package/build/internal/deformation/pose-slots.js.map +1 -1
- package/build/internal/deformation/pose-timeline.js.map +1 -1
- package/build/internal/deformation/rig-drivers.js.map +1 -1
- package/build/internal/deformation/rig-extension.js.map +1 -1
- package/build/internal/deformation/rig-runtime.js.map +1 -1
- package/build/internal/deformation/wind.js.map +1 -1
- package/build/internal/impostor/impostor-metadata.js.map +1 -1
- package/build/internal/impostor/impostor-node-material.js.map +1 -1
- package/build/internal/impostor/impostor-runtime-support.js.map +1 -1
- package/build/internal/impostor/octahedral.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-display.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-metadata.js.map +1 -1
- package/build/internal/pixel-impostor/pixel-impostor-runtime.js.map +1 -1
- package/build/internal/rendering/skin-projection.js +52 -52
- package/build/internal/rendering/skin-projection.js.map +2 -2
- package/build/internal/rendering/surface-query.js +52 -52
- package/build/internal/rendering/surface-query.js.map +2 -2
- package/build/internal/rendering/wireframe.js.map +1 -1
- package/build/internal/rendering/wrap-preview-settings.js.map +1 -1
- package/build/internal/rendering/wrap-preview.js +52 -52
- package/build/internal/rendering/wrap-preview.js.map +2 -2
- package/build/internal/rendering/wrap-skin.js +52 -52
- package/build/internal/rendering/wrap-skin.js.map +2 -2
- package/build/pixel-impostor-runtime.js.map +1 -1
- package/build/wrap-effects.js +52 -52
- package/build/wrap-effects.js.map +2 -2
- package/package.json +107 -94
- package/src/deformation/cloth-surface.ts +223 -223
- package/src/deformation/fur-surface.ts +129 -129
- package/src/deformation/ik-constraints.ts +37 -37
- package/src/deformation/joints-json.ts +94 -94
- package/src/deformation/limits.ts +25 -25
- package/src/deformation/motion-settings.ts +9 -9
- package/src/deformation/pin-cloth.ts +149 -149
- package/src/deformation/pin-ik.ts +582 -582
- package/src/deformation/pin-types.ts +22 -22
- package/src/deformation/pin-wiggle.ts +119 -119
- package/src/deformation/pose-slots.ts +106 -106
- package/src/deformation/pose-timeline.ts +242 -242
- package/src/deformation/rig-drivers.ts +32 -32
- package/src/deformation/rig-extension.ts +87 -87
- package/src/deformation/rig-runtime.ts +416 -416
- package/src/deformation/wind.ts +36 -36
- package/src/impostor/impostor-metadata.js +137 -137
- package/src/impostor/impostor-node-material.js +1581 -1581
- package/src/impostor/impostor-runtime-support.js +139 -139
- package/src/impostor/octahedral.js +280 -280
- package/src/pixel-impostor/pixel-impostor-display.js +794 -794
- package/src/pixel-impostor/pixel-impostor-metadata.js +74 -74
- package/src/pixel-impostor/pixel-impostor-runtime.js +78 -78
- package/src/public/deformation-runtime.ts +3 -3
- package/src/public/impostor-runtime-needle-engine.js +56 -56
- package/src/public/impostor-runtime.js +21 -21
- package/src/public/pixel-impostor-runtime.js +5 -5
- package/src/public/wrap-effects.js +21 -21
- package/src/rendering/skin-projection.js +113 -113
- package/src/rendering/surface-query.js +88 -88
- package/src/rendering/wireframe.js +115 -115
- package/src/rendering/wrap-preview-settings.js +123 -123
- package/src/rendering/wrap-preview.js +645 -645
- package/src/rendering/wrap-skin.js +109 -109
- package/build/internal/public/deformation-runtime.js +0 -2143
- package/build/internal/public/deformation-runtime.js.map +0 -7
- package/build/internal/public/impostor-runtime-needle-engine.js +0 -42
- package/build/internal/public/impostor-runtime-needle-engine.js.map +0 -7
- package/build/internal/public/impostor-runtime.js +0 -1333
- package/build/internal/public/impostor-runtime.js.map +0 -7
- package/build/internal/public/pixel-impostor-runtime.js +0 -876
- package/build/internal/public/pixel-impostor-runtime.js.map +0 -7
- package/build/internal/public/wrap-effects.js +0 -5640
- package/build/internal/public/wrap-effects.js.map +0 -7
|
@@ -1,242 +1,242 @@
|
|
|
1
|
-
import type { PinPose, PinRecord, Point, Rotation } from './pin-types.js';
|
|
2
|
-
import type { PinParents } from './pin-types.js';
|
|
3
|
-
|
|
4
|
-
/** A pose slot's time controls its share of the loop around its own moment.
|
|
5
|
-
* The transition settles at each authored pose without holding a flat frame. */
|
|
6
|
-
export const DEFAULT_POSE_SECONDS = 0.7;
|
|
7
|
-
export const MIN_POSE_SECONDS = 0.05;
|
|
8
|
-
export const MAX_POSE_SECONDS = 60;
|
|
9
|
-
/** Travel kept below the longest pose so a slider dragged to the bottom can
|
|
10
|
-
* always be dragged further on the next pass, and shrinks back when shortened. */
|
|
11
|
-
export const POSE_TIME_HEADROOM = 0.5;
|
|
12
|
-
|
|
13
|
-
export type PoseTiming = { readonly slot: number; readonly seconds: number };
|
|
14
|
-
export type PoseTimeline = readonly PoseTiming[];
|
|
15
|
-
|
|
16
|
-
export function poseTimeScale(timeline: PoseTimeline): number {
|
|
17
|
-
return Math.max(DEFAULT_POSE_SECONDS, ...timeline.map(entry => entry.seconds)) + POSE_TIME_HEADROOM;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/** The strip reads downwards: zero at the top, the scale at the bottom, which
|
|
21
|
-
* is what makes "drag down" mean "stay longer". */
|
|
22
|
-
export function poseSecondsAt(offset: number, height: number, scale: number): number {
|
|
23
|
-
if (!(height > 0)) return MIN_POSE_SECONDS;
|
|
24
|
-
return Math.min(1, Math.max(0, offset / height)) * scale;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** How far down the strip a time sits, as a percentage for the fill and handle. */
|
|
28
|
-
export function poseTimeOffset(seconds: number, scale: number): number {
|
|
29
|
-
if (!(scale > 0)) return 0;
|
|
30
|
-
return Math.min(100, Math.max(0, (seconds / scale) * 100));
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function poseLoopSeconds(timeline: PoseTimeline): number {
|
|
34
|
-
return timeline.reduce((total, entry) => total + Math.max(0, entry.seconds), 0);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/** The moment each pose is reached. Neighbours split the gap between them in
|
|
38
|
-
* proportion to their own times, which is what makes a time an ownership share
|
|
39
|
-
* rather than the length of one arc. */
|
|
40
|
-
export function poseKnots(timeline: PoseTimeline): number[] {
|
|
41
|
-
const knots = [0];
|
|
42
|
-
for (let i = 1; i < timeline.length; i++) {
|
|
43
|
-
knots.push(knots[i - 1] + (Math.max(0, timeline[i - 1].seconds) + Math.max(0, timeline[i].seconds)) / 2);
|
|
44
|
-
}
|
|
45
|
-
return knots;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** How long one pass takes: the first pose's moment to the last one's.
|
|
49
|
-
* Looping playback adds the closing transition up to poseLoopSeconds. */
|
|
50
|
-
export function posePassSeconds(timeline: PoseTimeline): number {
|
|
51
|
-
const knots = poseKnots(timeline);
|
|
52
|
-
return knots.length ? knots[knots.length - 1] : 0;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function poseTimeOf(timeline: PoseTimeline, slot: number): number {
|
|
56
|
-
const index = timeline.findIndex(entry => entry.slot === slot);
|
|
57
|
-
return index < 0 ? 0 : poseKnots(timeline)[index];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/** Septic: zero first, second AND third derivative at both ends, so a one-shot
|
|
61
|
-
* move away from rest and back to rest is G3 at the moment it starts and the
|
|
62
|
-
* moment it settles. A cubic smoothstep leaves a jerk step at both. */
|
|
63
|
-
export function smoothStep(t: number): number {
|
|
64
|
-
const x = Math.min(1, Math.max(0, t));
|
|
65
|
-
return x ** 4 * (35 + x * (-84 + x * (70 - 20 * x)));
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export interface PoseSpline {
|
|
69
|
-
readonly slots: readonly number[];
|
|
70
|
-
readonly loop: number;
|
|
71
|
-
/** How much each pose owns of the frame at this moment; sums to one, and is
|
|
72
|
-
* one on a pose exactly at its own moment. */
|
|
73
|
-
weightsAt(time: number): number[];
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** A periodic C2 interpolant of the authored poses. Its weights are shared by
|
|
77
|
-
* rig playback, GLB clip mixing and trajectory sampling, so the visible path
|
|
78
|
-
* is the path the animation actually follows. */
|
|
79
|
-
export function preparePoseSpline(timeline: PoseTimeline): PoseSpline {
|
|
80
|
-
const n = timeline.length, slots = timeline.map(entry => entry.slot);
|
|
81
|
-
const loop = poseLoopSeconds(timeline), knots = poseKnots(timeline);
|
|
82
|
-
if (n < 2 || !(loop > 0)) {
|
|
83
|
-
const fixed = timeline.map((_, i) => (i === 0 ? 1 : 0));
|
|
84
|
-
return { slots, loop: Math.max(loop, 0), weightsAt: () => [...fixed] };
|
|
85
|
-
}
|
|
86
|
-
return { slots, loop, weightsAt: time => splineWeights(knots,loop,n,time) };
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function splineWeights(knots: readonly number[], loop: number, n: number, time: number): number[] {
|
|
90
|
-
const cursor = ((time % loop) + loop) % loop, weights = new Array<number>(n).fill(0);
|
|
91
|
-
let k = n - 1; while (k > 0 && cursor < knots[k]) k--;
|
|
92
|
-
const start = knots[k], end = k + 1 < n ? knots[k + 1] : loop;
|
|
93
|
-
const h=end-start,t=h>0?(cursor-start)/h:0,next=(k+1)%n;
|
|
94
|
-
if(n<3||!(h>0)){weights[k]=1-smoothStep(t);weights[next]+=smoothStep(t);return weights;}
|
|
95
|
-
const coordinate=(i:number)=>i<0?knots[i+n]-loop:i>=n?knots[i-n]+loop:knots[i];
|
|
96
|
-
const unit=(i:number)=>{const result=new Array<number>(n).fill(0);result[(i+n)%n]=1;return result;};
|
|
97
|
-
const subtract=(a:readonly number[],b:readonly number[],scale:number)=>a.map((v,j)=>(v-b[j])/scale);
|
|
98
|
-
const derivatives=(i:number)=>{
|
|
99
|
-
const before=coordinate(i)-coordinate(i-1),after=coordinate(i+1)-coordinate(i);
|
|
100
|
-
const previous=subtract(unit(i),unit(i-1),Math.max(before,1e-8));
|
|
101
|
-
const following=subtract(unit(i+1),unit(i),Math.max(after,1e-8));
|
|
102
|
-
// Full centered tangents round a three-pose corner visibly. Reducing them
|
|
103
|
-
// to a quarter left a mathematically C2 but visibly triangular path.
|
|
104
|
-
return {velocity:previous.map((v,j)=>(v*after+following[j]*before)/(before+after)),
|
|
105
|
-
acceleration:previous.map((v,j)=>2*(following[j]-v)/(before+after))};
|
|
106
|
-
};
|
|
107
|
-
const a=derivatives(k),b=derivatives(k+1),p=unit(k),q=unit(k+1);
|
|
108
|
-
for(let j=0;j<n;j++){
|
|
109
|
-
const v0=h*a.velocity[j],v1=h*b.velocity[j],a0=h*h*a.acceleration[j],a1=h*h*b.acceleration[j];
|
|
110
|
-
const d=q[j]-p[j]-v0-a0/2,v=v1-v0-a0,acc=a1-a0;
|
|
111
|
-
weights[j]=p[j]+t*(v0+t*(a0/2+t*((10*d-4*v+acc/2)+t*((-15*d+7*v-acc)+t*(6*d-3*v+acc/2)))));
|
|
112
|
-
}
|
|
113
|
-
return weights;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const conjugate = (q: Rotation): Rotation => [-q[0], -q[1], -q[2], q[3]];
|
|
117
|
-
const multiply = (a: Rotation, b: Rotation): Rotation => [
|
|
118
|
-
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
|
|
119
|
-
a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
|
|
120
|
-
a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
|
|
121
|
-
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2],
|
|
122
|
-
];
|
|
123
|
-
function rotate(q: Rotation, v: Point): Point {
|
|
124
|
-
const tx = 2 * (q[1] * v[2] - q[2] * v[1]), ty = 2 * (q[2] * v[0] - q[0] * v[2]), tz = 2 * (q[0] * v[1] - q[1] * v[0]);
|
|
125
|
-
return [v[0] + q[3] * tx + (q[1] * tz - q[2] * ty), v[1] + q[3] * ty + (q[2] * tx - q[0] * tz), v[2] + q[3] * tz + (q[0] * ty - q[1] * tx)];
|
|
126
|
-
}
|
|
127
|
-
const safeScale = (v: number) => (Math.abs(v) > 1e-8 ? v : 1e-8);
|
|
128
|
-
|
|
129
|
-
/** A pose in its parent's frame. */
|
|
130
|
-
function localPose(pose: PinPose, parent: PinPose | undefined): PinPose {
|
|
131
|
-
if (!parent) return pose;
|
|
132
|
-
const inverse = conjugate(parent.rotation);
|
|
133
|
-
const offset = rotate(inverse, [pose.position[0] - parent.position[0], pose.position[1] - parent.position[1], pose.position[2] - parent.position[2]]);
|
|
134
|
-
return {
|
|
135
|
-
position: [offset[0] / safeScale(parent.scale[0]), offset[1] / safeScale(parent.scale[1]), offset[2] / safeScale(parent.scale[2])],
|
|
136
|
-
rotation: multiply(inverse, pose.rotation),
|
|
137
|
-
scale: [pose.scale[0] / safeScale(parent.scale[0]), pose.scale[1] / safeScale(parent.scale[1]), pose.scale[2] / safeScale(parent.scale[2])],
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/** The inverse of localPose: a local transform read back out into the world. */
|
|
142
|
-
function worldPose(local: PinPose, parent: PinPose | undefined): PinPose {
|
|
143
|
-
if (!parent) return local;
|
|
144
|
-
const scaled: Point = [local.position[0] * parent.scale[0], local.position[1] * parent.scale[1], local.position[2] * parent.scale[2]];
|
|
145
|
-
const offset = rotate(parent.rotation, scaled);
|
|
146
|
-
return {
|
|
147
|
-
position: [parent.position[0] + offset[0], parent.position[1] + offset[1], parent.position[2] + offset[2]],
|
|
148
|
-
rotation: multiply(parent.rotation, local.rotation),
|
|
149
|
-
scale: [local.scale[0] * parent.scale[0], local.scale[1] * parent.scale[1], local.scale[2] * parent.scale[2]],
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/** Parents before children, with any cycle in a derived hierarchy broken by
|
|
154
|
-
* treating the pin that closes it as a root. */
|
|
155
|
-
function hierarchyOrder(pins: readonly PinRecord[], parents: PinParents): { order: number[]; parentOf: (number | undefined)[] } {
|
|
156
|
-
const index = new Map(pins.map((pin, i) => [pin.id, i]));
|
|
157
|
-
const parentOf = pins.map(pin => { const p = index.get(parents.get(pin.id) ?? ''); return p === index.get(pin.id) ? undefined : p; });
|
|
158
|
-
const order: number[] = [], state = new Uint8Array(pins.length);
|
|
159
|
-
const visit = (i: number) => {
|
|
160
|
-
if (state[i]) { if (state[i] === 1) parentOf[i] = undefined; return; }
|
|
161
|
-
state[i] = 1;
|
|
162
|
-
const parent = parentOf[i];
|
|
163
|
-
if (parent !== undefined) visit(parent);
|
|
164
|
-
state[i] = 2; order.push(i);
|
|
165
|
-
};
|
|
166
|
-
for (let i = 0; i < pins.length; i++) visit(i);
|
|
167
|
-
return { order, parentOf };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
/** The active pair shares one hemisphere, including the last-to-first seam. */
|
|
172
|
-
export function blendRotations(rotations: readonly Rotation[], weights: readonly number[]): Rotation {
|
|
173
|
-
let [x, y, z, w] = [0, 0, 0, 0];
|
|
174
|
-
const reference=rotations[weights.findIndex(weight=>weight>1e-12)]??rotations[0];
|
|
175
|
-
let dominant = reference, best = -Infinity;
|
|
176
|
-
for (let i = 0; i < rotations.length; i++) {
|
|
177
|
-
const q = rotations[i];
|
|
178
|
-
if(weights[i]===0)continue;
|
|
179
|
-
const aligned: Rotation = q[0] * reference[0] + q[1] * reference[1] + q[2] * reference[2] + q[3] * reference[3] < 0
|
|
180
|
-
? [-q[0], -q[1], -q[2], -q[3]] : q;
|
|
181
|
-
if (weights[i] > best) { best = weights[i]; dominant = aligned; }
|
|
182
|
-
x += aligned[0] * weights[i]; y += aligned[1] * weights[i];
|
|
183
|
-
z += aligned[2] * weights[i]; w += aligned[3] * weights[i];
|
|
184
|
-
}
|
|
185
|
-
const length = Math.hypot(x, y, z, w);
|
|
186
|
-
// Opposed rotations at equal weight cancel and name no orientation at all.
|
|
187
|
-
return length > 1e-6 ? [x / length, y / length, z / length, w / length] : dominant;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Blends by id, so a pin missing from either side simply keeps its own pose —
|
|
191
|
-
* it appears on both sides of the blend and cannot move. Everything but the
|
|
192
|
-
* transform comes from the destination: ids, rest positions and flags are
|
|
193
|
-
* shared between slots and are not animated. */
|
|
194
|
-
export function blendPins(from: readonly PinRecord[], to: readonly PinRecord[], t: number, parents: PinParents = new Map()): readonly PinRecord[] {
|
|
195
|
-
if (t <= 0 && from.length === to.length && to.every((pin, i) => from[i]?.id === pin.id)) return from;
|
|
196
|
-
const aligned = to.map(pin => from.find(other => other.id === pin.id) ?? pin);
|
|
197
|
-
return blendPoses(to, [aligned, to], [1 - t, t], parents);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** Every pose at once, by weight. `poses` are the same pins in the same order
|
|
201
|
-
* as `base` — they all come from resolving one document — so this walks them by
|
|
202
|
-
* index rather than searching per pin per frame.
|
|
203
|
-
*
|
|
204
|
-
* The blend happens in each pin's PARENT frame, never in world space. A chain
|
|
205
|
-
* posed two ways differs mostly in its joints' rotations, and averaging world
|
|
206
|
-
* positions cuts the chord between them: the limb shortens in the middle of
|
|
207
|
-
* every transition and lengthens again at each pose. In the parent's frame only
|
|
208
|
-
* the joint's own rotation and offset move, so the chain keeps its shape, and a
|
|
209
|
-
* pin's transform stays relative to whatever IK did to its parent — which is
|
|
210
|
-
* what makes moving an IK target interpolate as one arc rather than dragging
|
|
211
|
-
* every pin along a straight line of its own. */
|
|
212
|
-
export function blendPoses(base: readonly PinRecord[], poses: readonly (readonly PinRecord[])[], weights: readonly number[], parents: PinParents = new Map()): readonly PinRecord[] {
|
|
213
|
-
if (!poses.length || poses.some(pose => pose.length !== base.length)) return base;
|
|
214
|
-
const { order, parentOf } = hierarchyOrder(base, parents);
|
|
215
|
-
const world = new Array<PinPose>(base.length), blended = new Array<PinRecord>(base.length);
|
|
216
|
-
for (const i of order) {
|
|
217
|
-
const parent = parentOf[i], parentWorld = parent === undefined ? undefined : world[parent];
|
|
218
|
-
const locals = poses.map((pose, k) => localPose(pose[i].pose, parent === undefined ? undefined : poses[k][parent].pose));
|
|
219
|
-
world[i] = worldPose(weightedPose(locals, weights), parentWorld);
|
|
220
|
-
const pin: PinRecord = { ...base[i], pose: world[i] };
|
|
221
|
-
// A goal is an anchor in the world, not a point on its parent. A planted
|
|
222
|
-
// foot has the same goal in every pose, and must come out of the blend as
|
|
223
|
-
// that goal exactly however the body above it moves.
|
|
224
|
-
if (!poses.every(pose => pose[i].ikTarget)) { blended[i] = pin; continue; }
|
|
225
|
-
const targets = poses.map(pose => pose[i].ikTarget!);
|
|
226
|
-
blended[i] = { ...pin, ikTarget: {
|
|
227
|
-
position: [0, 1, 2].map(axis => targets.reduce((sum, target, k) => sum + target.position[axis] * weights[k], 0)) as unknown as Point,
|
|
228
|
-
rotation: blendRotations(targets.map(target => target.rotation), weights),
|
|
229
|
-
} };
|
|
230
|
-
}
|
|
231
|
-
return blended;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/** One pose out of many, by weight. */
|
|
235
|
-
function weightedPose(poses: readonly PinPose[], weights: readonly number[]): PinPose {
|
|
236
|
-
const position: [number, number, number] = [0, 0, 0], scale: [number, number, number] = [0, 0, 0];
|
|
237
|
-
for (let k = 0; k < poses.length; k++) for (let axis = 0; axis < 3; axis++) {
|
|
238
|
-
position[axis] += poses[k].position[axis] * weights[k];
|
|
239
|
-
scale[axis] += poses[k].scale[axis] * weights[k];
|
|
240
|
-
}
|
|
241
|
-
return { position, rotation: blendRotations(poses.map(pose => pose.rotation), weights), scale };
|
|
242
|
-
}
|
|
1
|
+
import type { PinPose, PinRecord, Point, Rotation } from './pin-types.js';
|
|
2
|
+
import type { PinParents } from './pin-types.js';
|
|
3
|
+
|
|
4
|
+
/** A pose slot's time controls its share of the loop around its own moment.
|
|
5
|
+
* The transition settles at each authored pose without holding a flat frame. */
|
|
6
|
+
export const DEFAULT_POSE_SECONDS = 0.7;
|
|
7
|
+
export const MIN_POSE_SECONDS = 0.05;
|
|
8
|
+
export const MAX_POSE_SECONDS = 60;
|
|
9
|
+
/** Travel kept below the longest pose so a slider dragged to the bottom can
|
|
10
|
+
* always be dragged further on the next pass, and shrinks back when shortened. */
|
|
11
|
+
export const POSE_TIME_HEADROOM = 0.5;
|
|
12
|
+
|
|
13
|
+
export type PoseTiming = { readonly slot: number; readonly seconds: number };
|
|
14
|
+
export type PoseTimeline = readonly PoseTiming[];
|
|
15
|
+
|
|
16
|
+
export function poseTimeScale(timeline: PoseTimeline): number {
|
|
17
|
+
return Math.max(DEFAULT_POSE_SECONDS, ...timeline.map(entry => entry.seconds)) + POSE_TIME_HEADROOM;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** The strip reads downwards: zero at the top, the scale at the bottom, which
|
|
21
|
+
* is what makes "drag down" mean "stay longer". */
|
|
22
|
+
export function poseSecondsAt(offset: number, height: number, scale: number): number {
|
|
23
|
+
if (!(height > 0)) return MIN_POSE_SECONDS;
|
|
24
|
+
return Math.min(1, Math.max(0, offset / height)) * scale;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** How far down the strip a time sits, as a percentage for the fill and handle. */
|
|
28
|
+
export function poseTimeOffset(seconds: number, scale: number): number {
|
|
29
|
+
if (!(scale > 0)) return 0;
|
|
30
|
+
return Math.min(100, Math.max(0, (seconds / scale) * 100));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function poseLoopSeconds(timeline: PoseTimeline): number {
|
|
34
|
+
return timeline.reduce((total, entry) => total + Math.max(0, entry.seconds), 0);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The moment each pose is reached. Neighbours split the gap between them in
|
|
38
|
+
* proportion to their own times, which is what makes a time an ownership share
|
|
39
|
+
* rather than the length of one arc. */
|
|
40
|
+
export function poseKnots(timeline: PoseTimeline): number[] {
|
|
41
|
+
const knots = [0];
|
|
42
|
+
for (let i = 1; i < timeline.length; i++) {
|
|
43
|
+
knots.push(knots[i - 1] + (Math.max(0, timeline[i - 1].seconds) + Math.max(0, timeline[i].seconds)) / 2);
|
|
44
|
+
}
|
|
45
|
+
return knots;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** How long one pass takes: the first pose's moment to the last one's.
|
|
49
|
+
* Looping playback adds the closing transition up to poseLoopSeconds. */
|
|
50
|
+
export function posePassSeconds(timeline: PoseTimeline): number {
|
|
51
|
+
const knots = poseKnots(timeline);
|
|
52
|
+
return knots.length ? knots[knots.length - 1] : 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function poseTimeOf(timeline: PoseTimeline, slot: number): number {
|
|
56
|
+
const index = timeline.findIndex(entry => entry.slot === slot);
|
|
57
|
+
return index < 0 ? 0 : poseKnots(timeline)[index];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Septic: zero first, second AND third derivative at both ends, so a one-shot
|
|
61
|
+
* move away from rest and back to rest is G3 at the moment it starts and the
|
|
62
|
+
* moment it settles. A cubic smoothstep leaves a jerk step at both. */
|
|
63
|
+
export function smoothStep(t: number): number {
|
|
64
|
+
const x = Math.min(1, Math.max(0, t));
|
|
65
|
+
return x ** 4 * (35 + x * (-84 + x * (70 - 20 * x)));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface PoseSpline {
|
|
69
|
+
readonly slots: readonly number[];
|
|
70
|
+
readonly loop: number;
|
|
71
|
+
/** How much each pose owns of the frame at this moment; sums to one, and is
|
|
72
|
+
* one on a pose exactly at its own moment. */
|
|
73
|
+
weightsAt(time: number): number[];
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A periodic C2 interpolant of the authored poses. Its weights are shared by
|
|
77
|
+
* rig playback, GLB clip mixing and trajectory sampling, so the visible path
|
|
78
|
+
* is the path the animation actually follows. */
|
|
79
|
+
export function preparePoseSpline(timeline: PoseTimeline): PoseSpline {
|
|
80
|
+
const n = timeline.length, slots = timeline.map(entry => entry.slot);
|
|
81
|
+
const loop = poseLoopSeconds(timeline), knots = poseKnots(timeline);
|
|
82
|
+
if (n < 2 || !(loop > 0)) {
|
|
83
|
+
const fixed = timeline.map((_, i) => (i === 0 ? 1 : 0));
|
|
84
|
+
return { slots, loop: Math.max(loop, 0), weightsAt: () => [...fixed] };
|
|
85
|
+
}
|
|
86
|
+
return { slots, loop, weightsAt: time => splineWeights(knots,loop,n,time) };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function splineWeights(knots: readonly number[], loop: number, n: number, time: number): number[] {
|
|
90
|
+
const cursor = ((time % loop) + loop) % loop, weights = new Array<number>(n).fill(0);
|
|
91
|
+
let k = n - 1; while (k > 0 && cursor < knots[k]) k--;
|
|
92
|
+
const start = knots[k], end = k + 1 < n ? knots[k + 1] : loop;
|
|
93
|
+
const h=end-start,t=h>0?(cursor-start)/h:0,next=(k+1)%n;
|
|
94
|
+
if(n<3||!(h>0)){weights[k]=1-smoothStep(t);weights[next]+=smoothStep(t);return weights;}
|
|
95
|
+
const coordinate=(i:number)=>i<0?knots[i+n]-loop:i>=n?knots[i-n]+loop:knots[i];
|
|
96
|
+
const unit=(i:number)=>{const result=new Array<number>(n).fill(0);result[(i+n)%n]=1;return result;};
|
|
97
|
+
const subtract=(a:readonly number[],b:readonly number[],scale:number)=>a.map((v,j)=>(v-b[j])/scale);
|
|
98
|
+
const derivatives=(i:number)=>{
|
|
99
|
+
const before=coordinate(i)-coordinate(i-1),after=coordinate(i+1)-coordinate(i);
|
|
100
|
+
const previous=subtract(unit(i),unit(i-1),Math.max(before,1e-8));
|
|
101
|
+
const following=subtract(unit(i+1),unit(i),Math.max(after,1e-8));
|
|
102
|
+
// Full centered tangents round a three-pose corner visibly. Reducing them
|
|
103
|
+
// to a quarter left a mathematically C2 but visibly triangular path.
|
|
104
|
+
return {velocity:previous.map((v,j)=>(v*after+following[j]*before)/(before+after)),
|
|
105
|
+
acceleration:previous.map((v,j)=>2*(following[j]-v)/(before+after))};
|
|
106
|
+
};
|
|
107
|
+
const a=derivatives(k),b=derivatives(k+1),p=unit(k),q=unit(k+1);
|
|
108
|
+
for(let j=0;j<n;j++){
|
|
109
|
+
const v0=h*a.velocity[j],v1=h*b.velocity[j],a0=h*h*a.acceleration[j],a1=h*h*b.acceleration[j];
|
|
110
|
+
const d=q[j]-p[j]-v0-a0/2,v=v1-v0-a0,acc=a1-a0;
|
|
111
|
+
weights[j]=p[j]+t*(v0+t*(a0/2+t*((10*d-4*v+acc/2)+t*((-15*d+7*v-acc)+t*(6*d-3*v+acc/2)))));
|
|
112
|
+
}
|
|
113
|
+
return weights;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const conjugate = (q: Rotation): Rotation => [-q[0], -q[1], -q[2], q[3]];
|
|
117
|
+
const multiply = (a: Rotation, b: Rotation): Rotation => [
|
|
118
|
+
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
|
|
119
|
+
a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
|
|
120
|
+
a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
|
|
121
|
+
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2],
|
|
122
|
+
];
|
|
123
|
+
function rotate(q: Rotation, v: Point): Point {
|
|
124
|
+
const tx = 2 * (q[1] * v[2] - q[2] * v[1]), ty = 2 * (q[2] * v[0] - q[0] * v[2]), tz = 2 * (q[0] * v[1] - q[1] * v[0]);
|
|
125
|
+
return [v[0] + q[3] * tx + (q[1] * tz - q[2] * ty), v[1] + q[3] * ty + (q[2] * tx - q[0] * tz), v[2] + q[3] * tz + (q[0] * ty - q[1] * tx)];
|
|
126
|
+
}
|
|
127
|
+
const safeScale = (v: number) => (Math.abs(v) > 1e-8 ? v : 1e-8);
|
|
128
|
+
|
|
129
|
+
/** A pose in its parent's frame. */
|
|
130
|
+
function localPose(pose: PinPose, parent: PinPose | undefined): PinPose {
|
|
131
|
+
if (!parent) return pose;
|
|
132
|
+
const inverse = conjugate(parent.rotation);
|
|
133
|
+
const offset = rotate(inverse, [pose.position[0] - parent.position[0], pose.position[1] - parent.position[1], pose.position[2] - parent.position[2]]);
|
|
134
|
+
return {
|
|
135
|
+
position: [offset[0] / safeScale(parent.scale[0]), offset[1] / safeScale(parent.scale[1]), offset[2] / safeScale(parent.scale[2])],
|
|
136
|
+
rotation: multiply(inverse, pose.rotation),
|
|
137
|
+
scale: [pose.scale[0] / safeScale(parent.scale[0]), pose.scale[1] / safeScale(parent.scale[1]), pose.scale[2] / safeScale(parent.scale[2])],
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The inverse of localPose: a local transform read back out into the world. */
|
|
142
|
+
function worldPose(local: PinPose, parent: PinPose | undefined): PinPose {
|
|
143
|
+
if (!parent) return local;
|
|
144
|
+
const scaled: Point = [local.position[0] * parent.scale[0], local.position[1] * parent.scale[1], local.position[2] * parent.scale[2]];
|
|
145
|
+
const offset = rotate(parent.rotation, scaled);
|
|
146
|
+
return {
|
|
147
|
+
position: [parent.position[0] + offset[0], parent.position[1] + offset[1], parent.position[2] + offset[2]],
|
|
148
|
+
rotation: multiply(parent.rotation, local.rotation),
|
|
149
|
+
scale: [local.scale[0] * parent.scale[0], local.scale[1] * parent.scale[1], local.scale[2] * parent.scale[2]],
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Parents before children, with any cycle in a derived hierarchy broken by
|
|
154
|
+
* treating the pin that closes it as a root. */
|
|
155
|
+
function hierarchyOrder(pins: readonly PinRecord[], parents: PinParents): { order: number[]; parentOf: (number | undefined)[] } {
|
|
156
|
+
const index = new Map(pins.map((pin, i) => [pin.id, i]));
|
|
157
|
+
const parentOf = pins.map(pin => { const p = index.get(parents.get(pin.id) ?? ''); return p === index.get(pin.id) ? undefined : p; });
|
|
158
|
+
const order: number[] = [], state = new Uint8Array(pins.length);
|
|
159
|
+
const visit = (i: number) => {
|
|
160
|
+
if (state[i]) { if (state[i] === 1) parentOf[i] = undefined; return; }
|
|
161
|
+
state[i] = 1;
|
|
162
|
+
const parent = parentOf[i];
|
|
163
|
+
if (parent !== undefined) visit(parent);
|
|
164
|
+
state[i] = 2; order.push(i);
|
|
165
|
+
};
|
|
166
|
+
for (let i = 0; i < pins.length; i++) visit(i);
|
|
167
|
+
return { order, parentOf };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
/** The active pair shares one hemisphere, including the last-to-first seam. */
|
|
172
|
+
export function blendRotations(rotations: readonly Rotation[], weights: readonly number[]): Rotation {
|
|
173
|
+
let [x, y, z, w] = [0, 0, 0, 0];
|
|
174
|
+
const reference=rotations[weights.findIndex(weight=>weight>1e-12)]??rotations[0];
|
|
175
|
+
let dominant = reference, best = -Infinity;
|
|
176
|
+
for (let i = 0; i < rotations.length; i++) {
|
|
177
|
+
const q = rotations[i];
|
|
178
|
+
if(weights[i]===0)continue;
|
|
179
|
+
const aligned: Rotation = q[0] * reference[0] + q[1] * reference[1] + q[2] * reference[2] + q[3] * reference[3] < 0
|
|
180
|
+
? [-q[0], -q[1], -q[2], -q[3]] : q;
|
|
181
|
+
if (weights[i] > best) { best = weights[i]; dominant = aligned; }
|
|
182
|
+
x += aligned[0] * weights[i]; y += aligned[1] * weights[i];
|
|
183
|
+
z += aligned[2] * weights[i]; w += aligned[3] * weights[i];
|
|
184
|
+
}
|
|
185
|
+
const length = Math.hypot(x, y, z, w);
|
|
186
|
+
// Opposed rotations at equal weight cancel and name no orientation at all.
|
|
187
|
+
return length > 1e-6 ? [x / length, y / length, z / length, w / length] : dominant;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Blends by id, so a pin missing from either side simply keeps its own pose —
|
|
191
|
+
* it appears on both sides of the blend and cannot move. Everything but the
|
|
192
|
+
* transform comes from the destination: ids, rest positions and flags are
|
|
193
|
+
* shared between slots and are not animated. */
|
|
194
|
+
export function blendPins(from: readonly PinRecord[], to: readonly PinRecord[], t: number, parents: PinParents = new Map()): readonly PinRecord[] {
|
|
195
|
+
if (t <= 0 && from.length === to.length && to.every((pin, i) => from[i]?.id === pin.id)) return from;
|
|
196
|
+
const aligned = to.map(pin => from.find(other => other.id === pin.id) ?? pin);
|
|
197
|
+
return blendPoses(to, [aligned, to], [1 - t, t], parents);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Every pose at once, by weight. `poses` are the same pins in the same order
|
|
201
|
+
* as `base` — they all come from resolving one document — so this walks them by
|
|
202
|
+
* index rather than searching per pin per frame.
|
|
203
|
+
*
|
|
204
|
+
* The blend happens in each pin's PARENT frame, never in world space. A chain
|
|
205
|
+
* posed two ways differs mostly in its joints' rotations, and averaging world
|
|
206
|
+
* positions cuts the chord between them: the limb shortens in the middle of
|
|
207
|
+
* every transition and lengthens again at each pose. In the parent's frame only
|
|
208
|
+
* the joint's own rotation and offset move, so the chain keeps its shape, and a
|
|
209
|
+
* pin's transform stays relative to whatever IK did to its parent — which is
|
|
210
|
+
* what makes moving an IK target interpolate as one arc rather than dragging
|
|
211
|
+
* every pin along a straight line of its own. */
|
|
212
|
+
export function blendPoses(base: readonly PinRecord[], poses: readonly (readonly PinRecord[])[], weights: readonly number[], parents: PinParents = new Map()): readonly PinRecord[] {
|
|
213
|
+
if (!poses.length || poses.some(pose => pose.length !== base.length)) return base;
|
|
214
|
+
const { order, parentOf } = hierarchyOrder(base, parents);
|
|
215
|
+
const world = new Array<PinPose>(base.length), blended = new Array<PinRecord>(base.length);
|
|
216
|
+
for (const i of order) {
|
|
217
|
+
const parent = parentOf[i], parentWorld = parent === undefined ? undefined : world[parent];
|
|
218
|
+
const locals = poses.map((pose, k) => localPose(pose[i].pose, parent === undefined ? undefined : poses[k][parent].pose));
|
|
219
|
+
world[i] = worldPose(weightedPose(locals, weights), parentWorld);
|
|
220
|
+
const pin: PinRecord = { ...base[i], pose: world[i] };
|
|
221
|
+
// A goal is an anchor in the world, not a point on its parent. A planted
|
|
222
|
+
// foot has the same goal in every pose, and must come out of the blend as
|
|
223
|
+
// that goal exactly however the body above it moves.
|
|
224
|
+
if (!poses.every(pose => pose[i].ikTarget)) { blended[i] = pin; continue; }
|
|
225
|
+
const targets = poses.map(pose => pose[i].ikTarget!);
|
|
226
|
+
blended[i] = { ...pin, ikTarget: {
|
|
227
|
+
position: [0, 1, 2].map(axis => targets.reduce((sum, target, k) => sum + target.position[axis] * weights[k], 0)) as unknown as Point,
|
|
228
|
+
rotation: blendRotations(targets.map(target => target.rotation), weights),
|
|
229
|
+
} };
|
|
230
|
+
}
|
|
231
|
+
return blended;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** One pose out of many, by weight. */
|
|
235
|
+
function weightedPose(poses: readonly PinPose[], weights: readonly number[]): PinPose {
|
|
236
|
+
const position: [number, number, number] = [0, 0, 0], scale: [number, number, number] = [0, 0, 0];
|
|
237
|
+
for (let k = 0; k < poses.length; k++) for (let axis = 0; axis < 3; axis++) {
|
|
238
|
+
position[axis] += poses[k].position[axis] * weights[k];
|
|
239
|
+
scale[axis] += poses[k].scale[axis] * weights[k];
|
|
240
|
+
}
|
|
241
|
+
return { position, rotation: blendRotations(poses.map(pose => pose.rotation), weights), scale };
|
|
242
|
+
}
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { Matrix4, Quaternion, Vector3 } from 'three';
|
|
2
|
-
import type { PinPose, PinRecord } from './pin-types.js';
|
|
3
|
-
export interface BoneDriver { id:string; influences:readonly {id:string;weight:number}[] }
|
|
4
|
-
const matrix=(pose:PinPose)=>new Matrix4().compose(new Vector3().fromArray(pose.position),new Quaternion().fromArray(pose.rotation),new Vector3().fromArray(pose.scale));
|
|
5
|
-
/** Drivers reference actual fitted bones, never hidden authoring nodes. The
|
|
6
|
-
* driven bone's reference frame remains independently FK editable. */
|
|
7
|
-
export function driverDelta(driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]){
|
|
8
|
-
const affine=new Matrix4();affine.elements.fill(0);
|
|
9
|
-
const rotation=new Quaternion(0,0,0,0),scale=new Vector3();let hemisphere:Quaternion|undefined;
|
|
10
|
-
for(const influence of driver.influences){
|
|
11
|
-
const current=pins.find(p=>p.id===influence.id)!.pose,rest=reference.find(p=>p.id===influence.id)!.pose,w=influence.weight;
|
|
12
|
-
const delta=matrix(current).multiply(matrix(rest).invert()),q=new Quaternion().fromArray(current.rotation).multiply(new Quaternion().fromArray(rest.rotation).invert());
|
|
13
|
-
hemisphere??=q;const sign=hemisphere.dot(q)<0?-w:w;
|
|
14
|
-
delta.elements.forEach((value,i)=>affine.elements[i]+=value*w);
|
|
15
|
-
rotation.set(rotation.x+q.x*sign,rotation.y+q.y*sign,rotation.z+q.z*sign,rotation.w+q.w*sign);
|
|
16
|
-
scale.addScaledVector(new Vector3().fromArray(current.scale).divide(new Vector3().fromArray(rest.scale)),w);
|
|
17
|
-
}
|
|
18
|
-
if(!driver.influences.length){affine.identity();rotation.identity();scale.setScalar(1);}
|
|
19
|
-
return {affine,rotation:rotation.normalize(),scale};
|
|
20
|
-
}
|
|
21
|
-
export function drivenPose(pose:PinPose,driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]):PinPose{
|
|
22
|
-
const delta=driverDelta(driver,pins,reference);
|
|
23
|
-
return {position:new Vector3().fromArray(pose.position).applyMatrix4(delta.affine).toArray(),rotation:delta.rotation.multiply(new Quaternion().fromArray(pose.rotation)).normalize().toArray(),scale:delta.scale.multiply(new Vector3().fromArray(pose.scale)).toArray()};
|
|
24
|
-
}
|
|
25
|
-
export function referencePose(pose:PinPose,driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]):PinPose{
|
|
26
|
-
const delta=driverDelta(driver,pins,reference);
|
|
27
|
-
if(Math.abs(delta.affine.determinant())<1e-10)throw new Error('This blended bone frame is singular. Adjust its driving bones before editing its position.');
|
|
28
|
-
return {position:new Vector3().fromArray(pose.position).applyMatrix4(delta.affine.invert()).toArray(),rotation:delta.rotation.invert().multiply(new Quaternion().fromArray(pose.rotation)).normalize().toArray(),scale:new Vector3().fromArray(pose.scale).divide(delta.scale).toArray()};
|
|
29
|
-
}
|
|
30
|
-
export function driveRig(pins:readonly PinRecord[],reference:readonly PinRecord[],drivers:readonly BoneDriver[]){
|
|
31
|
-
return pins.map(pin=>{const driver=drivers.find(d=>d.id===pin.id);return driver?{...pin,pose:drivenPose(pin.pose,driver,pins,reference)}:pin;});
|
|
32
|
-
}
|
|
1
|
+
import { Matrix4, Quaternion, Vector3 } from 'three';
|
|
2
|
+
import type { PinPose, PinRecord } from './pin-types.js';
|
|
3
|
+
export interface BoneDriver { id:string; influences:readonly {id:string;weight:number}[] }
|
|
4
|
+
const matrix=(pose:PinPose)=>new Matrix4().compose(new Vector3().fromArray(pose.position),new Quaternion().fromArray(pose.rotation),new Vector3().fromArray(pose.scale));
|
|
5
|
+
/** Drivers reference actual fitted bones, never hidden authoring nodes. The
|
|
6
|
+
* driven bone's reference frame remains independently FK editable. */
|
|
7
|
+
export function driverDelta(driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]){
|
|
8
|
+
const affine=new Matrix4();affine.elements.fill(0);
|
|
9
|
+
const rotation=new Quaternion(0,0,0,0),scale=new Vector3();let hemisphere:Quaternion|undefined;
|
|
10
|
+
for(const influence of driver.influences){
|
|
11
|
+
const current=pins.find(p=>p.id===influence.id)!.pose,rest=reference.find(p=>p.id===influence.id)!.pose,w=influence.weight;
|
|
12
|
+
const delta=matrix(current).multiply(matrix(rest).invert()),q=new Quaternion().fromArray(current.rotation).multiply(new Quaternion().fromArray(rest.rotation).invert());
|
|
13
|
+
hemisphere??=q;const sign=hemisphere.dot(q)<0?-w:w;
|
|
14
|
+
delta.elements.forEach((value,i)=>affine.elements[i]+=value*w);
|
|
15
|
+
rotation.set(rotation.x+q.x*sign,rotation.y+q.y*sign,rotation.z+q.z*sign,rotation.w+q.w*sign);
|
|
16
|
+
scale.addScaledVector(new Vector3().fromArray(current.scale).divide(new Vector3().fromArray(rest.scale)),w);
|
|
17
|
+
}
|
|
18
|
+
if(!driver.influences.length){affine.identity();rotation.identity();scale.setScalar(1);}
|
|
19
|
+
return {affine,rotation:rotation.normalize(),scale};
|
|
20
|
+
}
|
|
21
|
+
export function drivenPose(pose:PinPose,driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]):PinPose{
|
|
22
|
+
const delta=driverDelta(driver,pins,reference);
|
|
23
|
+
return {position:new Vector3().fromArray(pose.position).applyMatrix4(delta.affine).toArray(),rotation:delta.rotation.multiply(new Quaternion().fromArray(pose.rotation)).normalize().toArray(),scale:delta.scale.multiply(new Vector3().fromArray(pose.scale)).toArray()};
|
|
24
|
+
}
|
|
25
|
+
export function referencePose(pose:PinPose,driver:BoneDriver,pins:readonly PinRecord[],reference:readonly PinRecord[]):PinPose{
|
|
26
|
+
const delta=driverDelta(driver,pins,reference);
|
|
27
|
+
if(Math.abs(delta.affine.determinant())<1e-10)throw new Error('This blended bone frame is singular. Adjust its driving bones before editing its position.');
|
|
28
|
+
return {position:new Vector3().fromArray(pose.position).applyMatrix4(delta.affine.invert()).toArray(),rotation:delta.rotation.invert().multiply(new Quaternion().fromArray(pose.rotation)).normalize().toArray(),scale:new Vector3().fromArray(pose.scale).divide(delta.scale).toArray()};
|
|
29
|
+
}
|
|
30
|
+
export function driveRig(pins:readonly PinRecord[],reference:readonly PinRecord[],drivers:readonly BoneDriver[]){
|
|
31
|
+
return pins.map(pin=>{const driver=drivers.find(d=>d.id===pin.id);return driver?{...pin,pose:drivenPose(pin.pose,driver,pins,reference)}:pin;});
|
|
32
|
+
}
|