@holtjs/stage 0.4.1 → 0.5.1
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 +19 -0
- package/README.md +140 -119
- package/dist/{game-Cu1G9xgW.js → game-C5za2LGl.js} +28 -691
- package/dist/{game-GdPaQUqH.d.ts → game-CfUZiazr.d.ts} +9 -2
- package/dist/headless.d.ts +1 -1
- package/dist/headless.js +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/joystick.d.ts +1 -1
- package/dist/kernel.d.ts +1 -1
- 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/{renderer-webgpu-BorAD6Kk.js → renderer-webgpu-BE6fTBzo.js} +718 -1
- package/dist/renderer-webgpu.d.ts +1 -1
- package/dist/renderer-webgpu.js +1 -1
- 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/trace.d.ts +1 -1
- 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 };
|