@holtjs/stage 0.4.1 → 0.5.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/CHANGELOG.md +12 -0
- package/README.md +140 -119
- package/dist/lighting.d.ts +129 -0
- package/dist/lighting.js +527 -0
- package/dist/projection.d.ts +131 -0
- package/dist/projection.js +262 -0
- package/dist/rig2d.d.ts +317 -0
- package/dist/rig2d.js +871 -0
- package/dist/tilemap.d.ts +158 -0
- package/dist/tilemap.js +583 -0
- package/dist/world.d.ts +183 -0
- package/dist/world.js +400 -0
- package/llms.txt +26 -17
- package/package.json +21 -1
- package/skills/stage/SKILL.md +140 -157
- package/skills/stage/references/subpaths.md +62 -54
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// src/projection.ts
|
|
2
|
+
const TWO_PI = Math.PI * 2;
|
|
3
|
+
function shortestAngleDiff(from, to) {
|
|
4
|
+
let d = (to - from) % TWO_PI;
|
|
5
|
+
if (d > Math.PI) d -= TWO_PI;
|
|
6
|
+
else if (d < -Math.PI) d += TWO_PI;
|
|
7
|
+
return d;
|
|
8
|
+
}
|
|
9
|
+
var Camera25D = class {
|
|
10
|
+
viewportWidth;
|
|
11
|
+
viewportHeight;
|
|
12
|
+
focusX;
|
|
13
|
+
focusY;
|
|
14
|
+
yaw;
|
|
15
|
+
targetYaw;
|
|
16
|
+
pitch;
|
|
17
|
+
zoom;
|
|
18
|
+
yawVel = 0;
|
|
19
|
+
yawStiffness;
|
|
20
|
+
followTarget = null;
|
|
21
|
+
followLerp = .14;
|
|
22
|
+
constructor(cfg) {
|
|
23
|
+
this.viewportWidth = Math.max(1, cfg.viewportWidth);
|
|
24
|
+
this.viewportHeight = Math.max(1, cfg.viewportHeight);
|
|
25
|
+
this.focusX = cfg.focusX ?? 0;
|
|
26
|
+
this.focusY = cfg.focusY ?? 0;
|
|
27
|
+
this.yaw = cfg.yaw ?? 0;
|
|
28
|
+
this.targetYaw = this.yaw;
|
|
29
|
+
this.pitch = Math.max(.25, Math.min(Math.PI * .5, cfg.pitch ?? .82));
|
|
30
|
+
this.zoom = Math.max(.05, cfg.zoom ?? 1);
|
|
31
|
+
this.yawStiffness = cfg.yawStiffness ?? 210;
|
|
32
|
+
}
|
|
33
|
+
get sinPitch() {
|
|
34
|
+
return Math.sin(this.pitch);
|
|
35
|
+
}
|
|
36
|
+
get cosPitch() {
|
|
37
|
+
return Math.cos(this.pitch);
|
|
38
|
+
}
|
|
39
|
+
resize(width, height) {
|
|
40
|
+
this.viewportWidth = Math.max(1, width);
|
|
41
|
+
this.viewportHeight = Math.max(1, height);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
setFocus(wx, wy) {
|
|
45
|
+
this.focusX = wx;
|
|
46
|
+
this.focusY = wy;
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
startFollow(target, lerp = .14) {
|
|
50
|
+
this.followTarget = target;
|
|
51
|
+
this.followLerp = Math.max(.01, Math.min(1, lerp));
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
stopFollow() {
|
|
55
|
+
this.followTarget = null;
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
/** Rotate target yaw by `steps * stepAngleRad` (default 45° = `Math.PI / 4`, like pressing Q / E in *Don't Starve*). */
|
|
59
|
+
rotateSteps(steps, stepAngleRad = Math.PI * .25) {
|
|
60
|
+
this.targetYaw += steps * stepAngleRad;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
setYawImmediate(yawRad) {
|
|
64
|
+
this.yaw = yawRad;
|
|
65
|
+
this.targetYaw = yawRad;
|
|
66
|
+
this.yawVel = 0;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/** Step smooth critically-damped yaw rotation and focus follow deterministically on `deltaMs`. */
|
|
70
|
+
step(deltaMs) {
|
|
71
|
+
const dt = Math.min(.1, Math.max(0, deltaMs) * .001);
|
|
72
|
+
if (dt <= 0) return this;
|
|
73
|
+
const k = this.yawStiffness;
|
|
74
|
+
const damp = 2 * Math.sqrt(k);
|
|
75
|
+
const subSteps = 2;
|
|
76
|
+
const h = dt / subSteps;
|
|
77
|
+
for (let s = 0; s < subSteps; s++) {
|
|
78
|
+
const acc = shortestAngleDiff(this.yaw, this.targetYaw) * k - this.yawVel * damp;
|
|
79
|
+
this.yawVel += acc * h;
|
|
80
|
+
this.yaw += this.yawVel * h;
|
|
81
|
+
}
|
|
82
|
+
if (Math.abs(shortestAngleDiff(this.yaw, this.targetYaw)) < 1e-4 && Math.abs(this.yawVel) < 1e-4) {
|
|
83
|
+
this.yaw = this.targetYaw;
|
|
84
|
+
this.yawVel = 0;
|
|
85
|
+
}
|
|
86
|
+
if (this.followTarget) {
|
|
87
|
+
const alpha = 1 - Math.pow(1 - this.followLerp, dt * 60);
|
|
88
|
+
this.focusX += (this.followTarget.x - this.focusX) * alpha;
|
|
89
|
+
this.focusY += (this.followTarget.y - this.focusY) * alpha;
|
|
90
|
+
}
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/** Project a 3D world point `(wx, wy, wz)` (`wz >= 0` height above ground) into 2.5D screen coordinates + depth key. */
|
|
94
|
+
worldToScreen(wx, wy, wz = 0, out) {
|
|
95
|
+
const dx = wx - this.focusX;
|
|
96
|
+
const dy = wy - this.focusY;
|
|
97
|
+
const c = Math.cos(this.yaw);
|
|
98
|
+
const s = Math.sin(this.yaw);
|
|
99
|
+
const rx = dx * c + dy * s;
|
|
100
|
+
const ry = -dx * s + dy * c;
|
|
101
|
+
const sx = this.viewportWidth * .5 + rx * this.zoom;
|
|
102
|
+
const sy = this.viewportHeight * .5 + (ry * this.sinPitch - wz * this.cosPitch) * this.zoom;
|
|
103
|
+
const target = out ?? {
|
|
104
|
+
x: 0,
|
|
105
|
+
y: 0,
|
|
106
|
+
depth: 0,
|
|
107
|
+
scale: 1
|
|
108
|
+
};
|
|
109
|
+
target.x = sx;
|
|
110
|
+
target.y = sy;
|
|
111
|
+
target.depth = ry;
|
|
112
|
+
target.scale = this.zoom;
|
|
113
|
+
return target;
|
|
114
|
+
}
|
|
115
|
+
/** Unproject a screen pixel `(sx, sy)` onto the horizontal ground plane `wz = planeZ` (exact inverse of `worldToScreen`). */
|
|
116
|
+
screenToGround(sx, sy, planeZ = 0, out = {
|
|
117
|
+
x: 0,
|
|
118
|
+
y: 0
|
|
119
|
+
}) {
|
|
120
|
+
const sinP = Math.max(.05, this.sinPitch);
|
|
121
|
+
const rx = (sx - this.viewportWidth * .5) / this.zoom;
|
|
122
|
+
const ry = ((sy - this.viewportHeight * .5) / this.zoom + planeZ * this.cosPitch) / sinP;
|
|
123
|
+
const c = Math.cos(this.yaw);
|
|
124
|
+
const s = Math.sin(this.yaw);
|
|
125
|
+
out.x = this.focusX + rx * c - ry * s;
|
|
126
|
+
out.y = this.focusY + rx * s + ry * c;
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
/** Convert camera-relative input `(rightInput, downInput)` into world-plane movement `(vx, vy)` respecting current `yaw`! */
|
|
130
|
+
cameraInputToWorld(rightInput, downInput, out = {
|
|
131
|
+
x: 0,
|
|
132
|
+
y: 0
|
|
133
|
+
}) {
|
|
134
|
+
const c = Math.cos(this.yaw);
|
|
135
|
+
const s = Math.sin(this.yaw);
|
|
136
|
+
out.x = rightInput * c - downInput * s;
|
|
137
|
+
out.y = rightInput * s + downInput * c;
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
/** Compute axis-aligned world bounding box covering all 4 screen corners on the ground plane (for `ChunkedTilemap.syncViewport`). */
|
|
141
|
+
getGroundViewportBounds(out = {
|
|
142
|
+
left: 0,
|
|
143
|
+
top: 0,
|
|
144
|
+
width: 0,
|
|
145
|
+
height: 0
|
|
146
|
+
}) {
|
|
147
|
+
const p0 = this.screenToGround(0, 0);
|
|
148
|
+
const p1 = this.screenToGround(this.viewportWidth, 0);
|
|
149
|
+
const p2 = this.screenToGround(this.viewportWidth, this.viewportHeight);
|
|
150
|
+
const p3 = this.screenToGround(0, this.viewportHeight);
|
|
151
|
+
const minX = Math.min(p0.x, p1.x, p2.x, p3.x);
|
|
152
|
+
const maxX = Math.max(p0.x, p1.x, p2.x, p3.x);
|
|
153
|
+
const minY = Math.min(p0.y, p1.y, p2.y, p3.y);
|
|
154
|
+
const maxY = Math.max(p0.y, p1.y, p2.y, p3.y);
|
|
155
|
+
out.left = minX;
|
|
156
|
+
out.top = minY;
|
|
157
|
+
out.width = maxX - minX;
|
|
158
|
+
out.height = maxY - minY;
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/** Project a world-space sun direction `(sunAzimuthRad, sunElevationRad)` into screen-space oblique shadow vector. */
|
|
162
|
+
computeSunShadowVector(sunAzimuthRad, sunElevationRad, heightPx = 32, out = {
|
|
163
|
+
dx: 0,
|
|
164
|
+
dy: 0,
|
|
165
|
+
lengthRatio: 1
|
|
166
|
+
}) {
|
|
167
|
+
const lenRatio = Math.min(2.6, 1 / Math.tan(Math.max(.15, Math.min(Math.PI * .48, sunElevationRad))));
|
|
168
|
+
const worldRayX = -Math.cos(sunAzimuthRad) * heightPx * lenRatio;
|
|
169
|
+
const worldRayY = -Math.sin(sunAzimuthRad) * heightPx * lenRatio;
|
|
170
|
+
const c = Math.cos(this.yaw);
|
|
171
|
+
const s = Math.sin(this.yaw);
|
|
172
|
+
out.dx = (worldRayX * c + worldRayY * s) * this.zoom;
|
|
173
|
+
out.dy = (-worldRayX * s + worldRayY * c) * this.sinPitch * this.zoom;
|
|
174
|
+
out.lengthRatio = lenRatio;
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
/**
|
|
179
|
+
* Mount a complete *Don't Starve*-style 2.5D rotatable world rig onto a Stage `Scene`:
|
|
180
|
+
* - Attach any flat 2D map (`MountedChunkedTilemap.container`, ground decals, farm plots) into `groundWorld`
|
|
181
|
+
* - Register upright trees, tombstones, campfires, and `Rig2DView.container` characters via `registerBillboard()`
|
|
182
|
+
* Works identically across WebGL2, WebGPU, Canvas2D, and Node 60fps Headless!
|
|
183
|
+
*/
|
|
184
|
+
function mountStage25D(scene, camera, opts = {}) {
|
|
185
|
+
const root = scene.add.container(0, 0);
|
|
186
|
+
root.setScrollFactor(0, 0);
|
|
187
|
+
if (opts.depth !== void 0) root.setDepth(opts.depth);
|
|
188
|
+
if (opts.trackId) root.trackId = opts.trackId;
|
|
189
|
+
const tiltStage = scene.make.container(camera.viewportWidth * .5, camera.viewportHeight * .5);
|
|
190
|
+
tiltStage.zIndex = 0;
|
|
191
|
+
const yawStage = scene.make.container(0, 0);
|
|
192
|
+
const groundWorld = scene.make.container(-camera.focusX, -camera.focusY);
|
|
193
|
+
yawStage.add(groundWorld);
|
|
194
|
+
tiltStage.add(yawStage);
|
|
195
|
+
root.add(tiltStage);
|
|
196
|
+
const billboardLayer = scene.make.container(0, 0);
|
|
197
|
+
billboardLayer.zIndex = 10;
|
|
198
|
+
root.add(billboardLayer);
|
|
199
|
+
const entries = [];
|
|
200
|
+
const projScratch = {
|
|
201
|
+
x: 0,
|
|
202
|
+
y: 0,
|
|
203
|
+
depth: 0,
|
|
204
|
+
scale: 1
|
|
205
|
+
};
|
|
206
|
+
const sync = () => {
|
|
207
|
+
tiltStage.x = camera.viewportWidth * .5;
|
|
208
|
+
tiltStage.y = camera.viewportHeight * .5;
|
|
209
|
+
tiltStage.scaleX = camera.zoom;
|
|
210
|
+
tiltStage.scaleY = camera.zoom * camera.sinPitch;
|
|
211
|
+
yawStage.rotation = -camera.yaw;
|
|
212
|
+
groundWorld.x = -camera.focusX;
|
|
213
|
+
groundWorld.y = -camera.focusY;
|
|
214
|
+
for (let i = 0; i < entries.length; i++) {
|
|
215
|
+
const e = entries[i];
|
|
216
|
+
camera.worldToScreen(e.wx, e.wy, e.wz, projScratch);
|
|
217
|
+
e.node.x = projScratch.x;
|
|
218
|
+
e.node.y = projScratch.y;
|
|
219
|
+
e.node.scaleX = e.baseScaleX * projScratch.scale;
|
|
220
|
+
e.node.scaleY = e.baseScaleY * projScratch.scale;
|
|
221
|
+
e.node.zIndex = Math.round((projScratch.depth + e.depthBias) * 100);
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
sync();
|
|
225
|
+
return {
|
|
226
|
+
camera,
|
|
227
|
+
root,
|
|
228
|
+
groundWorld,
|
|
229
|
+
billboardLayer,
|
|
230
|
+
registerBillboard(node, wx, wy, bOpts = {}) {
|
|
231
|
+
node.removeFromDisplayList();
|
|
232
|
+
billboardLayer.add(node);
|
|
233
|
+
const entry = {
|
|
234
|
+
node,
|
|
235
|
+
wx,
|
|
236
|
+
wy,
|
|
237
|
+
wz: bOpts.wz ?? 0,
|
|
238
|
+
baseScaleX: bOpts.baseScaleX ?? node.scaleX,
|
|
239
|
+
baseScaleY: bOpts.baseScaleY ?? node.scaleY,
|
|
240
|
+
depthBias: bOpts.depthBias ?? 0
|
|
241
|
+
};
|
|
242
|
+
entries.push(entry);
|
|
243
|
+
return entry;
|
|
244
|
+
},
|
|
245
|
+
removeBillboard(entryOrNode) {
|
|
246
|
+
const idx = entries.findIndex((e) => e === entryOrNode || e.node === entryOrNode);
|
|
247
|
+
if (idx >= 0) {
|
|
248
|
+
const [removed] = entries.splice(idx, 1);
|
|
249
|
+
billboardLayer.remove(removed.node);
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
update(deltaMs = 0) {
|
|
253
|
+
if (deltaMs > 0) camera.step(deltaMs);
|
|
254
|
+
sync();
|
|
255
|
+
},
|
|
256
|
+
destroy() {
|
|
257
|
+
entries.length = 0;
|
|
258
|
+
root.destroy();
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
export { Camera25D, mountStage25D };
|
package/dist/rig2d.d.ts
ADDED
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
import { Yt as BlendMode, bt as ImageObject, jt as SceneHostLike, ut as Container, yt as Graphics } from "./game-GdPaQUqH.js";
|
|
2
|
+
export type RigCurveKind = 'linear' | 'step' | 'smoothstep' | 'outBack';
|
|
3
|
+
export interface RigBoneDef {
|
|
4
|
+
name: string;
|
|
5
|
+
parent?: string | null;
|
|
6
|
+
x?: number;
|
|
7
|
+
y?: number;
|
|
8
|
+
rotation?: number;
|
|
9
|
+
scaleX?: number;
|
|
10
|
+
scaleY?: number;
|
|
11
|
+
length?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface RigSlotDef {
|
|
14
|
+
name: string;
|
|
15
|
+
bone: string;
|
|
16
|
+
defaultAttachment?: string | null;
|
|
17
|
+
zOrder?: number;
|
|
18
|
+
tint?: number;
|
|
19
|
+
alpha?: number;
|
|
20
|
+
blendMode?: BlendMode;
|
|
21
|
+
}
|
|
22
|
+
export interface RigAttachmentDef {
|
|
23
|
+
id: string;
|
|
24
|
+
textureKey: string;
|
|
25
|
+
frame?: string | number;
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
originX?: number;
|
|
29
|
+
originY?: number;
|
|
30
|
+
x?: number;
|
|
31
|
+
y?: number;
|
|
32
|
+
rotation?: number;
|
|
33
|
+
scaleX?: number;
|
|
34
|
+
scaleY?: number;
|
|
35
|
+
tint?: number;
|
|
36
|
+
alpha?: number;
|
|
37
|
+
}
|
|
38
|
+
export interface RigKeyframe {
|
|
39
|
+
timeMs: number;
|
|
40
|
+
x?: number;
|
|
41
|
+
y?: number;
|
|
42
|
+
rotation?: number;
|
|
43
|
+
scaleX?: number;
|
|
44
|
+
scaleY?: number;
|
|
45
|
+
curve?: RigCurveKind;
|
|
46
|
+
}
|
|
47
|
+
export interface RigEventKeyframe {
|
|
48
|
+
timeMs: number;
|
|
49
|
+
name: string;
|
|
50
|
+
}
|
|
51
|
+
export interface RigClipDef {
|
|
52
|
+
name: string;
|
|
53
|
+
durationMs: number;
|
|
54
|
+
loop?: boolean;
|
|
55
|
+
/** Keyframe tracks keyed by bone name; values are **additive** (`x`, `y`, `rotation`) or **multiplicative** (`scaleX`, `scaleY`) relative to rest pose. */
|
|
56
|
+
tracks: Record<string, RigKeyframe[]>;
|
|
57
|
+
/** Optional slot attachment overrides timed within the clip (e.g. blink / mouth swap). */
|
|
58
|
+
slotTracks?: Record<string, Array<{
|
|
59
|
+
timeMs: number;
|
|
60
|
+
attachment: string | null;
|
|
61
|
+
}>>;
|
|
62
|
+
events?: RigEventKeyframe[];
|
|
63
|
+
}
|
|
64
|
+
export interface RigSkeletonDef {
|
|
65
|
+
id: string;
|
|
66
|
+
bones: RigBoneDef[];
|
|
67
|
+
slots: RigSlotDef[];
|
|
68
|
+
attachments: Record<string, RigAttachmentDef>;
|
|
69
|
+
skins?: Record<string, Record<string, string | null>>;
|
|
70
|
+
clips?: Record<string, RigClipDef>;
|
|
71
|
+
}
|
|
72
|
+
export interface CompiledBone {
|
|
73
|
+
readonly index: number;
|
|
74
|
+
readonly name: string;
|
|
75
|
+
readonly parentIndex: number;
|
|
76
|
+
readonly restX: number;
|
|
77
|
+
readonly restY: number;
|
|
78
|
+
readonly restRotation: number;
|
|
79
|
+
readonly restScaleX: number;
|
|
80
|
+
readonly restScaleY: number;
|
|
81
|
+
readonly length: number;
|
|
82
|
+
}
|
|
83
|
+
export interface CompiledSlot {
|
|
84
|
+
readonly index: number;
|
|
85
|
+
readonly name: string;
|
|
86
|
+
readonly boneIndex: number;
|
|
87
|
+
readonly defaultAttachment: string | null;
|
|
88
|
+
readonly zOrder: number;
|
|
89
|
+
readonly restTint: number;
|
|
90
|
+
readonly restAlpha: number;
|
|
91
|
+
readonly blendMode: BlendMode;
|
|
92
|
+
}
|
|
93
|
+
export interface Rig2DSkeleton {
|
|
94
|
+
readonly id: string;
|
|
95
|
+
readonly bones: ReadonlyArray<CompiledBone>;
|
|
96
|
+
readonly boneIndexByName: ReadonlyMap<string, number>;
|
|
97
|
+
readonly slots: ReadonlyArray<CompiledSlot>;
|
|
98
|
+
readonly slotIndexByName: ReadonlyMap<string, number>;
|
|
99
|
+
readonly attachments: ReadonlyMap<string, RigAttachmentDef>;
|
|
100
|
+
readonly skins: ReadonlyMap<string, Record<string, string | null>>;
|
|
101
|
+
readonly clips: ReadonlyMap<string, RigClipDef>;
|
|
102
|
+
}
|
|
103
|
+
export interface RigPlayOptions {
|
|
104
|
+
loop?: boolean;
|
|
105
|
+
speed?: number;
|
|
106
|
+
crossfadeMs?: number;
|
|
107
|
+
restart?: boolean;
|
|
108
|
+
}
|
|
109
|
+
export interface RigOverlayOptions {
|
|
110
|
+
/** Restrict overlay animation to these bones (and optionally their descendants if `includeChildren !== false`). */
|
|
111
|
+
bones?: string[];
|
|
112
|
+
includeChildren?: boolean;
|
|
113
|
+
loop?: boolean;
|
|
114
|
+
speed?: number;
|
|
115
|
+
weight?: number;
|
|
116
|
+
blendInMs?: number;
|
|
117
|
+
blendOutMs?: number;
|
|
118
|
+
}
|
|
119
|
+
export interface RigWorldTransform {
|
|
120
|
+
x: number;
|
|
121
|
+
y: number;
|
|
122
|
+
rotation: number;
|
|
123
|
+
scaleX: number;
|
|
124
|
+
scaleY: number;
|
|
125
|
+
a: number;
|
|
126
|
+
b: number;
|
|
127
|
+
c: number;
|
|
128
|
+
d: number;
|
|
129
|
+
tx: number;
|
|
130
|
+
ty: number;
|
|
131
|
+
}
|
|
132
|
+
export type RigEventListener = (eventName: string, clipName: string, timeMs: number) => void;
|
|
133
|
+
/** Shortest signed angular delta in `[-PI, PI]`. */
|
|
134
|
+
export declare function wrapAngleDelta(fromRad: number, toRad: number): number;
|
|
135
|
+
export declare function evalRigCurve(t: number, kind?: RigCurveKind): number;
|
|
136
|
+
/** Validate and topologically sort a 2D skeletal cutout rig definition (`parentIndex < boneIndex`). */
|
|
137
|
+
export declare function defineRig2DSkeleton(def: RigSkeletonDef): Rig2DSkeleton;
|
|
138
|
+
/** Zero-GC runtime instance of a `Rig2DSkeleton` with FK solver, slot equipment table, and 2-layer clip mixer. */
|
|
139
|
+
export declare class Rig2DInstance {
|
|
140
|
+
readonly skeleton: Rig2DSkeleton;
|
|
141
|
+
x: number;
|
|
142
|
+
y: number;
|
|
143
|
+
rotation: number;
|
|
144
|
+
scaleX: number;
|
|
145
|
+
scaleY: number;
|
|
146
|
+
flipX: boolean;
|
|
147
|
+
flipY: boolean;
|
|
148
|
+
readonly boneLocalX: Float32Array;
|
|
149
|
+
readonly boneLocalY: Float32Array;
|
|
150
|
+
readonly boneLocalRot: Float32Array;
|
|
151
|
+
readonly boneLocalScaleX: Float32Array;
|
|
152
|
+
readonly boneLocalScaleY: Float32Array;
|
|
153
|
+
readonly boneManualX: Float32Array;
|
|
154
|
+
readonly boneManualY: Float32Array;
|
|
155
|
+
readonly boneManualRot: Float32Array;
|
|
156
|
+
readonly boneWorldMat: Float32Array;
|
|
157
|
+
readonly boneWorldRot: Float32Array;
|
|
158
|
+
readonly boneWorldScaleX: Float32Array;
|
|
159
|
+
readonly boneWorldScaleY: Float32Array;
|
|
160
|
+
private readonly slotAttachments;
|
|
161
|
+
readonly slotTint: Uint32Array;
|
|
162
|
+
readonly slotAlpha: Float32Array;
|
|
163
|
+
readonly slotZOrder: Int32Array;
|
|
164
|
+
private baseClip;
|
|
165
|
+
private baseTimeMs;
|
|
166
|
+
private baseSpeed;
|
|
167
|
+
private baseLoop;
|
|
168
|
+
private prevClip;
|
|
169
|
+
private prevTimeMs;
|
|
170
|
+
private crossfadeElapsedMs;
|
|
171
|
+
private crossfadeDurationMs;
|
|
172
|
+
private overlayClip;
|
|
173
|
+
private overlayTimeMs;
|
|
174
|
+
private overlaySpeed;
|
|
175
|
+
private overlayLoop;
|
|
176
|
+
private overlayWeight;
|
|
177
|
+
private overlayBlendInMs;
|
|
178
|
+
private overlayBlendOutMs;
|
|
179
|
+
private overlayMask;
|
|
180
|
+
private readonly sampleA;
|
|
181
|
+
private readonly sampleB;
|
|
182
|
+
private readonly listeners;
|
|
183
|
+
private worldDirty;
|
|
184
|
+
constructor(skeleton: Rig2DSkeleton);
|
|
185
|
+
/** Reset all bones, slots, and attachments to the skeleton's rest setup pose. */
|
|
186
|
+
resetToSetupPose(): this;
|
|
187
|
+
setPosition(x: number, y: number): this;
|
|
188
|
+
setScale(sx: number, sy?: number): this;
|
|
189
|
+
setFlip(flipX: boolean, flipY?: boolean): this;
|
|
190
|
+
/** Swap a single slot's equipped cutout part in O(1) (`null` hides/unequips the slot). */
|
|
191
|
+
setAttachment(slotName: string, attachmentId: string | null): this;
|
|
192
|
+
getAttachment(slotName: string): string | null;
|
|
193
|
+
getSlotAttachmentByIndex(slotIndex: number): RigAttachmentDef | null;
|
|
194
|
+
/** Apply a named skin from the skeleton or an explicit `{ slotName: attachmentId | null }` wardrobe map. */
|
|
195
|
+
applySkin(skinNameOrMap: string | Record<string, string | null>): this;
|
|
196
|
+
setSlotTint(slotName: string, tintRgb: number, alpha?: number): this;
|
|
197
|
+
/** Apply an additive procedural bone offset (e.g. aiming arm or head tracking cursor). */
|
|
198
|
+
setBoneManualOffset(boneName: string, dx?: number, dy?: number, drot?: number): this;
|
|
199
|
+
onEvent(fn: RigEventListener): () => void;
|
|
200
|
+
get currentClipName(): string | null;
|
|
201
|
+
get activeOverlayClipName(): string | null;
|
|
202
|
+
/** Start or crossfade to a base locomotion/idle animation clip. */
|
|
203
|
+
play(clipOrName: string | RigClipDef, opts?: RigPlayOptions): this;
|
|
204
|
+
/** Play an action overlay clip (e.g. axe chop, pickaxe mine, lantern raise) masked onto specific bones. */
|
|
205
|
+
playOverlay(clipOrName: string | RigClipDef, opts?: RigOverlayOptions): this;
|
|
206
|
+
clearOverlay(): this;
|
|
207
|
+
/** Deterministically advance animation tracks by `deltaMs` and fire any crossed keyframe events. */
|
|
208
|
+
step(deltaMs: number): this;
|
|
209
|
+
private advanceClipTime;
|
|
210
|
+
private emitEventsInRange;
|
|
211
|
+
private evaluatePose;
|
|
212
|
+
/** Solve hierarchical 2D Forward Kinematics across all bones in topological order (zero heap allocation). */
|
|
213
|
+
solveWorldTransforms(): this;
|
|
214
|
+
/** Query a bone's solved world-space position & orientation (ideal for attaching torches, lights, particles, or hitboxes). */
|
|
215
|
+
getBoneWorldTransform(boneName: string, localOffsetX?: number, localOffsetY?: number, out?: RigWorldTransform): RigWorldTransform;
|
|
216
|
+
}
|
|
217
|
+
export interface MountRig2DOptions {
|
|
218
|
+
trackId?: string;
|
|
219
|
+
depth?: number;
|
|
220
|
+
contactShadow?: {
|
|
221
|
+
rx: number;
|
|
222
|
+
ry: number;
|
|
223
|
+
alpha?: number;
|
|
224
|
+
color?: number;
|
|
225
|
+
offsetY?: number;
|
|
226
|
+
} | false;
|
|
227
|
+
}
|
|
228
|
+
export interface Rig2DView {
|
|
229
|
+
readonly container: Container;
|
|
230
|
+
readonly instance: Rig2DInstance;
|
|
231
|
+
readonly shadow: Graphics | null;
|
|
232
|
+
readonly slotImages: ReadonlyArray<ImageObject>;
|
|
233
|
+
/** Step animation + sync `Container` and slot `ImageObject`s in a single call. */
|
|
234
|
+
update(deltaMs?: number): void;
|
|
235
|
+
/** Sync display nodes from current `Rig2DInstance` state without advancing time. */
|
|
236
|
+
sync(): void;
|
|
237
|
+
destroy(): void;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Mount a `Rig2DInstance` onto a Stage `Scene` using nested `Container` + `ImageObject` nodes.
|
|
241
|
+
* Works identically across WebGL2 (single-batch when sharing a cutout atlas!), WebGPU, Canvas2D, and Node 60fps Headless.
|
|
242
|
+
*/
|
|
243
|
+
export declare function mountRig2D(scene: SceneHostLike, instance: Rig2DInstance, opts?: MountRig2DOptions): Rig2DView;
|
|
244
|
+
export type CutoutShapeOp = {
|
|
245
|
+
kind: 'roundRect';
|
|
246
|
+
x: number;
|
|
247
|
+
y: number;
|
|
248
|
+
w: number;
|
|
249
|
+
h: number;
|
|
250
|
+
r: number;
|
|
251
|
+
fill?: number;
|
|
252
|
+
stroke?: number;
|
|
253
|
+
lineWidth?: number;
|
|
254
|
+
} | {
|
|
255
|
+
kind: 'ellipse';
|
|
256
|
+
cx: number;
|
|
257
|
+
cy: number;
|
|
258
|
+
rx: number;
|
|
259
|
+
ry: number;
|
|
260
|
+
fill?: number;
|
|
261
|
+
stroke?: number;
|
|
262
|
+
lineWidth?: number;
|
|
263
|
+
} | {
|
|
264
|
+
kind: 'circle';
|
|
265
|
+
cx: number;
|
|
266
|
+
cy: number;
|
|
267
|
+
r: number;
|
|
268
|
+
fill?: number;
|
|
269
|
+
stroke?: number;
|
|
270
|
+
lineWidth?: number;
|
|
271
|
+
} | {
|
|
272
|
+
kind: 'poly';
|
|
273
|
+
points: Array<[number, number]>;
|
|
274
|
+
fill?: number;
|
|
275
|
+
stroke?: number;
|
|
276
|
+
lineWidth?: number;
|
|
277
|
+
close?: boolean;
|
|
278
|
+
} | {
|
|
279
|
+
kind: 'line';
|
|
280
|
+
x1: number;
|
|
281
|
+
y1: number;
|
|
282
|
+
x2: number;
|
|
283
|
+
y2: number;
|
|
284
|
+
stroke: number;
|
|
285
|
+
lineWidth?: number;
|
|
286
|
+
};
|
|
287
|
+
export interface CutoutPartSpec {
|
|
288
|
+
id: string;
|
|
289
|
+
width: number;
|
|
290
|
+
height: number;
|
|
291
|
+
originX?: number;
|
|
292
|
+
originY?: number;
|
|
293
|
+
offsetX?: number;
|
|
294
|
+
offsetY?: number;
|
|
295
|
+
rotation?: number;
|
|
296
|
+
shapes: CutoutShapeOp[];
|
|
297
|
+
}
|
|
298
|
+
export interface BakedCutoutAtlas {
|
|
299
|
+
readonly textureKey: string;
|
|
300
|
+
readonly canvas: HTMLCanvasElement;
|
|
301
|
+
readonly width: number;
|
|
302
|
+
readonly height: number;
|
|
303
|
+
readonly frames: Record<string, {
|
|
304
|
+
x: number;
|
|
305
|
+
y: number;
|
|
306
|
+
w: number;
|
|
307
|
+
h: number;
|
|
308
|
+
}>;
|
|
309
|
+
readonly attachments: Record<string, RigAttachmentDef>;
|
|
310
|
+
/** Register the packed cutout sheet + all named sub-frames onto a Stage `Scene`'s `TextureManager`. */
|
|
311
|
+
registerOnScene(scene: SceneHostLike): void;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Pack procedural 2D cutout parts (heads, hats, robes, axes, pickaxes, torches, shields) onto a single padded atlas canvas
|
|
315
|
+
* with crisp hand-drawn outlines — zero external PNG files needed!
|
|
316
|
+
*/
|
|
317
|
+
export declare function bakeCutoutAtlas(textureKey: string, parts: CutoutPartSpec[], createCanvasFn?: (w: number, h: number) => HTMLCanvasElement, padding?: number): BakedCutoutAtlas;
|