@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
package/dist/lighting.js
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
import { o as FilterMode, r as BlendModes } from "./types-DRFB0Klc.js";
|
|
2
|
+
// src/lighting.ts
|
|
3
|
+
function lerpChannel(a, b, t) {
|
|
4
|
+
return Math.round(a + (b - a) * t) & 255;
|
|
5
|
+
}
|
|
6
|
+
function lerpRgb(rgbA, rgbB, t) {
|
|
7
|
+
const u = Math.max(0, Math.min(1, t));
|
|
8
|
+
const r = lerpChannel(rgbA >> 16 & 255, rgbB >> 16 & 255, u);
|
|
9
|
+
const g = lerpChannel(rgbA >> 8 & 255, rgbB >> 8 & 255, u);
|
|
10
|
+
const b = lerpChannel(rgbA & 255, rgbB & 255, u);
|
|
11
|
+
return r << 16 | g << 8 | b;
|
|
12
|
+
}
|
|
13
|
+
/** Deterministic *Don't Starve* / *Graveyard Keeper* Day -> Dusk -> Night clock controller. */
|
|
14
|
+
var DayNightCycle = class {
|
|
15
|
+
dayLengthMs;
|
|
16
|
+
dayRatio;
|
|
17
|
+
duskRatio;
|
|
18
|
+
time01;
|
|
19
|
+
dayCount = 1;
|
|
20
|
+
constructor(cfg = {}) {
|
|
21
|
+
this.dayLengthMs = Math.max(1e3, cfg.dayLengthMs ?? 48e4);
|
|
22
|
+
this.dayRatio = Math.max(.1, Math.min(.85, cfg.dayRatio ?? .5));
|
|
23
|
+
this.duskRatio = Math.max(.05, Math.min(.85 - this.dayRatio, cfg.duskRatio ?? .25));
|
|
24
|
+
this.time01 = ((cfg.initialTime01 ?? .15) % 1 + 1) % 1;
|
|
25
|
+
}
|
|
26
|
+
step(deltaMs) {
|
|
27
|
+
if (deltaMs > 0) {
|
|
28
|
+
const next = this.time01 + deltaMs / this.dayLengthMs;
|
|
29
|
+
if (next >= 1) {
|
|
30
|
+
this.dayCount += Math.floor(next);
|
|
31
|
+
this.time01 = next % 1;
|
|
32
|
+
} else this.time01 = next;
|
|
33
|
+
}
|
|
34
|
+
return this.sample();
|
|
35
|
+
}
|
|
36
|
+
setTime01(t) {
|
|
37
|
+
this.time01 = (t % 1 + 1) % 1;
|
|
38
|
+
return this.sample();
|
|
39
|
+
}
|
|
40
|
+
sample(out) {
|
|
41
|
+
const t = this.time01;
|
|
42
|
+
const duskStart = this.dayRatio;
|
|
43
|
+
const nightStart = this.dayRatio + this.duskRatio;
|
|
44
|
+
let phase;
|
|
45
|
+
let ambientRgb;
|
|
46
|
+
let darkness;
|
|
47
|
+
let sunElevationRad;
|
|
48
|
+
const DAY_COLOR = 0xfff8eb;
|
|
49
|
+
const DUSK_COLOR = 0xc97a5b;
|
|
50
|
+
const NIGHT_COLOR = 1449267;
|
|
51
|
+
if (t < duskStart) {
|
|
52
|
+
phase = "day";
|
|
53
|
+
const u = t / duskStart;
|
|
54
|
+
if (u < .15) {
|
|
55
|
+
const w = u / .15;
|
|
56
|
+
ambientRgb = lerpRgb(DUSK_COLOR, DAY_COLOR, w);
|
|
57
|
+
darkness = .35 * (1 - w);
|
|
58
|
+
} else if (u > .85) {
|
|
59
|
+
const w = (u - .85) / .15;
|
|
60
|
+
ambientRgb = lerpRgb(DAY_COLOR, 0xf6c28b, w);
|
|
61
|
+
darkness = .12 * w;
|
|
62
|
+
} else {
|
|
63
|
+
ambientRgb = DAY_COLOR;
|
|
64
|
+
darkness = 0;
|
|
65
|
+
}
|
|
66
|
+
sunElevationRad = .35 + Math.sin(u * Math.PI) * .95;
|
|
67
|
+
} else if (t < nightStart) {
|
|
68
|
+
phase = "dusk";
|
|
69
|
+
const u = (t - duskStart) / Math.max(1e-4, this.duskRatio);
|
|
70
|
+
ambientRgb = u < .5 ? lerpRgb(0xf6c28b, DUSK_COLOR, u * 2) : lerpRgb(DUSK_COLOR, NIGHT_COLOR, (u - .5) * 2);
|
|
71
|
+
darkness = .12 + u * .74;
|
|
72
|
+
sunElevationRad = .35 * (1 - u) + .18 * u;
|
|
73
|
+
} else {
|
|
74
|
+
phase = "night";
|
|
75
|
+
const nightSpan = Math.max(1e-4, 1 - nightStart);
|
|
76
|
+
const u = (t - nightStart) / nightSpan;
|
|
77
|
+
if (u > .85) {
|
|
78
|
+
const w = (u - .85) / .15;
|
|
79
|
+
ambientRgb = lerpRgb(NIGHT_COLOR, DUSK_COLOR, w);
|
|
80
|
+
darkness = .88 - w * .53;
|
|
81
|
+
} else {
|
|
82
|
+
ambientRgb = NIGHT_COLOR;
|
|
83
|
+
darkness = .88;
|
|
84
|
+
}
|
|
85
|
+
sunElevationRad = .32 + Math.sin(u * Math.PI) * .45;
|
|
86
|
+
}
|
|
87
|
+
const sunAzimuthRad = -Math.PI * .75 + t * Math.PI * 2;
|
|
88
|
+
const target = out ?? {
|
|
89
|
+
time01: 0,
|
|
90
|
+
dayCount: 1,
|
|
91
|
+
phase: "day",
|
|
92
|
+
ambientRgb: DAY_COLOR,
|
|
93
|
+
darkness: 0,
|
|
94
|
+
sunAzimuthRad: 0,
|
|
95
|
+
sunElevationRad: 1
|
|
96
|
+
};
|
|
97
|
+
target.time01 = t;
|
|
98
|
+
target.dayCount = this.dayCount;
|
|
99
|
+
target.phase = phase;
|
|
100
|
+
target.ambientRgb = ambientRgb;
|
|
101
|
+
target.darkness = darkness;
|
|
102
|
+
target.sunAzimuthRad = sunAzimuthRad;
|
|
103
|
+
target.sunElevationRad = sunElevationRad;
|
|
104
|
+
return target;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Compute a tangent-space RGBA normal map (`R = nx*0.5+0.5`, `G = ny*0.5+0.5`, `B = nz*0.5+0.5`, `A = sourceAlpha`)
|
|
109
|
+
* from raw `Uint8ClampedArray` RGBA pixels using a 3x3 Sobel height gradient.
|
|
110
|
+
* Pure zero-DOM function usable in Node, Workers, and browsers!
|
|
111
|
+
*/
|
|
112
|
+
function computeNormalMapPixels(rgba, width, height, opts = {}) {
|
|
113
|
+
const strength = opts.strength ?? 2.4;
|
|
114
|
+
const lumW = opts.luminanceWeight ?? .35;
|
|
115
|
+
const n = width * height;
|
|
116
|
+
const hField = new Float32Array(n);
|
|
117
|
+
for (let i = 0; i < n; i++) {
|
|
118
|
+
const o = i * 4;
|
|
119
|
+
const a = rgba[o + 3] / 255;
|
|
120
|
+
const lum = (rgba[o] * .299 + rgba[o + 1] * .587 + rgba[o + 2] * .114) / 255;
|
|
121
|
+
hField[i] = a * (1 - lumW + lum * lumW);
|
|
122
|
+
}
|
|
123
|
+
const smoothed = new Float32Array(n);
|
|
124
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
125
|
+
let sum = 0;
|
|
126
|
+
let cnt = 0;
|
|
127
|
+
for (let dy = -2; dy <= 2; dy++) {
|
|
128
|
+
const ny = Math.max(0, Math.min(height - 1, y + dy));
|
|
129
|
+
for (let dx = -2; dx <= 2; dx++) {
|
|
130
|
+
const nx = Math.max(0, Math.min(width - 1, x + dx));
|
|
131
|
+
sum += hField[ny * width + nx];
|
|
132
|
+
cnt++;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
smoothed[y * width + x] = hField[y * width + x] * .45 + sum / cnt * .55;
|
|
136
|
+
}
|
|
137
|
+
const out = new Uint8ClampedArray(n * 4);
|
|
138
|
+
const sampleH = (x, y) => {
|
|
139
|
+
const cx = Math.max(0, Math.min(width - 1, x));
|
|
140
|
+
const cy = Math.max(0, Math.min(height - 1, y));
|
|
141
|
+
return smoothed[cy * width + cx];
|
|
142
|
+
};
|
|
143
|
+
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
|
144
|
+
const idx = y * width + x;
|
|
145
|
+
const srcAlpha = rgba[idx * 4 + 3];
|
|
146
|
+
if (srcAlpha === 0) {
|
|
147
|
+
out[idx * 4] = 128;
|
|
148
|
+
out[idx * 4 + 1] = 128;
|
|
149
|
+
out[idx * 4 + 2] = 255;
|
|
150
|
+
out[idx * 4 + 3] = 0;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
const tl = sampleH(x - 1, y - 1);
|
|
154
|
+
const tc = sampleH(x, y - 1);
|
|
155
|
+
const tr = sampleH(x + 1, y - 1);
|
|
156
|
+
const ml = sampleH(x - 1, y);
|
|
157
|
+
const mr = sampleH(x + 1, y);
|
|
158
|
+
const bl = sampleH(x - 1, y + 1);
|
|
159
|
+
const bc = sampleH(x, y + 1);
|
|
160
|
+
const br = sampleH(x + 1, y + 1);
|
|
161
|
+
const dHx = tr + 2 * mr + br - (tl + 2 * ml + bl);
|
|
162
|
+
const dHy = bl + 2 * bc + br - (tl + 2 * tc + tr);
|
|
163
|
+
let nx = -dHx * strength;
|
|
164
|
+
let ny = -dHy * strength;
|
|
165
|
+
let nz = 1;
|
|
166
|
+
const invLen = 1 / Math.hypot(nx, ny, nz);
|
|
167
|
+
nx *= invLen;
|
|
168
|
+
ny *= invLen;
|
|
169
|
+
nz *= invLen;
|
|
170
|
+
out[idx * 4] = Math.round((nx * .5 + .5) * 255);
|
|
171
|
+
out[idx * 4 + 1] = Math.round((ny * .5 + .5) * 255);
|
|
172
|
+
out[idx * 4 + 2] = Math.round((nz * .5 + .5) * 255);
|
|
173
|
+
out[idx * 4 + 3] = srcAlpha;
|
|
174
|
+
}
|
|
175
|
+
return out;
|
|
176
|
+
}
|
|
177
|
+
/** Bake a tangent-space normal map `HTMLCanvasElement` directly from any 2D sprite/cutout atlas canvas! */
|
|
178
|
+
function bakeNormalMapCanvas(sourceCanvas, createCanvasFn, opts = {}) {
|
|
179
|
+
const w = sourceCanvas.width;
|
|
180
|
+
const h = sourceCanvas.height;
|
|
181
|
+
const srcCtx = sourceCanvas.getContext("2d");
|
|
182
|
+
if (!srcCtx) throw new Error("bakeNormalMapCanvas: source 2D context unavailable");
|
|
183
|
+
const normalBytes = computeNormalMapPixels(srcCtx.getImageData(0, 0, w, h).data, w, h, opts);
|
|
184
|
+
const dst = (createCanvasFn ?? ((cw, ch) => {
|
|
185
|
+
const c = document.createElement("canvas");
|
|
186
|
+
c.width = cw;
|
|
187
|
+
c.height = ch;
|
|
188
|
+
return c;
|
|
189
|
+
}))(w, h);
|
|
190
|
+
const dstCtx = dst.getContext("2d");
|
|
191
|
+
if (!dstCtx) throw new Error("bakeNormalMapCanvas: destination 2D context unavailable");
|
|
192
|
+
const dstImg = dstCtx.createImageData(w, h);
|
|
193
|
+
dstImg.data.set(normalBytes);
|
|
194
|
+
dstCtx.putImageData(dstImg, 0, 0);
|
|
195
|
+
return dst;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Evaluate exact 2D normal-mapped Lambertian + specular lighting at pixel `(px, py)` for unit-testing & CPU reference parity.
|
|
199
|
+
*/
|
|
200
|
+
function evaluateNormalLitPixel(baseRgb, normalX, normalY, normalZ, px, py, ambientRgb, lights) {
|
|
201
|
+
const br = (baseRgb >> 16 & 255) / 255;
|
|
202
|
+
const bg = (baseRgb >> 8 & 255) / 255;
|
|
203
|
+
const bb = (baseRgb & 255) / 255;
|
|
204
|
+
let accR = (ambientRgb >> 16 & 255) / 255;
|
|
205
|
+
let accG = (ambientRgb >> 8 & 255) / 255;
|
|
206
|
+
let accB = (ambientRgb & 255) / 255;
|
|
207
|
+
const nLen = Math.hypot(normalX, normalY, normalZ) || 1;
|
|
208
|
+
const nx = normalX / nLen;
|
|
209
|
+
const ny = normalY / nLen;
|
|
210
|
+
const nz = normalZ / nLen;
|
|
211
|
+
for (let i = 0; i < lights.length; i++) {
|
|
212
|
+
const l = lights[i];
|
|
213
|
+
if (l.enabled === false || l.intensity <= 0 || l.radius <= 0) continue;
|
|
214
|
+
const dx = l.x - px;
|
|
215
|
+
const dy = l.y - py;
|
|
216
|
+
const dist2D = Math.hypot(dx, dy);
|
|
217
|
+
if (dist2D >= l.radius) continue;
|
|
218
|
+
const dz = l.z;
|
|
219
|
+
const inv3D = 1 / (Math.hypot(dx, dy, dz) || 1);
|
|
220
|
+
const lx = dx * inv3D;
|
|
221
|
+
const ly = dy * inv3D;
|
|
222
|
+
const lz = dz * inv3D;
|
|
223
|
+
const ndotl = Math.max(0, nx * lx + ny * ly + nz * lz);
|
|
224
|
+
const u = 1 - dist2D / l.radius;
|
|
225
|
+
const atten = u * u * l.intensity;
|
|
226
|
+
const hx = lx;
|
|
227
|
+
const hy = ly;
|
|
228
|
+
const hz = lz + 1;
|
|
229
|
+
const invH = 1 / (Math.hypot(hx, hy, hz) || 1);
|
|
230
|
+
const spec = Math.pow(Math.max(0, nx * hx * invH + ny * hy * invH + nz * hz * invH), 16) * .25;
|
|
231
|
+
const lr = (l.color >> 16 & 255) / 255;
|
|
232
|
+
const lg = (l.color >> 8 & 255) / 255;
|
|
233
|
+
const lb = (l.color & 255) / 255;
|
|
234
|
+
const factor = (ndotl + spec) * atten;
|
|
235
|
+
accR += lr * factor;
|
|
236
|
+
accG += lg * factor;
|
|
237
|
+
accB += lb * factor;
|
|
238
|
+
}
|
|
239
|
+
const r = Math.min(255, Math.round(br * accR * 255));
|
|
240
|
+
const g = Math.min(255, Math.round(bg * accG * 255));
|
|
241
|
+
const b = Math.min(255, Math.round(bb * accB * 255));
|
|
242
|
+
return r << 16 | g << 8 | b;
|
|
243
|
+
}
|
|
244
|
+
var Lighting2DSystem = class {
|
|
245
|
+
scene;
|
|
246
|
+
cycle;
|
|
247
|
+
overlayImage;
|
|
248
|
+
renderTarget;
|
|
249
|
+
lightGraphics;
|
|
250
|
+
lights = /* @__PURE__ */ new Map();
|
|
251
|
+
resScale;
|
|
252
|
+
viewportW;
|
|
253
|
+
viewportH;
|
|
254
|
+
elapsedSec = 0;
|
|
255
|
+
manualAmbientRgb = null;
|
|
256
|
+
constructor(scene, opts) {
|
|
257
|
+
this.scene = scene;
|
|
258
|
+
this.cycle = opts.cycle ?? new DayNightCycle();
|
|
259
|
+
this.resScale = Math.max(.125, Math.min(1, opts.resolutionScale ?? .5));
|
|
260
|
+
this.viewportW = Math.max(1, Math.round(opts.width));
|
|
261
|
+
this.viewportH = Math.max(1, Math.round(opts.height));
|
|
262
|
+
const rtKey = opts.key ?? `__stage_lightmap_${Math.random().toString(36).slice(2, 8)}`;
|
|
263
|
+
const rtW = Math.max(2, Math.round(this.viewportW * this.resScale));
|
|
264
|
+
const rtH = Math.max(2, Math.round(this.viewportH * this.resScale));
|
|
265
|
+
const rtHost = scene.renderer;
|
|
266
|
+
this.renderTarget = rtHost.createRenderTarget(rtKey, rtW, rtH, scene.textures, FilterMode.LINEAR);
|
|
267
|
+
this.lightGraphics = scene.make.graphics({ add: false });
|
|
268
|
+
this.lightGraphics.blendMode = BlendModes.ADD;
|
|
269
|
+
this.overlayImage = scene.add.image(0, 0, rtKey);
|
|
270
|
+
this.overlayImage.setOrigin(0, 0);
|
|
271
|
+
this.overlayImage.setScrollFactor(0, 0);
|
|
272
|
+
this.overlayImage.setDisplaySize(this.viewportW, this.viewportH);
|
|
273
|
+
this.overlayImage.setBlendMode(BlendModes.MULTIPLY);
|
|
274
|
+
if (opts.depth !== void 0) this.overlayImage.setDepth(opts.depth);
|
|
275
|
+
else this.overlayImage.setDepth(5e5);
|
|
276
|
+
}
|
|
277
|
+
addLight(light) {
|
|
278
|
+
const copy = {
|
|
279
|
+
enabled: true,
|
|
280
|
+
flicker: 0,
|
|
281
|
+
...light
|
|
282
|
+
};
|
|
283
|
+
this.lights.set(copy.id, copy);
|
|
284
|
+
return copy;
|
|
285
|
+
}
|
|
286
|
+
getLight(id) {
|
|
287
|
+
return this.lights.get(id);
|
|
288
|
+
}
|
|
289
|
+
removeLight(id) {
|
|
290
|
+
return this.lights.delete(id);
|
|
291
|
+
}
|
|
292
|
+
get lightCount() {
|
|
293
|
+
return this.lights.size;
|
|
294
|
+
}
|
|
295
|
+
setAmbientOverride(rgb) {
|
|
296
|
+
this.manualAmbientRgb = rgb;
|
|
297
|
+
}
|
|
298
|
+
resize(width, height) {
|
|
299
|
+
this.viewportW = Math.max(1, Math.round(width));
|
|
300
|
+
this.viewportH = Math.max(1, Math.round(height));
|
|
301
|
+
const rtW = Math.max(2, Math.round(this.viewportW * this.resScale));
|
|
302
|
+
const rtH = Math.max(2, Math.round(this.viewportH * this.resScale));
|
|
303
|
+
const oldKey = this.renderTarget.key;
|
|
304
|
+
this.renderTarget.destroy();
|
|
305
|
+
const newKey = `${oldKey}_r`;
|
|
306
|
+
const rtHost = this.scene.renderer;
|
|
307
|
+
this.renderTarget = rtHost.createRenderTarget(newKey, rtW, rtH, this.scene.textures, FilterMode.LINEAR);
|
|
308
|
+
this.overlayImage.setTexture(newKey);
|
|
309
|
+
this.overlayImage.setDisplaySize(this.viewportW, this.viewportH);
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Step the day/night cycle + torch flicker and render the `MULTIPLY` lightmap FBO!
|
|
313
|
+
* Pass `projectToScreen` when using `Camera25D`; otherwise defaults to `scene.cameras.main.worldToScreen`.
|
|
314
|
+
*/
|
|
315
|
+
update(deltaMs, projectToScreen) {
|
|
316
|
+
this.elapsedSec += Math.max(0, deltaMs) * .001;
|
|
317
|
+
const state = this.cycle.step(deltaMs);
|
|
318
|
+
const amb = this.manualAmbientRgb ?? state.ambientRgb;
|
|
319
|
+
const g = this.lightGraphics;
|
|
320
|
+
g.clear();
|
|
321
|
+
const s = this.resScale;
|
|
322
|
+
const pt = {
|
|
323
|
+
x: 0,
|
|
324
|
+
y: 0,
|
|
325
|
+
scale: 1
|
|
326
|
+
};
|
|
327
|
+
let idx = 0;
|
|
328
|
+
for (const l of this.lights.values()) {
|
|
329
|
+
idx++;
|
|
330
|
+
if (l.enabled === false || l.intensity <= 0 || l.radius <= 0) continue;
|
|
331
|
+
if (projectToScreen) projectToScreen(l.x, l.y, l.z, pt);
|
|
332
|
+
else {
|
|
333
|
+
const wpt = this.scene.cameras.main.worldToScreen(l.x, l.y);
|
|
334
|
+
pt.x = wpt.x;
|
|
335
|
+
pt.y = wpt.y;
|
|
336
|
+
pt.scale = this.scene.cameras.main.zoom;
|
|
337
|
+
}
|
|
338
|
+
const flick = l.flicker ? 1 + Math.sin(this.elapsedSec * 9.7 + idx * 2.3) * l.flicker * .6 + Math.sin(this.elapsedSec * 23.1 + idx * 5.1) * l.flicker * .4 : 1;
|
|
339
|
+
const rad = l.radius * pt.scale * s * flick;
|
|
340
|
+
const sx = pt.x * s;
|
|
341
|
+
const sy = pt.y * s;
|
|
342
|
+
const bands = 5;
|
|
343
|
+
for (let b = bands; b >= 1; b--) {
|
|
344
|
+
const frac = b / bands;
|
|
345
|
+
const alpha = Math.min(1, l.intensity * flick / bands * (1.25 - frac * .5));
|
|
346
|
+
g.fillStyle(l.color, alpha);
|
|
347
|
+
g.fillCircle(sx, sy, rad * frac);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
const cam = this.scene.cameras.main;
|
|
351
|
+
const prevBg = { ...cam.backgroundColor };
|
|
352
|
+
const prevTrans = cam.transparent;
|
|
353
|
+
cam.transparent = false;
|
|
354
|
+
cam.setBackgroundColor(amb);
|
|
355
|
+
this.scene.renderer.renderToTarget(this.renderTarget, { sorted: () => [g] }, cam);
|
|
356
|
+
cam.transparent = prevTrans;
|
|
357
|
+
cam.backgroundColor = prevBg;
|
|
358
|
+
this.overlayImage.setDisplaySize(this.viewportW, this.viewportH);
|
|
359
|
+
return state;
|
|
360
|
+
}
|
|
361
|
+
destroy() {
|
|
362
|
+
this.overlayImage.destroy();
|
|
363
|
+
this.lightGraphics.destroy();
|
|
364
|
+
this.renderTarget.destroy();
|
|
365
|
+
this.lights.clear();
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
const NORMAL_LIT_VS = `
|
|
369
|
+
attribute vec2 aCorner;
|
|
370
|
+
attribute vec2 aUv;
|
|
371
|
+
uniform vec2 uRes;
|
|
372
|
+
uniform vec4 uRect;
|
|
373
|
+
varying vec2 vUv;
|
|
374
|
+
varying vec2 vScreenPos;
|
|
375
|
+
void main() {
|
|
376
|
+
vec2 pos = uRect.xy + aCorner * uRect.zw;
|
|
377
|
+
vScreenPos = pos;
|
|
378
|
+
vUv = aUv;
|
|
379
|
+
vec2 clip = (pos / uRes) * 2.0 - 1.0;
|
|
380
|
+
gl_Position = vec4(clip.x, -clip.y, 0.0, 1.0);
|
|
381
|
+
}
|
|
382
|
+
`;
|
|
383
|
+
const NORMAL_LIT_FS = `
|
|
384
|
+
precision mediump float;
|
|
385
|
+
varying vec2 vUv;
|
|
386
|
+
varying vec2 vScreenPos;
|
|
387
|
+
uniform sampler2D uDiffuse;
|
|
388
|
+
uniform sampler2D uNormal;
|
|
389
|
+
uniform vec3 uAmbient;
|
|
390
|
+
uniform int uLightCount;
|
|
391
|
+
uniform vec4 uLightPosRad[8];
|
|
392
|
+
uniform vec4 uLightColInt[8];
|
|
393
|
+
void main() {
|
|
394
|
+
vec4 base = texture2D(uDiffuse, vUv);
|
|
395
|
+
if (base.a <= 0.004) discard;
|
|
396
|
+
vec3 nSample = texture2D(uNormal, vUv).xyz * 2.0 - 1.0;
|
|
397
|
+
vec3 N = normalize(nSample);
|
|
398
|
+
vec3 totalLight = uAmbient;
|
|
399
|
+
for (int i = 0; i < 8; i++) {
|
|
400
|
+
if (i >= uLightCount) break;
|
|
401
|
+
vec4 pr = uLightPosRad[i];
|
|
402
|
+
vec4 ci = uLightColInt[i];
|
|
403
|
+
vec2 d2 = pr.xy - vScreenPos;
|
|
404
|
+
float dist2D = length(d2);
|
|
405
|
+
if (dist2D < pr.w) {
|
|
406
|
+
vec3 L = normalize(vec3(d2, pr.z));
|
|
407
|
+
float ndotl = max(dot(N, L), 0.0);
|
|
408
|
+
float u = 1.0 - dist2D / pr.w;
|
|
409
|
+
float atten = u * u * ci.w;
|
|
410
|
+
vec3 H = normalize(L + vec3(0.0, 0.0, 1.0));
|
|
411
|
+
float spec = pow(max(dot(N, H), 0.0), 16.0) * 0.25;
|
|
412
|
+
totalLight += ci.rgb * ((ndotl + spec) * atten);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
vec3 rgb = base.rgb * totalLight;
|
|
416
|
+
gl_FragColor = vec4(rgb, base.a);
|
|
417
|
+
}
|
|
418
|
+
`;
|
|
419
|
+
/**
|
|
420
|
+
* Create a state-isolated WebGL2 `CustomPass` that renders normal-mapped 2D props/characters with per-pixel
|
|
421
|
+
* 3D tangent-space point lighting (`uDiffuse` + `uNormal`), gracefully no-oping on Canvas2D!
|
|
422
|
+
*/
|
|
423
|
+
function createNormalLitSpritePass(scene, diffuseKey, normalKey, getQuadsAndLights) {
|
|
424
|
+
let prog = null;
|
|
425
|
+
let vbo = null;
|
|
426
|
+
const posRadArr = /* @__PURE__ */ new Float32Array(32);
|
|
427
|
+
const colIntArr = /* @__PURE__ */ new Float32Array(32);
|
|
428
|
+
return scene.add.customPass((_pass, renderer) => {
|
|
429
|
+
const gl = renderer.gl;
|
|
430
|
+
if (!gl) return;
|
|
431
|
+
const diffTexObj = scene.textures.get(diffuseKey);
|
|
432
|
+
const normTexObj = scene.textures.get(normalKey);
|
|
433
|
+
if (!diffTexObj || !normTexObj) return;
|
|
434
|
+
const glRenderer = renderer;
|
|
435
|
+
const diffGl = glRenderer.ensureGlTexture(diffTexObj.source[0]);
|
|
436
|
+
const normGl = glRenderer.ensureGlTexture(normTexObj.source[0]);
|
|
437
|
+
if (!diffGl || !normGl) return;
|
|
438
|
+
if (!prog) {
|
|
439
|
+
const vs = gl.createShader(gl.VERTEX_SHADER);
|
|
440
|
+
gl.shaderSource(vs, NORMAL_LIT_VS);
|
|
441
|
+
gl.compileShader(vs);
|
|
442
|
+
const fs = gl.createShader(gl.FRAGMENT_SHADER);
|
|
443
|
+
gl.shaderSource(fs, NORMAL_LIT_FS);
|
|
444
|
+
gl.compileShader(fs);
|
|
445
|
+
prog = gl.createProgram();
|
|
446
|
+
gl.attachShader(prog, vs);
|
|
447
|
+
gl.attachShader(prog, fs);
|
|
448
|
+
gl.bindAttribLocation(prog, 0, "aCorner");
|
|
449
|
+
gl.bindAttribLocation(prog, 1, "aUv");
|
|
450
|
+
gl.linkProgram(prog);
|
|
451
|
+
gl.deleteShader(vs);
|
|
452
|
+
gl.deleteShader(fs);
|
|
453
|
+
vbo = gl.createBuffer();
|
|
454
|
+
}
|
|
455
|
+
if (!prog || !vbo) return;
|
|
456
|
+
const { ambientRgb, quads, lights } = getQuadsAndLights();
|
|
457
|
+
if (!quads.length) return;
|
|
458
|
+
gl.useProgram(prog);
|
|
459
|
+
glRenderer.bindTextureAt(0, diffGl);
|
|
460
|
+
glRenderer.bindTextureAt(1, normGl);
|
|
461
|
+
gl.uniform1i(gl.getUniformLocation(prog, "uDiffuse"), 0);
|
|
462
|
+
gl.uniform1i(gl.getUniformLocation(prog, "uNormal"), 1);
|
|
463
|
+
gl.uniform2f(gl.getUniformLocation(prog, "uRes"), glRenderer.width, glRenderer.height);
|
|
464
|
+
gl.uniform3f(gl.getUniformLocation(prog, "uAmbient"), (ambientRgb >> 16 & 255) / 255, (ambientRgb >> 8 & 255) / 255, (ambientRgb & 255) / 255);
|
|
465
|
+
const nL = Math.min(8, lights.length);
|
|
466
|
+
for (let i = 0; i < nL; i++) {
|
|
467
|
+
const l = lights[i];
|
|
468
|
+
posRadArr[i * 4] = l.screenX;
|
|
469
|
+
posRadArr[i * 4 + 1] = l.screenY;
|
|
470
|
+
posRadArr[i * 4 + 2] = l.z;
|
|
471
|
+
posRadArr[i * 4 + 3] = l.radius;
|
|
472
|
+
colIntArr[i * 4] = (l.color >> 16 & 255) / 255;
|
|
473
|
+
colIntArr[i * 4 + 1] = (l.color >> 8 & 255) / 255;
|
|
474
|
+
colIntArr[i * 4 + 2] = (l.color & 255) / 255;
|
|
475
|
+
colIntArr[i * 4 + 3] = l.intensity;
|
|
476
|
+
}
|
|
477
|
+
gl.uniform1i(gl.getUniformLocation(prog, "uLightCount"), nL);
|
|
478
|
+
gl.uniform4fv(gl.getUniformLocation(prog, "uLightPosRad[0]"), posRadArr);
|
|
479
|
+
gl.uniform4fv(gl.getUniformLocation(prog, "uLightColInt[0]"), colIntArr);
|
|
480
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
|
|
481
|
+
gl.enableVertexAttribArray(0);
|
|
482
|
+
gl.enableVertexAttribArray(1);
|
|
483
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 16, 0);
|
|
484
|
+
gl.vertexAttribPointer(1, 2, gl.FLOAT, false, 16, 8);
|
|
485
|
+
const uRectLoc = gl.getUniformLocation(prog, "uRect");
|
|
486
|
+
const quadVerts = /* @__PURE__ */ new Float32Array(24);
|
|
487
|
+
for (let i = 0; i < quads.length; i++) {
|
|
488
|
+
const q = quads[i];
|
|
489
|
+
const ox = q.originX ?? .5;
|
|
490
|
+
const oy = q.originY ?? 1;
|
|
491
|
+
const x0 = q.x - q.width * ox;
|
|
492
|
+
const y0 = q.y - q.height * oy;
|
|
493
|
+
const u0 = q.u0 ?? 0, v0 = q.v0 ?? 0, u1 = q.u1 ?? 1, v1 = q.v1 ?? 1;
|
|
494
|
+
quadVerts.set([
|
|
495
|
+
0,
|
|
496
|
+
0,
|
|
497
|
+
u0,
|
|
498
|
+
v0,
|
|
499
|
+
1,
|
|
500
|
+
0,
|
|
501
|
+
u1,
|
|
502
|
+
v0,
|
|
503
|
+
1,
|
|
504
|
+
1,
|
|
505
|
+
u1,
|
|
506
|
+
v1,
|
|
507
|
+
0,
|
|
508
|
+
0,
|
|
509
|
+
u0,
|
|
510
|
+
v0,
|
|
511
|
+
1,
|
|
512
|
+
1,
|
|
513
|
+
u1,
|
|
514
|
+
v1,
|
|
515
|
+
0,
|
|
516
|
+
1,
|
|
517
|
+
u0,
|
|
518
|
+
v1
|
|
519
|
+
]);
|
|
520
|
+
gl.bufferData(gl.ARRAY_BUFFER, quadVerts, gl.STREAM_DRAW);
|
|
521
|
+
gl.uniform4f(uRectLoc, x0, y0, q.width, q.height);
|
|
522
|
+
gl.drawArrays(gl.TRIANGLES, 0, 6);
|
|
523
|
+
glRenderer.recordCustomDraw(1);
|
|
524
|
+
}
|
|
525
|
+
}, { restoreState: true });
|
|
526
|
+
}
|
|
527
|
+
export { DayNightCycle, Lighting2DSystem, bakeNormalMapCanvas, computeNormalMapPixels, createNormalLitSpritePass, evaluateNormalLitPixel, lerpRgb };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { gt as DisplayObject, jt as SceneHostLike, ut as Container } from "./game-CfUZiazr.js";
|
|
2
|
+
export interface Camera25DConfig {
|
|
3
|
+
viewportWidth: number;
|
|
4
|
+
viewportHeight: number;
|
|
5
|
+
focusX?: number;
|
|
6
|
+
focusY?: number;
|
|
7
|
+
/** Camera ground azimuth in radians (`0` = +X right, +Y down/front). */
|
|
8
|
+
yaw?: number;
|
|
9
|
+
/** Camera tilt angle above horizontal ground in radians (`Math.PI / 4` = 45°, `Math.PI / 2` = pure top-down). */
|
|
10
|
+
pitch?: number;
|
|
11
|
+
zoom?: number;
|
|
12
|
+
/** Spring stiffness for smooth Q/E camera yaw rotation & follow. */
|
|
13
|
+
yawStiffness?: number;
|
|
14
|
+
}
|
|
15
|
+
export interface ProjectedPoint25D {
|
|
16
|
+
/** Screen pixel X. */
|
|
17
|
+
x: number;
|
|
18
|
+
/** Screen pixel Y. */
|
|
19
|
+
y: number;
|
|
20
|
+
/** Camera-space ground depth (increases monotonically from back horizon toward front camera; use for painter's sort). */
|
|
21
|
+
depth: number;
|
|
22
|
+
/** Effective uniform billboard scale (`zoom`). */
|
|
23
|
+
scale: number;
|
|
24
|
+
}
|
|
25
|
+
export declare class Camera25D {
|
|
26
|
+
viewportWidth: number;
|
|
27
|
+
viewportHeight: number;
|
|
28
|
+
focusX: number;
|
|
29
|
+
focusY: number;
|
|
30
|
+
yaw: number;
|
|
31
|
+
targetYaw: number;
|
|
32
|
+
pitch: number;
|
|
33
|
+
zoom: number;
|
|
34
|
+
private yawVel;
|
|
35
|
+
private readonly yawStiffness;
|
|
36
|
+
private followTarget;
|
|
37
|
+
private followLerp;
|
|
38
|
+
constructor(cfg: Camera25DConfig);
|
|
39
|
+
get sinPitch(): number;
|
|
40
|
+
get cosPitch(): number;
|
|
41
|
+
resize(width: number, height: number): this;
|
|
42
|
+
setFocus(wx: number, wy: number): this;
|
|
43
|
+
startFollow(target: {
|
|
44
|
+
x: number;
|
|
45
|
+
y: number;
|
|
46
|
+
}, lerp?: number): this;
|
|
47
|
+
stopFollow(): this;
|
|
48
|
+
/** Rotate target yaw by `steps * stepAngleRad` (default 45° = `Math.PI / 4`, like pressing Q / E in *Don't Starve*). */
|
|
49
|
+
rotateSteps(steps: number, stepAngleRad?: number): this;
|
|
50
|
+
setYawImmediate(yawRad: number): this;
|
|
51
|
+
/** Step smooth critically-damped yaw rotation and focus follow deterministically on `deltaMs`. */
|
|
52
|
+
step(deltaMs: number): this;
|
|
53
|
+
/** Project a 3D world point `(wx, wy, wz)` (`wz >= 0` height above ground) into 2.5D screen coordinates + depth key. */
|
|
54
|
+
worldToScreen(wx: number, wy: number, wz?: number, out?: ProjectedPoint25D): ProjectedPoint25D;
|
|
55
|
+
/** Unproject a screen pixel `(sx, sy)` onto the horizontal ground plane `wz = planeZ` (exact inverse of `worldToScreen`). */
|
|
56
|
+
screenToGround(sx: number, sy: number, planeZ?: number, out?: {
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
}): {
|
|
60
|
+
x: number;
|
|
61
|
+
y: number;
|
|
62
|
+
};
|
|
63
|
+
/** Convert camera-relative input `(rightInput, downInput)` into world-plane movement `(vx, vy)` respecting current `yaw`! */
|
|
64
|
+
cameraInputToWorld(rightInput: number, downInput: number, out?: {
|
|
65
|
+
x: number;
|
|
66
|
+
y: number;
|
|
67
|
+
}): {
|
|
68
|
+
x: number;
|
|
69
|
+
y: number;
|
|
70
|
+
};
|
|
71
|
+
/** Compute axis-aligned world bounding box covering all 4 screen corners on the ground plane (for `ChunkedTilemap.syncViewport`). */
|
|
72
|
+
getGroundViewportBounds(out?: {
|
|
73
|
+
left: number;
|
|
74
|
+
top: number;
|
|
75
|
+
width: number;
|
|
76
|
+
height: number;
|
|
77
|
+
}): {
|
|
78
|
+
left: number;
|
|
79
|
+
top: number;
|
|
80
|
+
width: number;
|
|
81
|
+
height: number;
|
|
82
|
+
};
|
|
83
|
+
/** Project a world-space sun direction `(sunAzimuthRad, sunElevationRad)` into screen-space oblique shadow vector. */
|
|
84
|
+
computeSunShadowVector(sunAzimuthRad: number, sunElevationRad: number, heightPx?: number, out?: {
|
|
85
|
+
dx: number;
|
|
86
|
+
dy: number;
|
|
87
|
+
lengthRatio: number;
|
|
88
|
+
}): {
|
|
89
|
+
dx: number;
|
|
90
|
+
dy: number;
|
|
91
|
+
lengthRatio: number;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export interface BillboardEntry {
|
|
95
|
+
readonly node: DisplayObject;
|
|
96
|
+
wx: number;
|
|
97
|
+
wy: number;
|
|
98
|
+
wz: number;
|
|
99
|
+
baseScaleX: number;
|
|
100
|
+
baseScaleY: number;
|
|
101
|
+
depthBias: number;
|
|
102
|
+
}
|
|
103
|
+
export interface Stage25DRig {
|
|
104
|
+
readonly camera: Camera25D;
|
|
105
|
+
/** Root screen-space container (`scrollFactor = 0`). */
|
|
106
|
+
readonly root: Container;
|
|
107
|
+
/** Ground container whose children (e.g. `ChunkedTilemap`) automatically rotate by `-yaw` and tilt by `sin(pitch)`. */
|
|
108
|
+
readonly groundWorld: Container;
|
|
109
|
+
/** Upright billboard container whose registered sprites/rigs stay vertical facing the lens while sorting by 2.5D depth. */
|
|
110
|
+
readonly billboardLayer: Container;
|
|
111
|
+
registerBillboard(node: DisplayObject, wx: number, wy: number, opts?: {
|
|
112
|
+
wz?: number;
|
|
113
|
+
depthBias?: number;
|
|
114
|
+
baseScaleX?: number;
|
|
115
|
+
baseScaleY?: number;
|
|
116
|
+
}): BillboardEntry;
|
|
117
|
+
removeBillboard(entryOrNode: BillboardEntry | DisplayObject): void;
|
|
118
|
+
/** Advance `Camera25D.step(deltaMs)` and update the ground plane matrix + all registered upright billboards. */
|
|
119
|
+
update(deltaMs?: number): void;
|
|
120
|
+
destroy(): void;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Mount a complete *Don't Starve*-style 2.5D rotatable world rig onto a Stage `Scene`:
|
|
124
|
+
* - Attach any flat 2D map (`MountedChunkedTilemap.container`, ground decals, farm plots) into `groundWorld`
|
|
125
|
+
* - Register upright trees, tombstones, campfires, and `Rig2DView.container` characters via `registerBillboard()`
|
|
126
|
+
* Works identically across WebGL2, WebGPU, Canvas2D, and Node 60fps Headless!
|
|
127
|
+
*/
|
|
128
|
+
export declare function mountStage25D(scene: SceneHostLike, camera: Camera25D, opts?: {
|
|
129
|
+
depth?: number;
|
|
130
|
+
trackId?: string;
|
|
131
|
+
}): Stage25DRig;
|