@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,22 +1,22 @@
|
|
|
1
|
-
import type { IKConstraint } from './ik-constraints.js';
|
|
2
|
-
import type { MotionSettings } from './motion-settings.js';
|
|
3
|
-
|
|
4
|
-
/** Shared data contract; deliberately independent of editor history and GPU types. */
|
|
5
|
-
export type Point = readonly [number, number, number];
|
|
6
|
-
export type Rotation = readonly [number, number, number, number];
|
|
7
|
-
export type PinPose = { readonly position: Point; readonly rotation: Rotation; readonly scale: Point };
|
|
8
|
-
export type PinParents = ReadonlyMap<string, string>;
|
|
9
|
-
export type HierarchyEdge = {from:string;to:string;cost:number;geodesic?:number};
|
|
10
|
-
export type PinMotion = 'none' | 'ik' | 'wiggle' | 'cloth' | 'fur';
|
|
11
|
-
export const DEFAULT_FUR_DEPTH = .1;
|
|
12
|
-
export const MAX_FUR_DEPTH = .5;
|
|
13
|
-
export type IKTarget = { readonly position: Point; readonly rotation: Rotation };
|
|
14
|
-
export const IK_CHAIN_KINDS = ['leg','arm','head','tail','wing','fin','tentacle','spine'] as const;
|
|
15
|
-
export type IKChainRole = { readonly kind: typeof IK_CHAIN_KINDS[number]; readonly side: 'center'|'left'|'right'; readonly position?: 'front'|'hind' };
|
|
16
|
-
export type PinRecord = { readonly id: string; readonly rest: Point; readonly pose: PinPose; readonly rigidGroup: string | null; readonly independent: boolean;
|
|
17
|
-
/** This pose was set on purpose in the slot now open, rather than reached by
|
|
18
|
-
* being carried by a parent or swept along by an IK solve. A slot still
|
|
19
|
-
* stores an unauthored pose it could not otherwise reproduce — an IK chain
|
|
20
|
-
* does not come back the same way twice — but only an authored one is shown
|
|
21
|
-
* as deliberate and only an authored one can be released. */
|
|
22
|
-
readonly authored?: boolean; readonly guide?: boolean; readonly ik?: boolean; readonly wiggle?: boolean; readonly cloth?: boolean; readonly fur?: boolean; readonly furDepth?: number; readonly motion?: MotionSettings; readonly ikTarget?: IKTarget; readonly ikConstraint?: IKConstraint; readonly chainRole?: IKChainRole };
|
|
1
|
+
import type { IKConstraint } from './ik-constraints.js';
|
|
2
|
+
import type { MotionSettings } from './motion-settings.js';
|
|
3
|
+
|
|
4
|
+
/** Shared data contract; deliberately independent of editor history and GPU types. */
|
|
5
|
+
export type Point = readonly [number, number, number];
|
|
6
|
+
export type Rotation = readonly [number, number, number, number];
|
|
7
|
+
export type PinPose = { readonly position: Point; readonly rotation: Rotation; readonly scale: Point };
|
|
8
|
+
export type PinParents = ReadonlyMap<string, string>;
|
|
9
|
+
export type HierarchyEdge = {from:string;to:string;cost:number;geodesic?:number};
|
|
10
|
+
export type PinMotion = 'none' | 'ik' | 'wiggle' | 'cloth' | 'fur';
|
|
11
|
+
export const DEFAULT_FUR_DEPTH = .1;
|
|
12
|
+
export const MAX_FUR_DEPTH = .5;
|
|
13
|
+
export type IKTarget = { readonly position: Point; readonly rotation: Rotation };
|
|
14
|
+
export const IK_CHAIN_KINDS = ['leg','arm','head','tail','wing','fin','tentacle','spine'] as const;
|
|
15
|
+
export type IKChainRole = { readonly kind: typeof IK_CHAIN_KINDS[number]; readonly side: 'center'|'left'|'right'; readonly position?: 'front'|'hind' };
|
|
16
|
+
export type PinRecord = { readonly id: string; readonly rest: Point; readonly pose: PinPose; readonly rigidGroup: string | null; readonly independent: boolean;
|
|
17
|
+
/** This pose was set on purpose in the slot now open, rather than reached by
|
|
18
|
+
* being carried by a parent or swept along by an IK solve. A slot still
|
|
19
|
+
* stores an unauthored pose it could not otherwise reproduce — an IK chain
|
|
20
|
+
* does not come back the same way twice — but only an authored one is shown
|
|
21
|
+
* as deliberate and only an authored one can be released. */
|
|
22
|
+
readonly authored?: boolean; readonly guide?: boolean; readonly ik?: boolean; readonly wiggle?: boolean; readonly cloth?: boolean; readonly fur?: boolean; readonly furDepth?: number; readonly motion?: MotionSettings; readonly ikTarget?: IKTarget; readonly ikConstraint?: IKConstraint; readonly chainRole?: IKChainRole };
|
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import { Quaternion, Vector3 } from 'three';
|
|
2
|
-
import { DEFAULT_FUR_DEPTH, type PinPose, type PinRecord } from './pin-types.js';
|
|
3
|
-
import type { PinParents } from './pin-types.js';
|
|
4
|
-
import { DEFAULT_MOTION, type MotionSettings } from './motion-settings.js';
|
|
5
|
-
import { ikChain } from './pin-ik.js';
|
|
6
|
-
import { windAcceleration } from './wind.js';
|
|
7
|
-
|
|
8
|
-
/** Disposable secondary motion. Never writes authored pins or history. */
|
|
9
|
-
export class PinWiggle {
|
|
10
|
-
private springs = new Map<string, { position: Vector3; velocity: Vector3; target:Vector3 }>();
|
|
11
|
-
private topology = '';
|
|
12
|
-
private lastPins: readonly PinRecord[] | null = null;
|
|
13
|
-
private lastParents: PinParents | null = null;
|
|
14
|
-
private awake = false;
|
|
15
|
-
private windTime = 0;
|
|
16
|
-
reset() { this.springs.clear(); this.topology = ''; this.lastPins = null; this.lastParents = null; this.awake = false; this.windTime = 0; }
|
|
17
|
-
|
|
18
|
-
step(pins: readonly PinRecord[], parents: PinParents, seconds: number, masses?:ReadonlyMap<string,number>, windStrength = 0): { pins: readonly PinRecord[]; active: boolean } {
|
|
19
|
-
if (windStrength <= 0 && !this.awake && pins === this.lastPins && parents === this.lastParents) return {pins,active:false};
|
|
20
|
-
const topology = JSON.stringify(pins.map(p => [p.id,p.rest,p.rigidGroup,p.independent,p.guide,p.wiggle,p.fur,p.furDepth,p.motion,parents.get(p.id)]));
|
|
21
|
-
if (topology !== this.topology) { this.reset(); this.topology = topology; }
|
|
22
|
-
const groups = new Map<string, PinRecord[]>(), byId = new Map(pins.map(p => [p.id,p]));
|
|
23
|
-
const key = (p: PinRecord) => p.rigidGroup ?? p.id;
|
|
24
|
-
for (const pin of pins) { const group = groups.get(key(pin)) ?? []; group.push(pin); groups.set(key(pin),group); }
|
|
25
|
-
const settings = new Map<string,Readonly<MotionSettings>>();
|
|
26
|
-
const volumes=[...groups.values()].filter(group=>!group.every(p=>p.guide)).map(group=>Math.max(1e-9,masses?.get(group[0].id)??1)).sort((a,b)=>a-b);
|
|
27
|
-
const typicalVolume=volumes[Math.floor(volumes.length/2)]??1;
|
|
28
|
-
for (const pin of pins.filter(p=>p.wiggle && !p.guide)) {
|
|
29
|
-
const chain=ikChain(pins,parents,pin.id,masses);
|
|
30
|
-
for (const node of chain.length>1 ? chain.slice(1) : chain) if (!settings.has(key(node))) settings.set(key(node),pin.motion ?? DEFAULT_MOTION);
|
|
31
|
-
}
|
|
32
|
-
for (const pin of pins.filter(p=>p.wiggle && !p.guide)) settings.set(key(pin),pin.motion ?? DEFAULT_MOTION);
|
|
33
|
-
// Fur belongs to the outer shell: unlike Wiggle it must not make the
|
|
34
|
-
// articulated core's ancestor chain into springs.
|
|
35
|
-
for (const pin of pins.filter(p=>p.fur && !p.guide)) settings.set(key(pin),pin.motion ?? DEFAULT_MOTION);
|
|
36
|
-
const output = new Map<string, PinPose>(), visiting = new Set<string>();
|
|
37
|
-
let active = false;
|
|
38
|
-
// Exact underdamped spring integration, stable across variable frame times.
|
|
39
|
-
// Clamp background-tab gaps rather than simulating missed seconds.
|
|
40
|
-
const dt = Math.min(Math.max(seconds,0),.05);
|
|
41
|
-
this.windTime += dt;
|
|
42
|
-
const visit = (id: string) => {
|
|
43
|
-
const group = groups.get(id)!;
|
|
44
|
-
if (output.has(group[0].id)) return;
|
|
45
|
-
if (visiting.has(id)) { for (const pin of group) output.set(pin.id,pin.pose); return; }
|
|
46
|
-
visiting.add(id);
|
|
47
|
-
const parent = group.map(p => byId.get(parents.get(p.id) ?? '')).find(p => p && key(p) !== id);
|
|
48
|
-
if (parent) visit(key(parent));
|
|
49
|
-
const parentPose = parent ? output.get(parent.id)! : undefined;
|
|
50
|
-
const inherited = parent && parentPose ? new Quaternion().fromArray(parentPose.rotation).multiply(new Quaternion().fromArray(parent.pose.rotation).invert()) : new Quaternion();
|
|
51
|
-
const targets = group.map(pin => {
|
|
52
|
-
const position = new Vector3().fromArray(pin.pose.position);
|
|
53
|
-
if (!pin.ik && parent && parentPose) position.sub(new Vector3().fromArray(parent.pose.position)).applyQuaternion(inherited).add(new Vector3().fromArray(parentPose.position));
|
|
54
|
-
return { pin, position, rotation: (pin.ik ? new Quaternion() : inherited.clone()).multiply(new Quaternion().fromArray(pin.pose.rotation)) };
|
|
55
|
-
});
|
|
56
|
-
const center = targets.reduce((sum,p) => sum.add(p.position),new Vector3()).divideScalar(group.length);
|
|
57
|
-
let position = center, rotation = new Quaternion();
|
|
58
|
-
const motion=settings.get(id);
|
|
59
|
-
if (motion && !group.every(p=>p.guide) && !group.some(p=>p.ik)) {
|
|
60
|
-
const ratio=Math.max(.125,Math.min(8,(masses?.get(group[0].id)??typicalVolume)/typicalVolume));
|
|
61
|
-
const gain=motion.strength*Math.max(.25,Math.min(4,Math.pow(1/ratio,.5*motion.massInfluence)));
|
|
62
|
-
const frequency=motion.frequency*2*Math.PI, damping=Math.max(.05,Math.min(1,motion.damping*Math.pow(ratio,.25*motion.massInfluence)));
|
|
63
|
-
const decay = damping * frequency, oscillation = frequency * Math.sqrt(1-damping*damping);
|
|
64
|
-
const e = Math.exp(-decay*dt), c = Math.cos(oscillation*dt), s = oscillation > 1e-8 ? Math.sin(oscillation*dt)/oscillation : dt;
|
|
65
|
-
let spring = this.springs.get(id);
|
|
66
|
-
if (!spring) { spring = { position:center.clone(),velocity:new Vector3(),target:center.clone() }; this.springs.set(id,spring); }
|
|
67
|
-
spring.position.addScaledVector(center.clone().sub(spring.target),1-gain);spring.target.copy(center);
|
|
68
|
-
if(motion.strength===0){spring.position.copy(center);spring.velocity.set(0,0,0);}
|
|
69
|
-
else if (windStrength > 0) spring.velocity.addScaledVector(windAcceleration(this.windTime, windStrength,
|
|
70
|
-
parentPose ? Math.max(center.distanceTo(new Vector3().fromArray(parentPose.position)), 1e-3) : 1, center.toArray()), motion.strength * dt);
|
|
71
|
-
const offset = spring.position.clone().sub(center), velocity = spring.velocity.clone();
|
|
72
|
-
spring.position.copy(offset).multiplyScalar(c).addScaledVector(velocity.clone().addScaledVector(offset,decay),s).multiplyScalar(e).add(center);
|
|
73
|
-
spring.velocity.copy(velocity).multiplyScalar(c).sub(velocity.clone().multiplyScalar(decay).addScaledVector(offset,frequency*frequency).multiplyScalar(s)).multiplyScalar(e);
|
|
74
|
-
const fur = group.find(p=>p.fur), furDepth=fur?.furDepth??DEFAULT_FUR_DEPTH;
|
|
75
|
-
let radius = 1;
|
|
76
|
-
if (parentPose) {
|
|
77
|
-
const pivot = new Vector3().fromArray(parentPose.position), desired = center.clone().sub(pivot);
|
|
78
|
-
radius = desired.length();
|
|
79
|
-
const direction = spring.position.clone().sub(pivot);
|
|
80
|
-
if (radius > 1e-8) {
|
|
81
|
-
if (direction.lengthSq() < 1e-16) direction.copy(desired);
|
|
82
|
-
const length=direction.length();direction.divideScalar(length);
|
|
83
|
-
const coreRadius=radius*(1-furDepth),outerRadius=radius*(1+furDepth);
|
|
84
|
-
const projected=fur?Math.max(coreRadius,Math.min(outerRadius,length)):radius;
|
|
85
|
-
spring.position.copy(pivot).addScaledVector(direction,projected);
|
|
86
|
-
if(!fur||length<coreRadius||length>outerRadius)spring.velocity.addScaledVector(direction,-spring.velocity.dot(direction));
|
|
87
|
-
rotation.setFromUnitVectors(desired.normalize(),direction);
|
|
88
|
-
} else { spring.position.copy(center); spring.velocity.set(0,0,0); }
|
|
89
|
-
}
|
|
90
|
-
if (fur) {
|
|
91
|
-
const shell = Math.max(1e-4, radius) * furDepth;
|
|
92
|
-
if (shell === 0) { spring.position.copy(center); spring.velocity.set(0,0,0); rotation.identity(); }
|
|
93
|
-
const displacement = spring.position.clone().sub(center);
|
|
94
|
-
if (displacement.length() > shell) {
|
|
95
|
-
displacement.setLength(shell); spring.position.copy(center).add(displacement);
|
|
96
|
-
const outward=displacement.normalize();spring.velocity.addScaledVector(outward,-Math.max(0,spring.velocity.dot(outward)));
|
|
97
|
-
}
|
|
98
|
-
if(parentPose&&radius>1e-8){
|
|
99
|
-
const pivot=new Vector3().fromArray(parentPose.position),desired=center.clone().sub(pivot).normalize(),direction=spring.position.clone().sub(pivot).normalize();
|
|
100
|
-
rotation.setFromUnitVectors(desired,direction);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
const epsilon = Math.max(radius,1e-3)*1e-5;
|
|
104
|
-
if (spring.position.distanceToSquared(center) < epsilon*epsilon && spring.velocity.lengthSq() < epsilon*epsilon*frequency*frequency) {
|
|
105
|
-
spring.position.copy(center); spring.velocity.set(0,0,0); rotation.identity();
|
|
106
|
-
} else active = true;
|
|
107
|
-
position = spring.position;
|
|
108
|
-
}
|
|
109
|
-
for (const target of targets) output.set(target.pin.id, {
|
|
110
|
-
position:target.position.sub(center).applyQuaternion(rotation).add(position).toArray(),
|
|
111
|
-
rotation:rotation.clone().multiply(target.rotation).normalize().toArray(),scale:target.pin.pose.scale,
|
|
112
|
-
});
|
|
113
|
-
visiting.delete(id);
|
|
114
|
-
};
|
|
115
|
-
for (const id of groups.keys()) visit(id);
|
|
116
|
-
this.lastPins = pins; this.lastParents = parents; this.awake = active;
|
|
117
|
-
return { pins: active ? pins.map(pin => ({...pin,pose:output.get(pin.id)!})) : pins, active };
|
|
118
|
-
}
|
|
119
|
-
}
|
|
1
|
+
import { Quaternion, Vector3 } from 'three';
|
|
2
|
+
import { DEFAULT_FUR_DEPTH, type PinPose, type PinRecord } from './pin-types.js';
|
|
3
|
+
import type { PinParents } from './pin-types.js';
|
|
4
|
+
import { DEFAULT_MOTION, type MotionSettings } from './motion-settings.js';
|
|
5
|
+
import { ikChain } from './pin-ik.js';
|
|
6
|
+
import { windAcceleration } from './wind.js';
|
|
7
|
+
|
|
8
|
+
/** Disposable secondary motion. Never writes authored pins or history. */
|
|
9
|
+
export class PinWiggle {
|
|
10
|
+
private springs = new Map<string, { position: Vector3; velocity: Vector3; target:Vector3 }>();
|
|
11
|
+
private topology = '';
|
|
12
|
+
private lastPins: readonly PinRecord[] | null = null;
|
|
13
|
+
private lastParents: PinParents | null = null;
|
|
14
|
+
private awake = false;
|
|
15
|
+
private windTime = 0;
|
|
16
|
+
reset() { this.springs.clear(); this.topology = ''; this.lastPins = null; this.lastParents = null; this.awake = false; this.windTime = 0; }
|
|
17
|
+
|
|
18
|
+
step(pins: readonly PinRecord[], parents: PinParents, seconds: number, masses?:ReadonlyMap<string,number>, windStrength = 0): { pins: readonly PinRecord[]; active: boolean } {
|
|
19
|
+
if (windStrength <= 0 && !this.awake && pins === this.lastPins && parents === this.lastParents) return {pins,active:false};
|
|
20
|
+
const topology = JSON.stringify(pins.map(p => [p.id,p.rest,p.rigidGroup,p.independent,p.guide,p.wiggle,p.fur,p.furDepth,p.motion,parents.get(p.id)]));
|
|
21
|
+
if (topology !== this.topology) { this.reset(); this.topology = topology; }
|
|
22
|
+
const groups = new Map<string, PinRecord[]>(), byId = new Map(pins.map(p => [p.id,p]));
|
|
23
|
+
const key = (p: PinRecord) => p.rigidGroup ?? p.id;
|
|
24
|
+
for (const pin of pins) { const group = groups.get(key(pin)) ?? []; group.push(pin); groups.set(key(pin),group); }
|
|
25
|
+
const settings = new Map<string,Readonly<MotionSettings>>();
|
|
26
|
+
const volumes=[...groups.values()].filter(group=>!group.every(p=>p.guide)).map(group=>Math.max(1e-9,masses?.get(group[0].id)??1)).sort((a,b)=>a-b);
|
|
27
|
+
const typicalVolume=volumes[Math.floor(volumes.length/2)]??1;
|
|
28
|
+
for (const pin of pins.filter(p=>p.wiggle && !p.guide)) {
|
|
29
|
+
const chain=ikChain(pins,parents,pin.id,masses);
|
|
30
|
+
for (const node of chain.length>1 ? chain.slice(1) : chain) if (!settings.has(key(node))) settings.set(key(node),pin.motion ?? DEFAULT_MOTION);
|
|
31
|
+
}
|
|
32
|
+
for (const pin of pins.filter(p=>p.wiggle && !p.guide)) settings.set(key(pin),pin.motion ?? DEFAULT_MOTION);
|
|
33
|
+
// Fur belongs to the outer shell: unlike Wiggle it must not make the
|
|
34
|
+
// articulated core's ancestor chain into springs.
|
|
35
|
+
for (const pin of pins.filter(p=>p.fur && !p.guide)) settings.set(key(pin),pin.motion ?? DEFAULT_MOTION);
|
|
36
|
+
const output = new Map<string, PinPose>(), visiting = new Set<string>();
|
|
37
|
+
let active = false;
|
|
38
|
+
// Exact underdamped spring integration, stable across variable frame times.
|
|
39
|
+
// Clamp background-tab gaps rather than simulating missed seconds.
|
|
40
|
+
const dt = Math.min(Math.max(seconds,0),.05);
|
|
41
|
+
this.windTime += dt;
|
|
42
|
+
const visit = (id: string) => {
|
|
43
|
+
const group = groups.get(id)!;
|
|
44
|
+
if (output.has(group[0].id)) return;
|
|
45
|
+
if (visiting.has(id)) { for (const pin of group) output.set(pin.id,pin.pose); return; }
|
|
46
|
+
visiting.add(id);
|
|
47
|
+
const parent = group.map(p => byId.get(parents.get(p.id) ?? '')).find(p => p && key(p) !== id);
|
|
48
|
+
if (parent) visit(key(parent));
|
|
49
|
+
const parentPose = parent ? output.get(parent.id)! : undefined;
|
|
50
|
+
const inherited = parent && parentPose ? new Quaternion().fromArray(parentPose.rotation).multiply(new Quaternion().fromArray(parent.pose.rotation).invert()) : new Quaternion();
|
|
51
|
+
const targets = group.map(pin => {
|
|
52
|
+
const position = new Vector3().fromArray(pin.pose.position);
|
|
53
|
+
if (!pin.ik && parent && parentPose) position.sub(new Vector3().fromArray(parent.pose.position)).applyQuaternion(inherited).add(new Vector3().fromArray(parentPose.position));
|
|
54
|
+
return { pin, position, rotation: (pin.ik ? new Quaternion() : inherited.clone()).multiply(new Quaternion().fromArray(pin.pose.rotation)) };
|
|
55
|
+
});
|
|
56
|
+
const center = targets.reduce((sum,p) => sum.add(p.position),new Vector3()).divideScalar(group.length);
|
|
57
|
+
let position = center, rotation = new Quaternion();
|
|
58
|
+
const motion=settings.get(id);
|
|
59
|
+
if (motion && !group.every(p=>p.guide) && !group.some(p=>p.ik)) {
|
|
60
|
+
const ratio=Math.max(.125,Math.min(8,(masses?.get(group[0].id)??typicalVolume)/typicalVolume));
|
|
61
|
+
const gain=motion.strength*Math.max(.25,Math.min(4,Math.pow(1/ratio,.5*motion.massInfluence)));
|
|
62
|
+
const frequency=motion.frequency*2*Math.PI, damping=Math.max(.05,Math.min(1,motion.damping*Math.pow(ratio,.25*motion.massInfluence)));
|
|
63
|
+
const decay = damping * frequency, oscillation = frequency * Math.sqrt(1-damping*damping);
|
|
64
|
+
const e = Math.exp(-decay*dt), c = Math.cos(oscillation*dt), s = oscillation > 1e-8 ? Math.sin(oscillation*dt)/oscillation : dt;
|
|
65
|
+
let spring = this.springs.get(id);
|
|
66
|
+
if (!spring) { spring = { position:center.clone(),velocity:new Vector3(),target:center.clone() }; this.springs.set(id,spring); }
|
|
67
|
+
spring.position.addScaledVector(center.clone().sub(spring.target),1-gain);spring.target.copy(center);
|
|
68
|
+
if(motion.strength===0){spring.position.copy(center);spring.velocity.set(0,0,0);}
|
|
69
|
+
else if (windStrength > 0) spring.velocity.addScaledVector(windAcceleration(this.windTime, windStrength,
|
|
70
|
+
parentPose ? Math.max(center.distanceTo(new Vector3().fromArray(parentPose.position)), 1e-3) : 1, center.toArray()), motion.strength * dt);
|
|
71
|
+
const offset = spring.position.clone().sub(center), velocity = spring.velocity.clone();
|
|
72
|
+
spring.position.copy(offset).multiplyScalar(c).addScaledVector(velocity.clone().addScaledVector(offset,decay),s).multiplyScalar(e).add(center);
|
|
73
|
+
spring.velocity.copy(velocity).multiplyScalar(c).sub(velocity.clone().multiplyScalar(decay).addScaledVector(offset,frequency*frequency).multiplyScalar(s)).multiplyScalar(e);
|
|
74
|
+
const fur = group.find(p=>p.fur), furDepth=fur?.furDepth??DEFAULT_FUR_DEPTH;
|
|
75
|
+
let radius = 1;
|
|
76
|
+
if (parentPose) {
|
|
77
|
+
const pivot = new Vector3().fromArray(parentPose.position), desired = center.clone().sub(pivot);
|
|
78
|
+
radius = desired.length();
|
|
79
|
+
const direction = spring.position.clone().sub(pivot);
|
|
80
|
+
if (radius > 1e-8) {
|
|
81
|
+
if (direction.lengthSq() < 1e-16) direction.copy(desired);
|
|
82
|
+
const length=direction.length();direction.divideScalar(length);
|
|
83
|
+
const coreRadius=radius*(1-furDepth),outerRadius=radius*(1+furDepth);
|
|
84
|
+
const projected=fur?Math.max(coreRadius,Math.min(outerRadius,length)):radius;
|
|
85
|
+
spring.position.copy(pivot).addScaledVector(direction,projected);
|
|
86
|
+
if(!fur||length<coreRadius||length>outerRadius)spring.velocity.addScaledVector(direction,-spring.velocity.dot(direction));
|
|
87
|
+
rotation.setFromUnitVectors(desired.normalize(),direction);
|
|
88
|
+
} else { spring.position.copy(center); spring.velocity.set(0,0,0); }
|
|
89
|
+
}
|
|
90
|
+
if (fur) {
|
|
91
|
+
const shell = Math.max(1e-4, radius) * furDepth;
|
|
92
|
+
if (shell === 0) { spring.position.copy(center); spring.velocity.set(0,0,0); rotation.identity(); }
|
|
93
|
+
const displacement = spring.position.clone().sub(center);
|
|
94
|
+
if (displacement.length() > shell) {
|
|
95
|
+
displacement.setLength(shell); spring.position.copy(center).add(displacement);
|
|
96
|
+
const outward=displacement.normalize();spring.velocity.addScaledVector(outward,-Math.max(0,spring.velocity.dot(outward)));
|
|
97
|
+
}
|
|
98
|
+
if(parentPose&&radius>1e-8){
|
|
99
|
+
const pivot=new Vector3().fromArray(parentPose.position),desired=center.clone().sub(pivot).normalize(),direction=spring.position.clone().sub(pivot).normalize();
|
|
100
|
+
rotation.setFromUnitVectors(desired,direction);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const epsilon = Math.max(radius,1e-3)*1e-5;
|
|
104
|
+
if (spring.position.distanceToSquared(center) < epsilon*epsilon && spring.velocity.lengthSq() < epsilon*epsilon*frequency*frequency) {
|
|
105
|
+
spring.position.copy(center); spring.velocity.set(0,0,0); rotation.identity();
|
|
106
|
+
} else active = true;
|
|
107
|
+
position = spring.position;
|
|
108
|
+
}
|
|
109
|
+
for (const target of targets) output.set(target.pin.id, {
|
|
110
|
+
position:target.position.sub(center).applyQuaternion(rotation).add(position).toArray(),
|
|
111
|
+
rotation:rotation.clone().multiply(target.rotation).normalize().toArray(),scale:target.pin.pose.scale,
|
|
112
|
+
});
|
|
113
|
+
visiting.delete(id);
|
|
114
|
+
};
|
|
115
|
+
for (const id of groups.keys()) visit(id);
|
|
116
|
+
this.lastPins = pins; this.lastParents = parents; this.awake = active;
|
|
117
|
+
return { pins: active ? pins.map(pin => ({...pin,pose:output.get(pin.id)!})) : pins, active };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -1,106 +1,106 @@
|
|
|
1
|
-
import type { IKTarget, PinPose, PinRecord, Point, Rotation } from './pin-types.js';
|
|
2
|
-
import type { PinParents } from './pin-types.js';
|
|
3
|
-
|
|
4
|
-
export type StoredPose = { readonly id:string; readonly pose:PinPose; readonly ikTarget?:IKTarget; readonly authored?:boolean };
|
|
5
|
-
export type PoseSlot = { readonly slot:number; readonly poses:readonly StoredPose[] };
|
|
6
|
-
export const POSE_SLOTS=9;
|
|
7
|
-
/** Below any edit a hand can make, and far above the drift of composing one
|
|
8
|
-
* carried transform after another. */
|
|
9
|
-
const AUTHORED_EPSILON=1e-6;
|
|
10
|
-
|
|
11
|
-
export function capturePose(pins:readonly PinRecord[]):readonly StoredPose[]{
|
|
12
|
-
return pins.map(pin=>({id:pin.id,pose:pin.pose,...(pin.authored?{authored:true}:{}),...(pin.ikTarget?{ikTarget:pin.ikTarget}:{})}));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
const restPoseOf=(rest:Point):PinPose=>({position:[rest[0],rest[1],rest[2]],rotation:[0,0,0,1],scale:[1,1,1]});
|
|
16
|
-
|
|
17
|
-
/** Where a pin lands when its parent carries it from rest. A parent's pose IS
|
|
18
|
-
* a similarity out of its own rest frame, so the child keeps its rest offset
|
|
19
|
-
* through that same rotation and scale — the transform hierarchy posing applies
|
|
20
|
-
* when a parent moves, written as one step from rest instead of accumulated. */
|
|
21
|
-
export function carriedPose(rest:Point,parentRest:Point,parent:PinPose):PinPose{
|
|
22
|
-
const [qx,qy,qz,qw]=parent.rotation;
|
|
23
|
-
const x=(rest[0]-parentRest[0])*parent.scale[0],y=(rest[1]-parentRest[1])*parent.scale[1],z=(rest[2]-parentRest[2])*parent.scale[2];
|
|
24
|
-
const tx=2*(qy*z-qz*y),ty=2*(qz*x-qx*z),tz=2*(qx*y-qy*x);
|
|
25
|
-
const position:Point=[parent.position[0]+x+qw*tx+(qy*tz-qz*ty),parent.position[1]+y+qw*ty+(qz*tx-qx*tz),parent.position[2]+z+qw*tz+(qx*ty-qy*tx)];
|
|
26
|
-
const rotation:Rotation=[qx,qy,qz,qw],scale:Point=[parent.scale[0],parent.scale[1],parent.scale[2]];
|
|
27
|
-
return {position,rotation,scale};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const samePose=(a:PinPose,b:PinPose)=>
|
|
31
|
-
a.position.every((v,i)=>Math.abs(v-b.position[i])<=AUTHORED_EPSILON)
|
|
32
|
-
&& a.scale.every((v,i)=>Math.abs(v-b.scale[i])<=AUTHORED_EPSILON)
|
|
33
|
-
&& a.rotation.every((v,i)=>Math.abs(v-b.rotation[i])<=AUTHORED_EPSILON);
|
|
34
|
-
|
|
35
|
-
/** What a slot would place a pin at if it said nothing about it. */
|
|
36
|
-
function inheritedPose(pin:PinRecord,parent:PinRecord|undefined,parentPose:PinPose|undefined):PinPose{
|
|
37
|
-
return parent&&parentPose?carriedPose(pin.rest,parent.rest,parentPose):restPoseOf(pin.rest);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Only the poses a slot actually authors. A pin the hierarchy already places
|
|
41
|
-
* is left out, so a point added while another pose was open keeps following its
|
|
42
|
-
* parent everywhere else until it is deliberately posed there. */
|
|
43
|
-
export function authoredPoses(pins:readonly PinRecord[],parents:PinParents=new Map()):readonly StoredPose[]{
|
|
44
|
-
const byId=new Map(pins.map(pin=>[pin.id,pin]));
|
|
45
|
-
return capturePose(pins).filter(stored=>{
|
|
46
|
-
const pin=byId.get(stored.id)!;
|
|
47
|
-
// Deliberate, or a goal, which is deliberate by definition.
|
|
48
|
-
if(pin.authored||pin.ikTarget)return true;
|
|
49
|
-
// Otherwise it is only kept because it could not be worked out again. A
|
|
50
|
-
// joint the IK solve swept along is not reproducible — the same goal
|
|
51
|
-
// reached from a different bend gives a different arm — so the value stays
|
|
52
|
-
// even though nobody posed it.
|
|
53
|
-
const parent=byId.get(parents.get(pin.id)??'');
|
|
54
|
-
return !samePose(pin.pose,inheritedPose(pin,parent,parent?.pose));
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Active pins are the edit register. Inactive slots contain transforms only;
|
|
59
|
-
* topology, rest positions and settings remain shared and action-owned. A pin
|
|
60
|
-
* the slot does not mention inherits through the hierarchy rather than dropping
|
|
61
|
-
* to rest, which is what keeps a newly placed point attached to its parent in
|
|
62
|
-
* every other pose. */
|
|
63
|
-
export function resolvePose(pins:readonly PinRecord[],slot:PoseSlot,parents:PinParents=new Map()):readonly PinRecord[]{
|
|
64
|
-
const saved=new Map(slot.poses.map(p=>[p.id,p]));
|
|
65
|
-
const byId=new Map(pins.map(pin=>[pin.id,pin]));
|
|
66
|
-
const memo=new Map<string,PinPose>(),visiting=new Set<string>();
|
|
67
|
-
const poseOf=(pin:PinRecord):PinPose=>{
|
|
68
|
-
const cached=memo.get(pin.id);if(cached)return cached;
|
|
69
|
-
const stored=saved.get(pin.id);
|
|
70
|
-
let pose:PinPose;
|
|
71
|
-
if(stored)pose=stored.pose;
|
|
72
|
-
else{
|
|
73
|
-
const parentId=parents.get(pin.id);
|
|
74
|
-
// A parent already on the stack means a cycle in a derived hierarchy;
|
|
75
|
-
// rest is the only answer that terminates.
|
|
76
|
-
const parent=parentId&&!visiting.has(parentId)?byId.get(parentId):undefined;
|
|
77
|
-
if(!parent)pose=restPoseOf(pin.rest);
|
|
78
|
-
else{visiting.add(pin.id);pose=carriedPose(pin.rest,parent.rest,poseOf(parent));visiting.delete(pin.id);}
|
|
79
|
-
}
|
|
80
|
-
memo.set(pin.id,pose);return pose;
|
|
81
|
-
};
|
|
82
|
-
const resolved:PinRecord[]=pins.map(pin=>{
|
|
83
|
-
const stored=saved.get(pin.id),{ikTarget:_target,authored:_authored,...base}=pin;
|
|
84
|
-
return {...base,pose:poseOf(pin),...(stored?.authored?{authored:true}:{}),...(stored?.ikTarget?{ikTarget:stored.ikTarget}:{})};
|
|
85
|
-
});
|
|
86
|
-
// Group membership is shared. A group created after another pose was saved
|
|
87
|
-
// still has one rotation/uniform scale when that pose is opened. Positions
|
|
88
|
-
// stay authored; subsequent rigid edits preserve that arrangement.
|
|
89
|
-
return resolved.map(pin=>{
|
|
90
|
-
if(!pin.rigidGroup)return pin;const anchor=resolved.find(p=>p.rigidGroup===pin.rigidGroup)!;
|
|
91
|
-
return {...pin,pose:{...pin.pose,rotation:anchor.pose.rotation,scale:[anchor.pose.scale[0],anchor.pose.scale[0],anchor.pose.scale[0]]}};
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
export function definedPoses(model:{readonly pins:readonly PinRecord[];readonly poseSlots:readonly PoseSlot[];readonly activePose:number}):PoseSlot[]{
|
|
95
|
-
const slots=model.poseSlots.filter(p=>p.slot!==model.activePose);
|
|
96
|
-
return [...slots,...(model.activePose === 0 ? [] : [{slot:model.activePose,poses:capturePose(model.pins)}])].sort((a,b)=>a.slot-b.slot);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Pose 0 is derived from shared rest positions, never an editable pose slot. */
|
|
100
|
-
export function restPoseSlot(pins:readonly PinRecord[]):PoseSlot{
|
|
101
|
-
return {slot:0,poses:pins.map(pin=>({id:pin.id,pose:restPoseOf(pin.rest)}))};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function playablePoses(model:{readonly pins:readonly PinRecord[];readonly poseSlots:readonly PoseSlot[];readonly activePose:number}):PoseSlot[]{
|
|
105
|
-
return [restPoseSlot(model.pins),...definedPoses(model)];
|
|
106
|
-
}
|
|
1
|
+
import type { IKTarget, PinPose, PinRecord, Point, Rotation } from './pin-types.js';
|
|
2
|
+
import type { PinParents } from './pin-types.js';
|
|
3
|
+
|
|
4
|
+
export type StoredPose = { readonly id:string; readonly pose:PinPose; readonly ikTarget?:IKTarget; readonly authored?:boolean };
|
|
5
|
+
export type PoseSlot = { readonly slot:number; readonly poses:readonly StoredPose[] };
|
|
6
|
+
export const POSE_SLOTS=9;
|
|
7
|
+
/** Below any edit a hand can make, and far above the drift of composing one
|
|
8
|
+
* carried transform after another. */
|
|
9
|
+
const AUTHORED_EPSILON=1e-6;
|
|
10
|
+
|
|
11
|
+
export function capturePose(pins:readonly PinRecord[]):readonly StoredPose[]{
|
|
12
|
+
return pins.map(pin=>({id:pin.id,pose:pin.pose,...(pin.authored?{authored:true}:{}),...(pin.ikTarget?{ikTarget:pin.ikTarget}:{})}));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const restPoseOf=(rest:Point):PinPose=>({position:[rest[0],rest[1],rest[2]],rotation:[0,0,0,1],scale:[1,1,1]});
|
|
16
|
+
|
|
17
|
+
/** Where a pin lands when its parent carries it from rest. A parent's pose IS
|
|
18
|
+
* a similarity out of its own rest frame, so the child keeps its rest offset
|
|
19
|
+
* through that same rotation and scale — the transform hierarchy posing applies
|
|
20
|
+
* when a parent moves, written as one step from rest instead of accumulated. */
|
|
21
|
+
export function carriedPose(rest:Point,parentRest:Point,parent:PinPose):PinPose{
|
|
22
|
+
const [qx,qy,qz,qw]=parent.rotation;
|
|
23
|
+
const x=(rest[0]-parentRest[0])*parent.scale[0],y=(rest[1]-parentRest[1])*parent.scale[1],z=(rest[2]-parentRest[2])*parent.scale[2];
|
|
24
|
+
const tx=2*(qy*z-qz*y),ty=2*(qz*x-qx*z),tz=2*(qx*y-qy*x);
|
|
25
|
+
const position:Point=[parent.position[0]+x+qw*tx+(qy*tz-qz*ty),parent.position[1]+y+qw*ty+(qz*tx-qx*tz),parent.position[2]+z+qw*tz+(qx*ty-qy*tx)];
|
|
26
|
+
const rotation:Rotation=[qx,qy,qz,qw],scale:Point=[parent.scale[0],parent.scale[1],parent.scale[2]];
|
|
27
|
+
return {position,rotation,scale};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const samePose=(a:PinPose,b:PinPose)=>
|
|
31
|
+
a.position.every((v,i)=>Math.abs(v-b.position[i])<=AUTHORED_EPSILON)
|
|
32
|
+
&& a.scale.every((v,i)=>Math.abs(v-b.scale[i])<=AUTHORED_EPSILON)
|
|
33
|
+
&& a.rotation.every((v,i)=>Math.abs(v-b.rotation[i])<=AUTHORED_EPSILON);
|
|
34
|
+
|
|
35
|
+
/** What a slot would place a pin at if it said nothing about it. */
|
|
36
|
+
function inheritedPose(pin:PinRecord,parent:PinRecord|undefined,parentPose:PinPose|undefined):PinPose{
|
|
37
|
+
return parent&&parentPose?carriedPose(pin.rest,parent.rest,parentPose):restPoseOf(pin.rest);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Only the poses a slot actually authors. A pin the hierarchy already places
|
|
41
|
+
* is left out, so a point added while another pose was open keeps following its
|
|
42
|
+
* parent everywhere else until it is deliberately posed there. */
|
|
43
|
+
export function authoredPoses(pins:readonly PinRecord[],parents:PinParents=new Map()):readonly StoredPose[]{
|
|
44
|
+
const byId=new Map(pins.map(pin=>[pin.id,pin]));
|
|
45
|
+
return capturePose(pins).filter(stored=>{
|
|
46
|
+
const pin=byId.get(stored.id)!;
|
|
47
|
+
// Deliberate, or a goal, which is deliberate by definition.
|
|
48
|
+
if(pin.authored||pin.ikTarget)return true;
|
|
49
|
+
// Otherwise it is only kept because it could not be worked out again. A
|
|
50
|
+
// joint the IK solve swept along is not reproducible — the same goal
|
|
51
|
+
// reached from a different bend gives a different arm — so the value stays
|
|
52
|
+
// even though nobody posed it.
|
|
53
|
+
const parent=byId.get(parents.get(pin.id)??'');
|
|
54
|
+
return !samePose(pin.pose,inheritedPose(pin,parent,parent?.pose));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Active pins are the edit register. Inactive slots contain transforms only;
|
|
59
|
+
* topology, rest positions and settings remain shared and action-owned. A pin
|
|
60
|
+
* the slot does not mention inherits through the hierarchy rather than dropping
|
|
61
|
+
* to rest, which is what keeps a newly placed point attached to its parent in
|
|
62
|
+
* every other pose. */
|
|
63
|
+
export function resolvePose(pins:readonly PinRecord[],slot:PoseSlot,parents:PinParents=new Map()):readonly PinRecord[]{
|
|
64
|
+
const saved=new Map(slot.poses.map(p=>[p.id,p]));
|
|
65
|
+
const byId=new Map(pins.map(pin=>[pin.id,pin]));
|
|
66
|
+
const memo=new Map<string,PinPose>(),visiting=new Set<string>();
|
|
67
|
+
const poseOf=(pin:PinRecord):PinPose=>{
|
|
68
|
+
const cached=memo.get(pin.id);if(cached)return cached;
|
|
69
|
+
const stored=saved.get(pin.id);
|
|
70
|
+
let pose:PinPose;
|
|
71
|
+
if(stored)pose=stored.pose;
|
|
72
|
+
else{
|
|
73
|
+
const parentId=parents.get(pin.id);
|
|
74
|
+
// A parent already on the stack means a cycle in a derived hierarchy;
|
|
75
|
+
// rest is the only answer that terminates.
|
|
76
|
+
const parent=parentId&&!visiting.has(parentId)?byId.get(parentId):undefined;
|
|
77
|
+
if(!parent)pose=restPoseOf(pin.rest);
|
|
78
|
+
else{visiting.add(pin.id);pose=carriedPose(pin.rest,parent.rest,poseOf(parent));visiting.delete(pin.id);}
|
|
79
|
+
}
|
|
80
|
+
memo.set(pin.id,pose);return pose;
|
|
81
|
+
};
|
|
82
|
+
const resolved:PinRecord[]=pins.map(pin=>{
|
|
83
|
+
const stored=saved.get(pin.id),{ikTarget:_target,authored:_authored,...base}=pin;
|
|
84
|
+
return {...base,pose:poseOf(pin),...(stored?.authored?{authored:true}:{}),...(stored?.ikTarget?{ikTarget:stored.ikTarget}:{})};
|
|
85
|
+
});
|
|
86
|
+
// Group membership is shared. A group created after another pose was saved
|
|
87
|
+
// still has one rotation/uniform scale when that pose is opened. Positions
|
|
88
|
+
// stay authored; subsequent rigid edits preserve that arrangement.
|
|
89
|
+
return resolved.map(pin=>{
|
|
90
|
+
if(!pin.rigidGroup)return pin;const anchor=resolved.find(p=>p.rigidGroup===pin.rigidGroup)!;
|
|
91
|
+
return {...pin,pose:{...pin.pose,rotation:anchor.pose.rotation,scale:[anchor.pose.scale[0],anchor.pose.scale[0],anchor.pose.scale[0]]}};
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
export function definedPoses(model:{readonly pins:readonly PinRecord[];readonly poseSlots:readonly PoseSlot[];readonly activePose:number}):PoseSlot[]{
|
|
95
|
+
const slots=model.poseSlots.filter(p=>p.slot!==model.activePose);
|
|
96
|
+
return [...slots,...(model.activePose === 0 ? [] : [{slot:model.activePose,poses:capturePose(model.pins)}])].sort((a,b)=>a.slot-b.slot);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Pose 0 is derived from shared rest positions, never an editable pose slot. */
|
|
100
|
+
export function restPoseSlot(pins:readonly PinRecord[]):PoseSlot{
|
|
101
|
+
return {slot:0,poses:pins.map(pin=>({id:pin.id,pose:restPoseOf(pin.rest)}))};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function playablePoses(model:{readonly pins:readonly PinRecord[];readonly poseSlots:readonly PoseSlot[];readonly activePose:number}):PoseSlot[]{
|
|
105
|
+
return [restPoseSlot(model.pins),...definedPoses(model)];
|
|
106
|
+
}
|