ugly-game 0.11.0 → 0.12.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.
@@ -0,0 +1,75 @@
1
+ export interface ProbeGrid {
2
+ ox: number;
3
+ oy: number;
4
+ oz: number;
5
+ /** World units between adjacent probes. */
6
+ cell: number;
7
+ nx: number;
8
+ ny: number;
9
+ nz: number;
10
+ }
11
+ /** What a ray found. `albedo` is the surface colour used for the single bounce. */
12
+ export interface TraceHit {
13
+ t: number;
14
+ n: [number, number, number];
15
+ albedo: [number, number, number];
16
+ }
17
+ /** Trace a ray and return the nearest hit within `maxT`, or null for "escaped to sky". */
18
+ export type TraceFn = (ox: number, oy: number, oz: number, dx: number, dy: number, dz: number, maxT: number) => TraceHit | null;
19
+ export interface BakeOptions {
20
+ /** Rays per probe. More is smoother and slower; 32 is a reasonable floor. */
21
+ rays?: number;
22
+ /** Direction the sunlight TRAVELS (not the direction to the sun). */
23
+ sun?: [number, number, number];
24
+ sunColor?: [number, number, number];
25
+ /** Radiance of the sky along a ray that escaped. Defaults to a dim blue-grey gradient. */
26
+ sky?: (dy: number) => [number, number, number];
27
+ /** How far a bounce ray looks before it counts as sky. */
28
+ maxDistance?: number;
29
+ }
30
+ export declare const PROBE_AXES: readonly [number, number, number][];
31
+ /** Floats per probe in the packed buffer: 6 axes × vec4 (rgb + pad, for std140-friendly strides). */
32
+ export declare const PROBE_STRIDE: number;
33
+ /** Deterministic near-uniform sphere directions (Fibonacci) — no RNG, so a re-bake is repeatable. */
34
+ export declare function sphereDirs(n: number): [number, number, number][];
35
+ /**
36
+ * Bake `grid` into a packed ambient-cube buffer of `nx*ny*nz*6` vec4s, axis-major within a probe.
37
+ *
38
+ * Each ray either hits a surface — contributing that surface's sun-lit albedo, one bounce — or
39
+ * escapes and contributes sky. The result is projected onto the six axes weighted by `max(0, d·axis)`,
40
+ * so each axis ends up with the irradiance arriving from that hemisphere.
41
+ */
42
+ export declare function bakeProbeGrid(grid: ProbeGrid, trace: TraceFn, opts?: BakeOptions): Float32Array;
43
+ /**
44
+ * CPU mirror of the WGSL `giAt` below — trilinear across the eight surrounding probes, then the
45
+ * ambient cube resolved against `n`. Mirrored 1:1 on purpose: it is what lets a headless oracle
46
+ * test the thing the GPU will actually do.
47
+ */
48
+ export declare function sampleProbeGrid(probes: Float32Array, grid: ProbeGrid, x: number, y: number, z: number, n: [number, number, number]): [number, number, number];
49
+ /**
50
+ * Drop-in WGSL, mirroring `sampleProbeGrid` exactly. Paste into a receiver fragment shader that has
51
+ * a `probes: array<vec4<f32>>` storage binding and a `giGrid` uniform in scope:
52
+ *
53
+ * struct GiGrid { org: vec4<f32>, dim: vec4<f32> }; // org.xyz = grid origin, org.w = cell size
54
+ * // dim.xyz = nx,ny,nz, dim.w = enable (0 = off)
55
+ *
56
+ * Gate on `giGrid.dim.w`: a caller that supplies no probes gets 0 and the flat ambient it had.
57
+ *
58
+ * UNVERIFIED. `sampleProbeGrid` above is covered by shared/giProbes.test.mts and this is written to
59
+ * mirror it line for line, but no renderer binds it yet, so nothing has executed it on a GPU. The
60
+ * mirror is a claim, not a measurement — treat the first splice into a fragment shader as the point
61
+ * where it gets tested, and compare against the CPU function on the same inputs.
62
+ */
63
+ export declare const GI_WGSL = "\nfn giProbeAt(ix: i32, iy: i32, iz: i32, w: array<f32, 6>, wsum: f32) -> vec3<f32> {\n let nx = i32(giGrid.dim.x); let ny = i32(giGrid.dim.y);\n let base = (iz * ny * nx + iy * nx + ix) * 6;\n var c = vec3<f32>(0.0);\n for (var a = 0; a < 6; a = a + 1) {\n c = c + probes[base + a].rgb * w[a];\n }\n return c / wsum;\n}\n\nfn giAt(p: vec3<f32>, n: vec3<f32>) -> vec3<f32> {\n if (giGrid.dim.w < 0.5) { return vec3<f32>(0.0); }\n let dim = vec3<i32>(i32(giGrid.dim.x), i32(giGrid.dim.y), i32(giGrid.dim.z));\n let f = (p - giGrid.org.xyz) / giGrid.org.w;\n let fc = clamp(f, vec3<f32>(0.0), vec3<f32>(dim - vec3<i32>(1)));\n let i0 = vec3<i32>(floor(fc));\n let i1 = min(i0 + vec3<i32>(1), dim - vec3<i32>(1));\n let t = clamp(fc - vec3<f32>(i0), vec3<f32>(0.0), vec3<f32>(1.0));\n\n // Ambient cube: only the three axes the normal faces contribute, weighted by (n\u00B7axis)\u00B2.\n var w = array<f32, 6>(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);\n var wsum = 0.0;\n let d = array<f32, 6>(n.x, -n.x, n.y, -n.y, n.z, -n.z);\n for (var a = 0; a < 6; a = a + 1) {\n if (d[a] > 0.0) { w[a] = d[a] * d[a]; wsum = wsum + w[a]; }\n }\n if (wsum <= 0.0) { return vec3<f32>(0.0); }\n\n let c00 = mix(giProbeAt(i0.x, i0.y, i0.z, w, wsum), giProbeAt(i1.x, i0.y, i0.z, w, wsum), t.x);\n let c10 = mix(giProbeAt(i0.x, i1.y, i0.z, w, wsum), giProbeAt(i1.x, i1.y, i0.z, w, wsum), t.x);\n let c01 = mix(giProbeAt(i0.x, i0.y, i1.z, w, wsum), giProbeAt(i1.x, i0.y, i1.z, w, wsum), t.x);\n let c11 = mix(giProbeAt(i0.x, i1.y, i1.z, w, wsum), giProbeAt(i1.x, i1.y, i1.z, w, wsum), t.x);\n return mix(mix(c00, c10, t.y), mix(c01, c11, t.y), t.z);\n}\n";
64
+ /** Convenience: a TraceFn over an axis-aligned box list, so the box case still has a one-liner. */
65
+ export declare function boxTrace(boxes: readonly {
66
+ x: number;
67
+ y: number;
68
+ z: number;
69
+ sx: number;
70
+ sy: number;
71
+ sz: number;
72
+ r: number;
73
+ g: number;
74
+ b: number;
75
+ }[]): TraceFn;
@@ -0,0 +1,261 @@
1
+ // PLATFORM. An irradiance probe grid that any world can bake, plus the WGSL to sample it.
2
+ //
3
+ // gpuGI.ts already bakes probes, but only for a LIST OF BOXES: `bakeProbes(boxes, grid)` ray-casts
4
+ // against `GIBox[]`. A voxel or SDF world has no box list — it has a density function — so that
5
+ // signature cannot express it, and its renderer half clears the colour target and hardcodes its sun,
6
+ // which makes it a demo rather than a component.
7
+ //
8
+ // This module keeps the part that generalises. The scene arrives as a TRACE CALLBACK:
9
+ //
10
+ // bakeProbeGrid(grid, (ox,oy,oz, dx,dy,dz, maxT) => hit | null)
11
+ //
12
+ // so a caller backs it with whatever it has — an SDF march, a voxel DDA, a BVH, a box list. Each
13
+ // probe stores a 6-axis AMBIENT CUBE (irradiance along ±X/±Y/±Z, the Half-Life 2 trick): cheap to
14
+ // store, keeps direction, so a red floor bleeds red upward and a nook that sees little sky goes
15
+ // dark on its own.
16
+ //
17
+ // The bake is PRESENTATIONAL — it must never feed a deterministic sim. It uses plain Math and a
18
+ // caller-supplied trace, neither of which is hash-stable across machines.
19
+ export const PROBE_AXES = [
20
+ [1, 0, 0],
21
+ [-1, 0, 0],
22
+ [0, 1, 0],
23
+ [0, -1, 0],
24
+ [0, 0, 1],
25
+ [0, 0, -1],
26
+ ];
27
+ /** Floats per probe in the packed buffer: 6 axes × vec4 (rgb + pad, for std140-friendly strides). */
28
+ export const PROBE_STRIDE = 6 * 4;
29
+ function defaultSky(dy) {
30
+ if (dy < 0)
31
+ return [0.03, 0.03, 0.04]; // below the horizon there is almost nothing to gather
32
+ const t = dy;
33
+ // Deliberately dim: a bright sky washes out the colour bleed that makes GI legible at all.
34
+ return [0.16 + 0.08 * t, 0.19 + 0.11 * t, 0.24 + 0.16 * t];
35
+ }
36
+ /** Deterministic near-uniform sphere directions (Fibonacci) — no RNG, so a re-bake is repeatable. */
37
+ export function sphereDirs(n) {
38
+ const out = [];
39
+ const ga = Math.PI * (3 - Math.sqrt(5));
40
+ for (let i = 0; i < n; i++) {
41
+ const y = 1 - ((i + 0.5) / n) * 2, r = Math.sqrt(Math.max(0, 1 - y * y)), th = ga * i;
42
+ out.push([Math.cos(th) * r, y, Math.sin(th) * r]);
43
+ }
44
+ return out;
45
+ }
46
+ /**
47
+ * Bake `grid` into a packed ambient-cube buffer of `nx*ny*nz*6` vec4s, axis-major within a probe.
48
+ *
49
+ * Each ray either hits a surface — contributing that surface's sun-lit albedo, one bounce — or
50
+ * escapes and contributes sky. The result is projected onto the six axes weighted by `max(0, d·axis)`,
51
+ * so each axis ends up with the irradiance arriving from that hemisphere.
52
+ */
53
+ export function bakeProbeGrid(grid, trace, opts = {}) {
54
+ const rays = opts.rays ?? 32;
55
+ const sunRaw = opts.sun ?? [-0.4, -0.85, -0.32];
56
+ const sl = Math.hypot(sunRaw[0], sunRaw[1], sunRaw[2]) || 1;
57
+ const sun = [
58
+ sunRaw[0] / sl,
59
+ sunRaw[1] / sl,
60
+ sunRaw[2] / sl,
61
+ ];
62
+ const sunCol = opts.sunColor ?? [1.15, 1.05, 0.9];
63
+ const sky = opts.sky ?? defaultSky;
64
+ const maxT = opts.maxDistance ?? grid.cell * Math.max(grid.nx, grid.ny, grid.nz) * 2;
65
+ const dirs = sphereDirs(rays);
66
+ const n = grid.nx * grid.ny * grid.nz;
67
+ const out = new Float32Array(n * PROBE_STRIDE);
68
+ for (let iz = 0; iz < grid.nz; iz++)
69
+ for (let iy = 0; iy < grid.ny; iy++)
70
+ for (let ix = 0; ix < grid.nx; ix++) {
71
+ const px = grid.ox + ix * grid.cell, py = grid.oy + iy * grid.cell, pz = grid.oz + iz * grid.cell;
72
+ const acc = new Float64Array(18); // 6 axes × rgb
73
+ const wsum = new Float64Array(6);
74
+ for (const [dx, dy, dz] of dirs) {
75
+ const hit = trace(px, py, pz, dx, dy, dz, maxT);
76
+ let r, g, b;
77
+ if (hit) {
78
+ // One bounce: how lit is the surface we struck? A face turned away from the sun
79
+ // contributes only its own ambient, which is what makes cave interiors settle dark
80
+ // instead of merely dimmer.
81
+ const ndl = Math.max(0, -(hit.n[0] * sun[0] + hit.n[1] * sun[1] + hit.n[2] * sun[2]));
82
+ r = hit.albedo[0] * sunCol[0] * ndl;
83
+ g = hit.albedo[1] * sunCol[1] * ndl;
84
+ b = hit.albedo[2] * sunCol[2] * ndl;
85
+ }
86
+ else {
87
+ const s = sky(dy);
88
+ r = s[0];
89
+ g = s[1];
90
+ b = s[2];
91
+ }
92
+ for (let a = 0; a < 6; a++) {
93
+ const ax = PROBE_AXES[a];
94
+ const w = dx * ax[0] + dy * ax[1] + dz * ax[2];
95
+ if (w <= 0)
96
+ continue;
97
+ acc[a * 3] += r * w;
98
+ acc[a * 3 + 1] += g * w;
99
+ acc[a * 3 + 2] += b * w;
100
+ wsum[a] += w;
101
+ }
102
+ }
103
+ const base = (iz * grid.ny * grid.nx + iy * grid.nx + ix) * PROBE_STRIDE;
104
+ for (let a = 0; a < 6; a++) {
105
+ const w = wsum[a] || 1;
106
+ out[base + a * 4] = acc[a * 3] / w;
107
+ out[base + a * 4 + 1] = acc[a * 3 + 1] / w;
108
+ out[base + a * 4 + 2] = acc[a * 3 + 2] / w;
109
+ out[base + a * 4 + 3] = 0;
110
+ }
111
+ }
112
+ return out;
113
+ }
114
+ /**
115
+ * CPU mirror of the WGSL `giAt` below — trilinear across the eight surrounding probes, then the
116
+ * ambient cube resolved against `n`. Mirrored 1:1 on purpose: it is what lets a headless oracle
117
+ * test the thing the GPU will actually do.
118
+ */
119
+ export function sampleProbeGrid(probes, grid, x, y, z, n) {
120
+ const fx = (x - grid.ox) / grid.cell, fy = (y - grid.oy) / grid.cell, fz = (z - grid.oz) / grid.cell;
121
+ const cl = (v, hi) => Math.max(0, Math.min(hi, v));
122
+ const x0 = Math.floor(cl(fx, grid.nx - 1)), y0 = Math.floor(cl(fy, grid.ny - 1)), z0 = Math.floor(cl(fz, grid.nz - 1));
123
+ const x1 = Math.min(x0 + 1, grid.nx - 1), y1 = Math.min(y0 + 1, grid.ny - 1), z1 = Math.min(z0 + 1, grid.nz - 1);
124
+ const tx = cl(fx - x0, 1), ty = cl(fy - y0, 1), tz = cl(fz - z0, 1);
125
+ // Ambient-cube weights: only the three axes the normal faces contribute, weighted by n·axis².
126
+ const w = new Float64Array(6);
127
+ let wsum = 0;
128
+ for (let a = 0; a < 6; a++) {
129
+ const ax = PROBE_AXES[a];
130
+ const d = n[0] * ax[0] + n[1] * ax[1] + n[2] * ax[2];
131
+ if (d <= 0)
132
+ continue;
133
+ w[a] = d * d;
134
+ wsum += w[a];
135
+ }
136
+ if (wsum === 0)
137
+ return [0, 0, 0];
138
+ const at = (ix, iy, iz) => {
139
+ const base = (iz * grid.ny * grid.nx + iy * grid.nx + ix) * PROBE_STRIDE;
140
+ let r = 0, g = 0, b = 0;
141
+ for (let a = 0; a < 6; a++) {
142
+ if (w[a] === 0)
143
+ continue;
144
+ r += probes[base + a * 4] * w[a];
145
+ g += probes[base + a * 4 + 1] * w[a];
146
+ b += probes[base + a * 4 + 2] * w[a];
147
+ }
148
+ return [r / wsum, g / wsum, b / wsum];
149
+ };
150
+ const lerp3 = (p, q, t) => [
151
+ p[0] + (q[0] - p[0]) * t,
152
+ p[1] + (q[1] - p[1]) * t,
153
+ p[2] + (q[2] - p[2]) * t,
154
+ ];
155
+ const c00 = lerp3(at(x0, y0, z0), at(x1, y0, z0), tx);
156
+ const c10 = lerp3(at(x0, y1, z0), at(x1, y1, z0), tx);
157
+ const c01 = lerp3(at(x0, y0, z1), at(x1, y0, z1), tx);
158
+ const c11 = lerp3(at(x0, y1, z1), at(x1, y1, z1), tx);
159
+ return lerp3(lerp3(c00, c10, ty), lerp3(c01, c11, ty), tz);
160
+ }
161
+ /**
162
+ * Drop-in WGSL, mirroring `sampleProbeGrid` exactly. Paste into a receiver fragment shader that has
163
+ * a `probes: array<vec4<f32>>` storage binding and a `giGrid` uniform in scope:
164
+ *
165
+ * struct GiGrid { org: vec4<f32>, dim: vec4<f32> }; // org.xyz = grid origin, org.w = cell size
166
+ * // dim.xyz = nx,ny,nz, dim.w = enable (0 = off)
167
+ *
168
+ * Gate on `giGrid.dim.w`: a caller that supplies no probes gets 0 and the flat ambient it had.
169
+ *
170
+ * UNVERIFIED. `sampleProbeGrid` above is covered by shared/giProbes.test.mts and this is written to
171
+ * mirror it line for line, but no renderer binds it yet, so nothing has executed it on a GPU. The
172
+ * mirror is a claim, not a measurement — treat the first splice into a fragment shader as the point
173
+ * where it gets tested, and compare against the CPU function on the same inputs.
174
+ */
175
+ export const GI_WGSL = /* wgsl */ `
176
+ fn giProbeAt(ix: i32, iy: i32, iz: i32, w: array<f32, 6>, wsum: f32) -> vec3<f32> {
177
+ let nx = i32(giGrid.dim.x); let ny = i32(giGrid.dim.y);
178
+ let base = (iz * ny * nx + iy * nx + ix) * 6;
179
+ var c = vec3<f32>(0.0);
180
+ for (var a = 0; a < 6; a = a + 1) {
181
+ c = c + probes[base + a].rgb * w[a];
182
+ }
183
+ return c / wsum;
184
+ }
185
+
186
+ fn giAt(p: vec3<f32>, n: vec3<f32>) -> vec3<f32> {
187
+ if (giGrid.dim.w < 0.5) { return vec3<f32>(0.0); }
188
+ let dim = vec3<i32>(i32(giGrid.dim.x), i32(giGrid.dim.y), i32(giGrid.dim.z));
189
+ let f = (p - giGrid.org.xyz) / giGrid.org.w;
190
+ let fc = clamp(f, vec3<f32>(0.0), vec3<f32>(dim - vec3<i32>(1)));
191
+ let i0 = vec3<i32>(floor(fc));
192
+ let i1 = min(i0 + vec3<i32>(1), dim - vec3<i32>(1));
193
+ let t = clamp(fc - vec3<f32>(i0), vec3<f32>(0.0), vec3<f32>(1.0));
194
+
195
+ // Ambient cube: only the three axes the normal faces contribute, weighted by (n·axis)².
196
+ var w = array<f32, 6>(0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
197
+ var wsum = 0.0;
198
+ let d = array<f32, 6>(n.x, -n.x, n.y, -n.y, n.z, -n.z);
199
+ for (var a = 0; a < 6; a = a + 1) {
200
+ if (d[a] > 0.0) { w[a] = d[a] * d[a]; wsum = wsum + w[a]; }
201
+ }
202
+ if (wsum <= 0.0) { return vec3<f32>(0.0); }
203
+
204
+ let c00 = mix(giProbeAt(i0.x, i0.y, i0.z, w, wsum), giProbeAt(i1.x, i0.y, i0.z, w, wsum), t.x);
205
+ let c10 = mix(giProbeAt(i0.x, i1.y, i0.z, w, wsum), giProbeAt(i1.x, i1.y, i0.z, w, wsum), t.x);
206
+ let c01 = mix(giProbeAt(i0.x, i0.y, i1.z, w, wsum), giProbeAt(i1.x, i0.y, i1.z, w, wsum), t.x);
207
+ let c11 = mix(giProbeAt(i0.x, i1.y, i1.z, w, wsum), giProbeAt(i1.x, i1.y, i1.z, w, wsum), t.x);
208
+ return mix(mix(c00, c10, t.y), mix(c01, c11, t.y), t.z);
209
+ }
210
+ `;
211
+ /** Convenience: a TraceFn over an axis-aligned box list, so the box case still has a one-liner. */
212
+ export function boxTrace(boxes) {
213
+ return (ox, oy, oz, dx, dy, dz, maxT) => {
214
+ let best = null;
215
+ for (const b of boxes) {
216
+ const o = [ox, oy, oz], d = [dx, dy, dz];
217
+ const mn = [b.x - b.sx / 2, b.y - b.sy / 2, b.z - b.sz / 2];
218
+ const mx = [b.x + b.sx / 2, b.y + b.sy / 2, b.z + b.sz / 2];
219
+ let tmin = -Infinity, tmax = Infinity, axis = 0, sign = 1, miss = false;
220
+ for (let a = 0; a < 3; a++) {
221
+ if (Math.abs(d[a]) < 1e-9) {
222
+ if (o[a] < mn[a] || o[a] > mx[a]) {
223
+ miss = true;
224
+ break;
225
+ }
226
+ continue;
227
+ }
228
+ const inv = 1 / d[a];
229
+ let t1 = (mn[a] - o[a]) * inv, t2 = (mx[a] - o[a]) * inv, s = -1;
230
+ if (t1 > t2) {
231
+ const tmp = t1;
232
+ t1 = t2;
233
+ t2 = tmp;
234
+ s = 1;
235
+ }
236
+ if (t1 > tmin) {
237
+ tmin = t1;
238
+ axis = a;
239
+ sign = s;
240
+ }
241
+ if (t2 < tmax)
242
+ tmax = t2;
243
+ if (tmin > tmax) {
244
+ miss = true;
245
+ break;
246
+ }
247
+ }
248
+ if (miss || tmax < 0)
249
+ continue;
250
+ const t = tmin > 0 ? tmin : tmax;
251
+ if (t < 0 || t > maxT)
252
+ continue;
253
+ if (best && t >= best.t)
254
+ continue;
255
+ const nrm = [0, 0, 0];
256
+ nrm[axis] = sign;
257
+ best = { t, n: nrm, albedo: [b.r, b.g, b.b] };
258
+ }
259
+ return best;
260
+ };
261
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {