@holtjs/stage 0.4.0 → 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 +19 -0
- package/README.md +140 -119
- package/dist/{game-CbmA4myj.js → game-Cu1G9xgW.js} +17 -4
- package/dist/{game-C6NrnZ7-.d.ts → game-GdPaQUqH.d.ts} +2 -1
- 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-Bvvi5jN8.js → renderer-webgpu-BorAD6Kk.js} +1 -0
- 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,129 @@
|
|
|
1
|
+
import { bt as ImageObject, jt as SceneHostLike, pt as CustomPassObject } from "./game-GdPaQUqH.js";
|
|
2
|
+
export type DayPhase = 'day' | 'dusk' | 'night';
|
|
3
|
+
export interface DayNightConfig {
|
|
4
|
+
dayLengthMs?: number;
|
|
5
|
+
dayRatio?: number;
|
|
6
|
+
duskRatio?: number;
|
|
7
|
+
initialTime01?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface DayNightState {
|
|
10
|
+
time01: number;
|
|
11
|
+
dayCount: number;
|
|
12
|
+
phase: DayPhase;
|
|
13
|
+
ambientRgb: number;
|
|
14
|
+
/** 0.0 at bright noon -> up to ~0.92 in pitch-black night. */
|
|
15
|
+
darkness: number;
|
|
16
|
+
sunAzimuthRad: number;
|
|
17
|
+
sunElevationRad: number;
|
|
18
|
+
}
|
|
19
|
+
export declare function lerpRgb(rgbA: number, rgbB: number, t: number): number;
|
|
20
|
+
/** Deterministic *Don't Starve* / *Graveyard Keeper* Day -> Dusk -> Night clock controller. */
|
|
21
|
+
export declare class DayNightCycle {
|
|
22
|
+
readonly dayLengthMs: number;
|
|
23
|
+
readonly dayRatio: number;
|
|
24
|
+
readonly duskRatio: number;
|
|
25
|
+
time01: number;
|
|
26
|
+
dayCount: number;
|
|
27
|
+
constructor(cfg?: DayNightConfig);
|
|
28
|
+
step(deltaMs: number): DayNightState;
|
|
29
|
+
setTime01(t: number): DayNightState;
|
|
30
|
+
sample(out?: DayNightState): DayNightState;
|
|
31
|
+
}
|
|
32
|
+
export interface NormalMapBakeOptions {
|
|
33
|
+
/** Bevel strength along alpha silhouette & luminance height (`default = 2.4`). */
|
|
34
|
+
strength?: number;
|
|
35
|
+
/** Weight of RGB luminance relative to alpha silhouette (`0 = pure silhouette dome`, `default = 0.35`). */
|
|
36
|
+
luminanceWeight?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 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`)
|
|
40
|
+
* from raw `Uint8ClampedArray` RGBA pixels using a 3x3 Sobel height gradient.
|
|
41
|
+
* Pure zero-DOM function usable in Node, Workers, and browsers!
|
|
42
|
+
*/
|
|
43
|
+
export declare function computeNormalMapPixels(rgba: Uint8ClampedArray | Uint8Array, width: number, height: number, opts?: NormalMapBakeOptions): Uint8ClampedArray;
|
|
44
|
+
/** Bake a tangent-space normal map `HTMLCanvasElement` directly from any 2D sprite/cutout atlas canvas! */
|
|
45
|
+
export declare function bakeNormalMapCanvas(sourceCanvas: HTMLCanvasElement, createCanvasFn?: (w: number, h: number) => HTMLCanvasElement, opts?: NormalMapBakeOptions): HTMLCanvasElement;
|
|
46
|
+
export interface PointLight2D {
|
|
47
|
+
id: string;
|
|
48
|
+
x: number;
|
|
49
|
+
y: number;
|
|
50
|
+
/** Height of light above the 2D sprite plane in pixels (`default = 36`, controls normal-map grazing angle). */
|
|
51
|
+
z: number;
|
|
52
|
+
radius: number;
|
|
53
|
+
color: number;
|
|
54
|
+
intensity: number;
|
|
55
|
+
/** Torch flame organic flicker amplitude in `[0, 0.5]` (`default = 0`). */
|
|
56
|
+
flicker?: number;
|
|
57
|
+
enabled?: boolean;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Evaluate exact 2D normal-mapped Lambertian + specular lighting at pixel `(px, py)` for unit-testing & CPU reference parity.
|
|
61
|
+
*/
|
|
62
|
+
export declare function evaluateNormalLitPixel(baseRgb: number, normalX: number, normalY: number, normalZ: number, px: number, py: number, ambientRgb: number, lights: ReadonlyArray<PointLight2D>): number;
|
|
63
|
+
export interface Lighting2DSystemOptions {
|
|
64
|
+
key?: string;
|
|
65
|
+
width: number;
|
|
66
|
+
height: number;
|
|
67
|
+
/** Downscale factor for the offscreen lightmap FBO (`2` = half-res soft butter-smooth light falloff at 1/4 fillrate!). */
|
|
68
|
+
resolutionScale?: number;
|
|
69
|
+
depth?: number;
|
|
70
|
+
cycle?: DayNightCycle;
|
|
71
|
+
}
|
|
72
|
+
export declare class Lighting2DSystem {
|
|
73
|
+
readonly scene: SceneHostLike;
|
|
74
|
+
readonly cycle: DayNightCycle;
|
|
75
|
+
readonly overlayImage: ImageObject;
|
|
76
|
+
private renderTarget;
|
|
77
|
+
private readonly lightGraphics;
|
|
78
|
+
private readonly lights;
|
|
79
|
+
private resScale;
|
|
80
|
+
private viewportW;
|
|
81
|
+
private viewportH;
|
|
82
|
+
private elapsedSec;
|
|
83
|
+
private manualAmbientRgb;
|
|
84
|
+
constructor(scene: SceneHostLike, opts: Lighting2DSystemOptions);
|
|
85
|
+
addLight(light: PointLight2D): PointLight2D;
|
|
86
|
+
getLight(id: string): PointLight2D | undefined;
|
|
87
|
+
removeLight(id: string): boolean;
|
|
88
|
+
get lightCount(): number;
|
|
89
|
+
setAmbientOverride(rgb: number | null): void;
|
|
90
|
+
resize(width: number, height: number): void;
|
|
91
|
+
/**
|
|
92
|
+
* Step the day/night cycle + torch flicker and render the `MULTIPLY` lightmap FBO!
|
|
93
|
+
* Pass `projectToScreen` when using `Camera25D`; otherwise defaults to `scene.cameras.main.worldToScreen`.
|
|
94
|
+
*/
|
|
95
|
+
update(deltaMs: number, projectToScreen?: (wx: number, wy: number, wz: number, out: {
|
|
96
|
+
x: number;
|
|
97
|
+
y: number;
|
|
98
|
+
scale: number;
|
|
99
|
+
}) => void): DayNightState;
|
|
100
|
+
destroy(): void;
|
|
101
|
+
}
|
|
102
|
+
export interface NormalLitQuad {
|
|
103
|
+
x: number;
|
|
104
|
+
y: number;
|
|
105
|
+
width: number;
|
|
106
|
+
height: number;
|
|
107
|
+
u0?: number;
|
|
108
|
+
v0?: number;
|
|
109
|
+
u1?: number;
|
|
110
|
+
v1?: number;
|
|
111
|
+
originX?: number;
|
|
112
|
+
originY?: number;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Create a state-isolated WebGL2 `CustomPass` that renders normal-mapped 2D props/characters with per-pixel
|
|
116
|
+
* 3D tangent-space point lighting (`uDiffuse` + `uNormal`), gracefully no-oping on Canvas2D!
|
|
117
|
+
*/
|
|
118
|
+
export declare function createNormalLitSpritePass(scene: SceneHostLike, diffuseKey: string, normalKey: string, getQuadsAndLights: () => {
|
|
119
|
+
ambientRgb: number;
|
|
120
|
+
quads: ReadonlyArray<NormalLitQuad>;
|
|
121
|
+
lights: ReadonlyArray<{
|
|
122
|
+
screenX: number;
|
|
123
|
+
screenY: number;
|
|
124
|
+
z: number;
|
|
125
|
+
radius: number;
|
|
126
|
+
color: number;
|
|
127
|
+
intensity: number;
|
|
128
|
+
}>;
|
|
129
|
+
}): CustomPassObject;
|
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 };
|