ugly-game 0.12.0 → 0.13.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/dist/giProbes.d.ts +1 -1
- package/dist/gpuShadow.d.ts +9 -0
- package/dist/gpuShadow.js +71 -1
- package/dist/gpuTaaResolve.d.ts +3 -1
- package/dist/gpuTaaResolve.js +27 -8
- package/dist/loop/gameLoop.d.ts +2 -1
- package/dist/loop/gameLoop.js +11 -4
- package/package.json +1 -1
package/dist/giProbes.d.ts
CHANGED
|
@@ -39,7 +39,7 @@ export declare function sphereDirs(n: number): [number, number, number][];
|
|
|
39
39
|
* escapes and contributes sky. The result is projected onto the six axes weighted by `max(0, d·axis)`,
|
|
40
40
|
* so each axis ends up with the irradiance arriving from that hemisphere.
|
|
41
41
|
*/
|
|
42
|
-
export declare function bakeProbeGrid(grid: ProbeGrid, trace: TraceFn, opts?: BakeOptions): Float32Array
|
|
42
|
+
export declare function bakeProbeGrid(grid: ProbeGrid, trace: TraceFn, opts?: BakeOptions): Float32Array<ArrayBuffer>;
|
|
43
43
|
/**
|
|
44
44
|
* CPU mirror of the WGSL `giAt` below — trilinear across the eight surrounding probes, then the
|
|
45
45
|
* ambient cube resolved against `n`. Mirrored 1:1 on purpose: it is what lets a headless oracle
|
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ProbeGrid } from './giProbes';
|
|
1
2
|
export interface CubeInstance {
|
|
2
3
|
x: number;
|
|
3
4
|
y: number;
|
|
@@ -44,6 +45,8 @@ export declare class GpuShadowRenderer {
|
|
|
44
45
|
private shadowBGL;
|
|
45
46
|
private mainBGL;
|
|
46
47
|
private shadowSampler;
|
|
48
|
+
private giGridBuf;
|
|
49
|
+
private giProbeBuf;
|
|
47
50
|
private count;
|
|
48
51
|
private cam;
|
|
49
52
|
private shadowCam;
|
|
@@ -127,6 +130,12 @@ export declare class GpuShadowRenderer {
|
|
|
127
130
|
overlayPass(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView): void;
|
|
128
131
|
/** Write a frustum's cull uniform: 6 planes + count + this frustum's VP + occlusion params (occ on the camera pass). */
|
|
129
132
|
private writeCull;
|
|
133
|
+
/**
|
|
134
|
+
* Supply a baked irradiance grid (see giProbes.bakeProbeGrid), or `null` to turn indirect light
|
|
135
|
+
* off. The probe buffer is reallocated only when the grid's size changes, so re-baking a moving
|
|
136
|
+
* cage each frame is a plain writeBuffer.
|
|
137
|
+
*/
|
|
138
|
+
setGI(probes: Float32Array<ArrayBuffer> | null, grid: ProbeGrid | null): void;
|
|
130
139
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
131
140
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
132
141
|
*
|
package/dist/gpuShadow.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
* emissive flag (> 0 ⇒ the cube self-glows at full brightness, bypassing sun/shadow/AO — used for the
|
|
13
13
|
* place ghost, destroy marker, projectiles, and sparkles so effect cubes read as distinct from terrain). */
|
|
14
14
|
import { frustumPlanes } from './mat4';
|
|
15
|
+
import { GI_WGSL, PROBE_STRIDE } from './giProbes';
|
|
15
16
|
const SHADOW_SIZE = 2048;
|
|
16
17
|
// CASCADED SHADOW MAPS. One ortho map has to choose between resolution and reach: AMS ran a single
|
|
17
18
|
// 90-block box against a 280-block draw distance, so everything past it was simply unshadowed and
|
|
@@ -220,6 +221,13 @@ const MAIN_WGSL = COMMON_WGSL +
|
|
|
220
221
|
@group(0) @binding(5) var atlasTex: texture_2d<f32>;
|
|
221
222
|
@group(0) @binding(6) var atlasSamp: sampler;
|
|
222
223
|
@group(0) @binding(7) var<storage, read> tileRects: array<vec4<f32>>; // [u0,v0,du,dv] per (material*3 + face-variant)
|
|
224
|
+
// GLOBAL ILLUMINATION (giProbes.ts). org.xyz = grid origin, org.w = cell size; dim.xyz = nx,ny,nz,
|
|
225
|
+
// dim.w = enable. A caller that never calls setGI leaves dim.w at 0, giAt returns zero, and this is
|
|
226
|
+
// byte-identical to the flat-ambient version it replaced.
|
|
227
|
+
struct GiGrid { org: vec4<f32>, dim: vec4<f32> };
|
|
228
|
+
@group(0) @binding(8) var<uniform> giGrid: GiGrid;
|
|
229
|
+
@group(0) @binding(9) var<storage, read> probes: array<vec4<f32>>;
|
|
230
|
+
${GI_WGSL}
|
|
223
231
|
|
|
224
232
|
struct VOut { @builtin(position) clip: vec4<f32>, @location(0) world: vec3<f32>, @location(1) nrm: vec3<f32>, @location(2) hue: f32, @location(3) emis: f32, @location(4) blk: vec3<f32>, @location(5) objN: vec3<f32>, @location(6) tile: f32, @location(7) upc: f32 };
|
|
225
233
|
|
|
@@ -352,7 +360,10 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
|
|
|
352
360
|
// orientation legible day or night, while the sun adds bright, shadow-casting key light on top during the day.
|
|
353
361
|
let fill = cam.sun.w * (0.5 + 0.85 * fixedFill(N, cam.up.xyz));
|
|
354
362
|
let key = cam.sunCol.xyz * ndl * sh; // direct sun (shadowed) — primary light
|
|
355
|
-
|
|
363
|
+
// INDIRECT: baked one-bounce irradiance, zero unless setGI supplied probes — additive over the
|
|
364
|
+
// existing look rather than a replacement for the ambient knob.
|
|
365
|
+
let gi = giAt(i.world, N);
|
|
366
|
+
let lit = base * (fill + key + gi) * ao;
|
|
356
367
|
// cam.up.w > 0.5 selects LINEAR-HDR output (unclamped, no gamma) for an HDR pipeline that tonemaps downstream;
|
|
357
368
|
// otherwise the classic gamma-encoded LDR path (all existing consumers). Keeps both looks from one shader.
|
|
358
369
|
if (cam.up.w > 0.5) { return vec4(max(lit, vec3(0.0)), 1.0); }
|
|
@@ -394,6 +405,8 @@ export class GpuShadowRenderer {
|
|
|
394
405
|
shadowBGL;
|
|
395
406
|
mainBGL;
|
|
396
407
|
shadowSampler;
|
|
408
|
+
giGridBuf;
|
|
409
|
+
giProbeBuf;
|
|
397
410
|
count = 0;
|
|
398
411
|
// camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm
|
|
399
412
|
cam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4);
|
|
@@ -513,6 +526,16 @@ export class GpuShadowRenderer {
|
|
|
513
526
|
visibility: GPUShaderStage.FRAGMENT,
|
|
514
527
|
buffer: { type: 'read-only-storage' },
|
|
515
528
|
}, // per-tile UV rects
|
|
529
|
+
{
|
|
530
|
+
binding: 8,
|
|
531
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
532
|
+
buffer: { type: 'uniform' },
|
|
533
|
+
}, // GI grid descriptor
|
|
534
|
+
{
|
|
535
|
+
binding: 9,
|
|
536
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
537
|
+
buffer: { type: 'read-only-storage' },
|
|
538
|
+
}, // GI probe irradiance
|
|
516
539
|
],
|
|
517
540
|
});
|
|
518
541
|
this.mainPipe = device.createRenderPipeline({
|
|
@@ -528,6 +551,18 @@ export class GpuShadowRenderer {
|
|
|
528
551
|
});
|
|
529
552
|
this.shadowBGL = shadowBGL;
|
|
530
553
|
this.mainBGL = mainBGL;
|
|
554
|
+
// GI defaults: a zeroed descriptor (dim.w = 0 ⇒ disabled) and a one-probe placeholder, so the
|
|
555
|
+
// bindings always exist and a caller that never touches GI pays nothing but two tiny buffers.
|
|
556
|
+
this.giGridBuf = device.createBuffer({
|
|
557
|
+
size: 32,
|
|
558
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
559
|
+
});
|
|
560
|
+
device.queue.writeBuffer(this.giGridBuf, 0, new Float32Array(8));
|
|
561
|
+
this.giProbeBuf = device.createBuffer({
|
|
562
|
+
size: PROBE_STRIDE * 4,
|
|
563
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
564
|
+
});
|
|
565
|
+
device.queue.writeBuffer(this.giProbeBuf, 0, new Float32Array(PROBE_STRIDE));
|
|
531
566
|
this.shadowSampler = device.createSampler({
|
|
532
567
|
compare: 'less',
|
|
533
568
|
magFilter: 'linear',
|
|
@@ -841,6 +876,8 @@ export class GpuShadowRenderer {
|
|
|
841
876
|
{ binding: 5, resource: this.atlasView },
|
|
842
877
|
{ binding: 6, resource: this.atlasSampler },
|
|
843
878
|
{ binding: 7, resource: { buffer: this.tileRectBuf } },
|
|
879
|
+
{ binding: 8, resource: { buffer: this.giGridBuf } },
|
|
880
|
+
{ binding: 9, resource: { buffer: this.giProbeBuf } },
|
|
844
881
|
],
|
|
845
882
|
});
|
|
846
883
|
}
|
|
@@ -986,6 +1023,39 @@ export class GpuShadowRenderer {
|
|
|
986
1023
|
u[47] = HIZ_DOWN;
|
|
987
1024
|
this.device.queue.writeBuffer(buf, 0, u);
|
|
988
1025
|
}
|
|
1026
|
+
/**
|
|
1027
|
+
* Supply a baked irradiance grid (see giProbes.bakeProbeGrid), or `null` to turn indirect light
|
|
1028
|
+
* off. The probe buffer is reallocated only when the grid's size changes, so re-baking a moving
|
|
1029
|
+
* cage each frame is a plain writeBuffer.
|
|
1030
|
+
*/
|
|
1031
|
+
setGI(probes, grid) {
|
|
1032
|
+
const d = this.device;
|
|
1033
|
+
if (!probes || !grid) {
|
|
1034
|
+
d.queue.writeBuffer(this.giGridBuf, 0, new Float32Array(8)); // dim.w = 0 ⇒ off
|
|
1035
|
+
return;
|
|
1036
|
+
}
|
|
1037
|
+
const need = probes.byteLength;
|
|
1038
|
+
if (this.giProbeBuf.size < need) {
|
|
1039
|
+
this.giProbeBuf.destroy();
|
|
1040
|
+
this.giProbeBuf = d.createBuffer({
|
|
1041
|
+
size: need,
|
|
1042
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
1043
|
+
});
|
|
1044
|
+
if (this.sceneReady)
|
|
1045
|
+
this.makeBindGroups(); // the old buffer is baked into the bind group
|
|
1046
|
+
}
|
|
1047
|
+
d.queue.writeBuffer(this.giProbeBuf, 0, probes);
|
|
1048
|
+
d.queue.writeBuffer(this.giGridBuf, 0, new Float32Array([
|
|
1049
|
+
grid.ox,
|
|
1050
|
+
grid.oy,
|
|
1051
|
+
grid.oz,
|
|
1052
|
+
grid.cell,
|
|
1053
|
+
grid.nx,
|
|
1054
|
+
grid.ny,
|
|
1055
|
+
grid.nz,
|
|
1056
|
+
1,
|
|
1057
|
+
]));
|
|
1058
|
+
}
|
|
989
1059
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
990
1060
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
991
1061
|
*
|
package/dist/gpuTaaResolve.d.ts
CHANGED
|
@@ -28,6 +28,8 @@ export declare class GpuTaaResolve {
|
|
|
28
28
|
* previous frame's UNJITTERED view-projection. Jitter belongs in the geometry pass's projection
|
|
29
29
|
* only — reprojection done with a jittered matrix would chase its own offset.
|
|
30
30
|
*/
|
|
31
|
-
frame(enc: GPUCommandEncoder, out: GPUTextureView, invCurrVP: Float32Array, prevVP: Float32Array, blend?: number, enabled?: boolean
|
|
31
|
+
frame(enc: GPUCommandEncoder, out: GPUTextureView, invCurrVP: Float32Array, prevVP: Float32Array, blend?: number, enabled?: boolean,
|
|
32
|
+
/** Clip width in standard deviations. Lower = harsher rejection, less ghosting, more shimmer. */
|
|
33
|
+
clipSigma?: number): void;
|
|
32
34
|
destroy(): void;
|
|
33
35
|
}
|
package/dist/gpuTaaResolve.js
CHANGED
|
@@ -46,7 +46,7 @@ struct RU {
|
|
|
46
46
|
invCurrVP: mat4x4<f32>,
|
|
47
47
|
prevVP: mat4x4<f32>,
|
|
48
48
|
texel: vec4<f32>, // xy = 1/size, zw = size
|
|
49
|
-
ctrl: vec4<f32>, // x = history blend, y = enabled, z = reset
|
|
49
|
+
ctrl: vec4<f32>, // x = history blend, y = enabled, z = reset, w = clip sigma
|
|
50
50
|
};
|
|
51
51
|
@group(0) @binding(0) var<uniform> ru: RU;
|
|
52
52
|
@group(0) @binding(1) var histSamp: sampler;
|
|
@@ -69,17 +69,27 @@ struct ROut { @location(0) screen: vec4<f32>, @location(1) hist: vec4<f32> };
|
|
|
69
69
|
// Disabled, or the first frame after a cut: the current frame IS the answer and the history.
|
|
70
70
|
if (ru.ctrl.y < 0.5 || ru.ctrl.z > 0.5) { o.screen = vec4(cur, 1.0); o.hist = vec4(cur, 1.0); return o; }
|
|
71
71
|
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
//
|
|
75
|
-
|
|
72
|
+
// ANTI-GHOSTING CLAMP, by VARIANCE rather than by min/max. A raw 3x3 min/max box is enormous over
|
|
73
|
+
// high-frequency detail -- a voxel world's textured terrain spans most of the range inside any
|
|
74
|
+
// nine pixels -- so stale history passes through it essentially unmodified and smears behind
|
|
75
|
+
// motion. Clipping to mean +/- gamma*sigma tracks what the neighbourhood actually IS rather than
|
|
76
|
+
// its extremes, which is what makes the rejection bite.
|
|
77
|
+
// Coordinates are clamped to the texture: an out-of-range textureLoad returns zero in WGSL, which
|
|
78
|
+
// would drag the mean to black along every screen edge and rim the frame.
|
|
79
|
+
var m1 = vec3<f32>(0.0); var m2 = vec3<f32>(0.0);
|
|
76
80
|
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
77
81
|
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
78
82
|
let q = clamp(px + vec2<i32>(dx, dy), vec2<i32>(0, 0), dim - vec2<i32>(1, 1));
|
|
79
83
|
let c = textureLoad(colorTex, q, 0).rgb;
|
|
80
|
-
|
|
84
|
+
m1 = m1 + c; m2 = m2 + c * c;
|
|
81
85
|
}
|
|
82
86
|
}
|
|
87
|
+
let mu = m1 / 9.0;
|
|
88
|
+
let sigma = sqrt(max(vec3<f32>(0.0), m2 / 9.0 - mu * mu));
|
|
89
|
+
let gamma = ru.ctrl.w; // clip width in standard deviations
|
|
90
|
+
// Always admit the current pixel, so a legitimately extreme sample is never clipped against itself.
|
|
91
|
+
var mn = min(mu - gamma * sigma, cur);
|
|
92
|
+
var mx = max(mu + gamma * sigma, cur);
|
|
83
93
|
|
|
84
94
|
// Depth -> world -> previous clip. WebGPU depth is [0,1] and clip-space Y is up while the
|
|
85
95
|
// framebuffer's Y is down, hence the flip in both directions.
|
|
@@ -96,7 +106,13 @@ struct ROut { @location(0) screen: vec4<f32>, @location(1) hist: vec4<f32> };
|
|
|
96
106
|
let prevUV = vec2<f32>(pndc.x * 0.5 + 0.5, 0.5 - pndc.y * 0.5);
|
|
97
107
|
if (prevUV.x >= 0.0 && prevUV.x <= 1.0 && prevUV.y >= 0.0 && prevUV.y <= 1.0) {
|
|
98
108
|
let hist = clamp(textureSampleLevel(histTex, histSamp, prevUV, 0.0).rgb, mn, mx);
|
|
99
|
-
|
|
109
|
+
// TRUST LESS WHEN MOVING. The further a pixel travelled since last frame, the likelier its
|
|
110
|
+
// history is disoccluded or belongs to different geometry -- and the more a wrong sample reads
|
|
111
|
+
// as a trailing smear. Fade the history weight out with screen-space velocity instead of
|
|
112
|
+
// holding 90% regardless of how fast the camera swings.
|
|
113
|
+
let velPx = length((uv - prevUV) * vec2<f32>(ru.texel.z, ru.texel.w));
|
|
114
|
+
let trust = clamp(1.0 - velPx * 0.045, 0.25, 1.0);
|
|
115
|
+
outc = mix(cur, hist, ru.ctrl.x * trust);
|
|
100
116
|
}
|
|
101
117
|
}
|
|
102
118
|
o.screen = vec4(outc, 1.0); o.hist = vec4(outc, 1.0);
|
|
@@ -185,7 +201,9 @@ export class GpuTaaResolve {
|
|
|
185
201
|
* previous frame's UNJITTERED view-projection. Jitter belongs in the geometry pass's projection
|
|
186
202
|
* only — reprojection done with a jittered matrix would chase its own offset.
|
|
187
203
|
*/
|
|
188
|
-
frame(enc, out, invCurrVP, prevVP, blend = 0.9, enabled = true
|
|
204
|
+
frame(enc, out, invCurrVP, prevVP, blend = 0.9, enabled = true,
|
|
205
|
+
/** Clip width in standard deviations. Lower = harsher rejection, less ghosting, more shimmer. */
|
|
206
|
+
clipSigma = 1.25) {
|
|
189
207
|
if (this.histView.length !== 2)
|
|
190
208
|
return;
|
|
191
209
|
this.u.set(invCurrVP, 0);
|
|
@@ -197,6 +215,7 @@ export class GpuTaaResolve {
|
|
|
197
215
|
this.u[36] = blend;
|
|
198
216
|
this.u[37] = enabled ? 1 : 0;
|
|
199
217
|
this.u[38] = this.primed ? 0 : 1; // first frame after a cut has no history to blend
|
|
218
|
+
this.u[39] = clipSigma;
|
|
200
219
|
this.device.queue.writeBuffer(this.uBuf, 0, this.u);
|
|
201
220
|
const pass = enc.beginRenderPass({
|
|
202
221
|
colorAttachments: [
|
package/dist/loop/gameLoop.d.ts
CHANGED
|
@@ -18,7 +18,8 @@ export interface FrameInfo {
|
|
|
18
18
|
}
|
|
19
19
|
export interface GameLoopOpts {
|
|
20
20
|
render: (info: FrameInfo) => void;
|
|
21
|
-
/** Render FPS per tier. Default { active:
|
|
21
|
+
/** Render FPS per tier. Default { active: Infinity, blurred: Infinity, hidden: 0 } — uncapped
|
|
22
|
+
* while visible. Pass finite numbers to cap; 0 disables rendering for that tier. */
|
|
22
23
|
gameFps?: {
|
|
23
24
|
active: number;
|
|
24
25
|
blurred: number;
|
package/dist/loop/gameLoop.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
2
2
|
// GAME LOOP — the one rAF loop a game hands its render (and optionally its sim) to.
|
|
3
3
|
// It owns three things a hand-rolled loop keeps getting wrong:
|
|
4
|
-
// 1. RENDER PACING by page tier —
|
|
4
|
+
// 1. RENDER PACING by page tier — uncapped while visible / hidden 0 (overridable).
|
|
5
5
|
// 2. A FIXED-STEP SIM accumulator (opt-in via `tick`) that advances on wall time
|
|
6
6
|
// while visible and FREEZES while hidden, with a catch-up clamp.
|
|
7
7
|
// 3. SIM RESTORE on resume — the part games forget. Hand it a NetAdapter and it
|
|
@@ -11,7 +11,14 @@
|
|
|
11
11
|
// catch-up clamping for free. Clock/rAF/pageState are injectable for tests.
|
|
12
12
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
13
13
|
import { createPageState } from './pageState';
|
|
14
|
-
|
|
14
|
+
// UNCAPPED by default. Pinning render to 60 threw away every frame a 120/144Hz display could have
|
|
15
|
+
// shown, and throttling a merely-BLURRED window to 20 made the game stutter whenever focus sat on a
|
|
16
|
+
// devtools panel or a second monitor — neither is something a player asked for. `Infinity` means
|
|
17
|
+
// "render every animation frame"; 0 still means "never render", which is why uncapped cannot just
|
|
18
|
+
// be 0. A caller that genuinely wants to throttle can still pass `gameFps`.
|
|
19
|
+
//
|
|
20
|
+
// `hidden: 0` stays: a hidden tab is not a focus question, and browsers stop firing rAF there anyway.
|
|
21
|
+
const DEFAULT_GAME_FPS = { active: Infinity, blurred: Infinity, hidden: 0 };
|
|
15
22
|
const DEFAULT_SIM = { fps: 60, maxCatchUpFrames: 6 };
|
|
16
23
|
export function runGameLoop(opts) {
|
|
17
24
|
const gameFps = opts.gameFps ?? DEFAULT_GAME_FPS;
|
|
@@ -65,8 +72,8 @@ export function runGameLoop(opts) {
|
|
|
65
72
|
acc -= fixedDt;
|
|
66
73
|
}
|
|
67
74
|
}
|
|
68
|
-
// RENDER: gate to the tier's FPS. 0 ⇒ never render (also covers a hidden tab
|
|
69
|
-
//
|
|
75
|
+
// RENDER: gate to the tier's FPS. 0 ⇒ never render (also covers a hidden tab whose browser still
|
|
76
|
+
// fires the occasional rAF); Infinity ⇒ uncapped, one render per animation frame.
|
|
70
77
|
const fps = gameFps[tier];
|
|
71
78
|
if (fps > 0) {
|
|
72
79
|
renderAcc += wallDt;
|