ugly-game 0.15.21 → 0.15.22
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/gpuShadow.d.ts +59 -0
- package/dist/gpuShadow.js +201 -8
- package/package.json +15 -16
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -129,6 +129,34 @@ export interface AtlasLayout {
|
|
|
129
129
|
* wrapping to an unrelated texture.
|
|
130
130
|
*/
|
|
131
131
|
export declare function packFaceMats(m: readonly number[]): [number, number];
|
|
132
|
+
/**
|
|
133
|
+
* One rigid transform over a contiguous run of instances — see `GpuShadowRenderer.setBodyTransforms`.
|
|
134
|
+
* `first`/`count` are INSTANCE SLOTS in the buffer, matching what `updateInstanceRange` wrote.
|
|
135
|
+
*/
|
|
136
|
+
export interface BodyTransform {
|
|
137
|
+
/** First instance slot the body owns. */
|
|
138
|
+
first: number;
|
|
139
|
+
/** How many consecutive slots. */
|
|
140
|
+
count: number;
|
|
141
|
+
/** World position of the body origin. Default (0,0,0). */
|
|
142
|
+
pos?: [number, number, number];
|
|
143
|
+
/** Body orientation as a quaternion (x, y, z, w). Default identity. */
|
|
144
|
+
rot?: [number, number, number, number];
|
|
145
|
+
}
|
|
146
|
+
/** Rigid-body table: `bodies[0]` is the header, so this is bodies + 1. See `struct Body` in the WGSL. */
|
|
147
|
+
export declare const MAX_BODIES = 32;
|
|
148
|
+
/**
|
|
149
|
+
* Pack the body table into two views over ONE ArrayBuffer, and return how many bodies are live.
|
|
150
|
+
*
|
|
151
|
+
* ⚠️ EXPORTED SO IT CAN BE TESTED WITHOUT A GPU. `Body.range` is `vec4<u32>` while the rest of the
|
|
152
|
+
* struct is f32, so this writes the same 48-byte slot through two typed-array views — the one thing
|
|
153
|
+
* here that can be wrong in a way nothing catches. Write `first` through the f32 view and the shader
|
|
154
|
+
* reads the IEEE bit pattern of 48000.0 as an integer index, which lands somewhere past the end of
|
|
155
|
+
* the buffer and clamps: the body simply never matches and quietly renders at the origin.
|
|
156
|
+
*
|
|
157
|
+
* Slot 0 is the header — see `struct Body` in the WGSL for the field meanings.
|
|
158
|
+
*/
|
|
159
|
+
export declare function packBodyTable(bodies: BodyTransform[], f: Float32Array, u: Uint32Array): number;
|
|
132
160
|
/** The 36-vertex cube as WGSL array-literal bodies (pos + per-face normal). Shared by shadow modules. */
|
|
133
161
|
export declare const CUBE: {
|
|
134
162
|
pos: string;
|
|
@@ -463,6 +491,12 @@ export declare const ALPHA_WGSL: string;
|
|
|
463
491
|
export declare class GpuShadowRenderer {
|
|
464
492
|
private device;
|
|
465
493
|
private instBuf;
|
|
494
|
+
private bodyBuf;
|
|
495
|
+
/** Staging for the body table: f32 and u32 views over ONE buffer, because `Body.range` is u32 and the rest is f32. */
|
|
496
|
+
private bodyScratch;
|
|
497
|
+
private bodyF32;
|
|
498
|
+
private bodyU32;
|
|
499
|
+
private bodyCount;
|
|
466
500
|
private sceneReady;
|
|
467
501
|
private camBuf;
|
|
468
502
|
private shadowTex;
|
|
@@ -689,6 +723,31 @@ export declare class GpuShadowRenderer {
|
|
|
689
723
|
/** How many instances the cull + draw passes consider, whatever is in the buffer past that. Pairs with
|
|
690
724
|
* `updateInstanceRange`: shrinking the dynamic tail is a count change, not an upload. */
|
|
691
725
|
setInstanceCount(n: number): void;
|
|
726
|
+
/**
|
|
727
|
+
* Give contiguous instance RANGES a rigid transform applied on the GPU, so a body that only moves
|
|
728
|
+
* costs a ~48-byte table write instead of a re-upload of every cube it owns.
|
|
729
|
+
*
|
|
730
|
+
* This is the answer to the orbiting-sky-body cost: a planet is thousands of instances whose
|
|
731
|
+
* positions relative to each other never change, and re-packing them in world space each frame was
|
|
732
|
+
* ~283 KB/frame — which also dirtied the shared instance buffer, dragging the untouched terrain
|
|
733
|
+
* segment's upload along with it. Upload the body ONCE in body-local space (`updateInstanceRange`),
|
|
734
|
+
* then call this every frame with its orbit.
|
|
735
|
+
*
|
|
736
|
+
* r.updateInstanceRange(moonCubesInLocalSpace, 48000) // once
|
|
737
|
+
* r.setBodyTransforms([{ first: 48000, count: moon.length, pos: orbitPos, rot: spinQuat }]) // per frame
|
|
738
|
+
*
|
|
739
|
+
* Ranges must not overlap; the first match wins. Positions are body-local, the transform is
|
|
740
|
+
* rotate-then-translate, and a body has no scale (use `CubeInstance.sx/sy/sz` for that).
|
|
741
|
+
*
|
|
742
|
+
* ⚠️ THE RANGES ARE INSTANCE SLOTS, NOT IDENTITIES. They are matched against the instance's slot in
|
|
743
|
+
* the buffer, so re-laying-out your segments silently re-parents whatever now occupies those slots.
|
|
744
|
+
* Reserve fixed capacity per segment — the same discipline `updateInstanceRange` already asks for.
|
|
745
|
+
*
|
|
746
|
+
* Pass `[]` to clear. Bodies past the table's capacity (32) are ignored.
|
|
747
|
+
*/
|
|
748
|
+
setBodyTransforms(bodies: BodyTransform[]): void;
|
|
749
|
+
/** How many rigid bodies the last `setBodyTransforms` installed. */
|
|
750
|
+
bodyTransformCount(): number;
|
|
692
751
|
/** Translucent overlay cubes drawn over the opaque scene; `emissive` carries per-cube alpha (0..1). The buffer
|
|
693
752
|
* GROWS to fit (no hard cap). Pass `eye` to SORT the cubes back-to-front (farthest first) so overlapping
|
|
694
753
|
* translucent cubes blend in the correct order — required for anything denser than the place ghost. By default
|
package/dist/gpuShadow.js
CHANGED
|
@@ -54,6 +54,64 @@ export function packFaceMats(m) {
|
|
|
54
54
|
// for the contact decal, which had to widen it for exactly the same reason.
|
|
55
55
|
const INST_F = 20;
|
|
56
56
|
const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
|
|
57
|
+
/** Rigid-body table: `bodies[0]` is the header, so this is bodies + 1. See `struct Body` in the WGSL. */
|
|
58
|
+
export const MAX_BODIES = 32;
|
|
59
|
+
const BODY_F = 12; // 3 × vec4 = 48 bytes, the WGSL struct stride
|
|
60
|
+
/**
|
|
61
|
+
* Pack the body table into two views over ONE ArrayBuffer, and return how many bodies are live.
|
|
62
|
+
*
|
|
63
|
+
* ⚠️ EXPORTED SO IT CAN BE TESTED WITHOUT A GPU. `Body.range` is `vec4<u32>` while the rest of the
|
|
64
|
+
* struct is f32, so this writes the same 48-byte slot through two typed-array views — the one thing
|
|
65
|
+
* here that can be wrong in a way nothing catches. Write `first` through the f32 view and the shader
|
|
66
|
+
* reads the IEEE bit pattern of 48000.0 as an integer index, which lands somewhere past the end of
|
|
67
|
+
* the buffer and clamps: the body simply never matches and quietly renders at the origin.
|
|
68
|
+
*
|
|
69
|
+
* Slot 0 is the header — see `struct Body` in the WGSL for the field meanings.
|
|
70
|
+
*/
|
|
71
|
+
export function packBodyTable(bodies, f, u) {
|
|
72
|
+
const n = Math.min(bodies.length, MAX_BODIES);
|
|
73
|
+
let lo = 0xffffffff, hi = 0, live = 0;
|
|
74
|
+
for (let i = 0; i < n; i++) {
|
|
75
|
+
const b = bodies[i];
|
|
76
|
+
if (b.count <= 0 || b.first < 0)
|
|
77
|
+
continue; // empty/invalid ranges are skipped, not errors
|
|
78
|
+
const o = (live + 1) * BODY_F, // +1: slot 0 is the header
|
|
79
|
+
r = b.rot, p = b.pos;
|
|
80
|
+
f[o] = r ? r[0] : 0;
|
|
81
|
+
f[o + 1] = r ? r[1] : 0;
|
|
82
|
+
f[o + 2] = r ? r[2] : 0;
|
|
83
|
+
f[o + 3] = r ? r[3] : 1; // identity quaternion by default
|
|
84
|
+
f[o + 4] = p ? p[0] : 0;
|
|
85
|
+
f[o + 5] = p ? p[1] : 0;
|
|
86
|
+
f[o + 6] = p ? p[2] : 0;
|
|
87
|
+
f[o + 7] = 0;
|
|
88
|
+
u[o + 8] = b.first;
|
|
89
|
+
u[o + 9] = b.count;
|
|
90
|
+
u[o + 10] = 0;
|
|
91
|
+
u[o + 11] = 0;
|
|
92
|
+
if (b.first < lo)
|
|
93
|
+
lo = b.first;
|
|
94
|
+
if (b.first + b.count > hi)
|
|
95
|
+
hi = b.first + b.count;
|
|
96
|
+
live++;
|
|
97
|
+
}
|
|
98
|
+
// The header. range.y = 0 is the "no bodies" sentinel every pass early-outs on, and lo/hi bound
|
|
99
|
+
// the loop away from terrain (below lo) and dynamics (at or above hi) for two compares apiece.
|
|
100
|
+
u[8] = live === 0 ? 0 : lo;
|
|
101
|
+
u[9] = live;
|
|
102
|
+
u[10] = live === 0 ? 0 : hi;
|
|
103
|
+
u[11] = 0;
|
|
104
|
+
return live;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* ⚠️ ONE NUMBER SHARED BY THREE LAYOUTS. `bodies` is declared in `commonHeadWgsl`, which the shadow,
|
|
108
|
+
* main AND alpha shaders all splice in, so the slot has to be free in every one of them and every
|
|
109
|
+
* one of their bind-group layouts has to declare it — even the alpha layout, which otherwise stops
|
|
110
|
+
* at binding 1. Main already occupies 0–10 (GI and patchwork included), hence 11.
|
|
111
|
+
*/
|
|
112
|
+
const BODY_BINDING = 11;
|
|
113
|
+
/** The cull pass has its own layout and its own `Inst`/`Body` declarations; nothing is shared with the draw passes. */
|
|
114
|
+
const CULL_BODY_BINDING = 5;
|
|
57
115
|
// Build the 36-vertex cube (6 faces × 2 tris) in JS and inject as WGSL const arrays —
|
|
58
116
|
// bulletproof + readable vs. bit-twiddling from vertex_index. cullMode is 'none', so winding
|
|
59
117
|
// doesn't matter. Positions in [-0.5, 0.5]; flat per-face normals.
|
|
@@ -1351,8 +1409,25 @@ struct Cam {
|
|
|
1351
1409
|
eye: vec4<f32>, // xyz = camera position in world space (material relief + specular); w = CONVEX (chamfer) radius (0 = sharp cube)
|
|
1352
1410
|
};
|
|
1353
1411
|
struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, aux2: vec4<f32> }; // pos.w = hue (<0 ⇒ ground) · scl.w = emissive/alpha · rot = quaternion · aux.x = atlas tile (<0 ⇒ hue) · aux.y = up-face code · aux.z/aux.w = CONTACT DECAL (0,0 = none) · aux2.xy = 27-bit NEIGHBOUR OCCUPANCY split 14/13 (0,0 = unknown ⇒ no corner AO) · aux2.z = per-block albedo jitter · aux2.w = texFit (≠0 ⇒ one atlas tile per face, texture coords NOT scaled by inst.scl)
|
|
1412
|
+
// ── RIGID BODIES ────────────────────────────────────────────────────────────────────────────────
|
|
1413
|
+
//
|
|
1414
|
+
// A CONTIGUOUS RANGE OF INSTANCES THAT MOVES AS ONE. Orbiting sky bodies used to be re-uploaded in
|
|
1415
|
+
// world space every frame — 3,535 instances × 80 B = 282,800 B/frame just to rotate a moon — and
|
|
1416
|
+
// the write invalidated the shared instance buffer's memo, so the whole terrain segment went with
|
|
1417
|
+
// it. A body is instead uploaded ONCE in body-local space and given a rotation + translation here.
|
|
1418
|
+
//
|
|
1419
|
+
// ⚠️ RANGE-BASED, NOT PER-INSTANCE. Inst is exactly five vec4s / 80 bytes with no spare slot, so
|
|
1420
|
+
// a per-instance body id would cost a SIXTH vec4 — +20% on every instance in the world, ~1 MB at
|
|
1421
|
+
// N=128 and 6.4 MB at N=256, to serve a few thousand sky cubes. Bodies already occupy contiguous
|
|
1422
|
+
// ranges (that is precisely what updateInstanceRange produces), so a range table costs ~9 × 48 B.
|
|
1423
|
+
//
|
|
1424
|
+
// bodies[0] is a HEADER, not a body: range.x = lowest instance index owned by any body, range.y =
|
|
1425
|
+
// body count, range.z = one past the highest. Terrain sits below range.x and dynamics above
|
|
1426
|
+
// range.z, so everything that is not a body pays two compares and never enters the loop.
|
|
1427
|
+
struct Body { rot: vec4<f32>, pos: vec4<f32>, range: vec4<u32> };
|
|
1354
1428
|
@group(0) @binding(0) var<uniform> cam: Cam;
|
|
1355
1429
|
@group(0) @binding(1) var<storage, read> insts: array<Inst>;
|
|
1430
|
+
@group(0) @binding(${BODY_BINDING}) var<storage, read> bodies: array<Body>;
|
|
1356
1431
|
|
|
1357
1432
|
const CUBE_POS = array<vec3<f32>, 36>(${CUBE.pos});
|
|
1358
1433
|
const CUBE_NRM = array<vec3<f32>, 36>(${CUBE.nrm});
|
|
@@ -1364,6 +1439,27 @@ const BEV_AXIS = array<vec3<f32>, ${V}>(${B.axisWgsl});
|
|
|
1364
1439
|
fn qrot(q: vec4<f32>, v: vec3<f32>) -> vec3<f32> { let t = 2.0 * cross(q.xyz, v); return v + q.w * t + cross(q.xyz, t); }
|
|
1365
1440
|
fn worldOf(inst: Inst, vi: u32) -> vec3<f32> { return inst.pos.xyz + qrot(inst.rot, CUBE_POS[vi] * inst.scl.xyz); }
|
|
1366
1441
|
|
|
1442
|
+
fn bodyNone() -> Body {
|
|
1443
|
+
return Body(vec4<f32>(0.0, 0.0, 0.0, 1.0), vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<u32>(0u, 0u, 0u, 0u));
|
|
1444
|
+
}
|
|
1445
|
+
// ⚠️ TAKES THE GLOBAL INSTANCE INDEX. The draw passes iterate a CULLED VISIBLE LIST, so
|
|
1446
|
+
// @builtin(instance_index) is a position within that list and means nothing to a range — the
|
|
1447
|
+
// caller must pass vis[ii], the same index it used to fetch the Inst.
|
|
1448
|
+
fn bodyOf(gi: u32) -> Body {
|
|
1449
|
+
let hdr = bodies[0];
|
|
1450
|
+
if (hdr.range.y == 0u || gi < hdr.range.x || gi >= hdr.range.z) { return bodyNone(); }
|
|
1451
|
+
for (var b = 1u; b <= hdr.range.y; b = b + 1u) {
|
|
1452
|
+
let bd = bodies[b];
|
|
1453
|
+
if (gi >= bd.range.x && gi < bd.range.x + bd.range.y) { return bd; }
|
|
1454
|
+
}
|
|
1455
|
+
return bodyNone();
|
|
1456
|
+
}
|
|
1457
|
+
// Rotation + translation only — a body has no scale, so a normal just rotates and needs no
|
|
1458
|
+
// inverse-transpose. (bodyDir on an already-inverse-scaled normal stays correct.)
|
|
1459
|
+
fn bodyPos(b: Body, w: vec3<f32>) -> vec3<f32> { return b.pos.xyz + qrot(b.rot, w); }
|
|
1460
|
+
fn bodyDir(b: Body, v: vec3<f32>) -> vec3<f32> { return qrot(b.rot, v); }
|
|
1461
|
+
fn worldOfB(inst: Inst, vi: u32, gi: u32) -> vec3<f32> { return bodyPos(bodyOf(gi), worldOf(inst, vi)); }
|
|
1462
|
+
|
|
1367
1463
|
// ── PER-CORNER AMBIENT OCCLUSION ────────────────────────────────────────────────────────────────
|
|
1368
1464
|
//
|
|
1369
1465
|
// The dark that collects where two surfaces meet: the inside of a step, the base of a wall, the
|
|
@@ -1883,6 +1979,12 @@ ${maskWgsl(seg, B, V, S)}
|
|
|
1883
1979
|
// the wrong offset, and the symptom is cubes winking out of existence at the edge of view rather
|
|
1884
1980
|
// than anything that looks like a struct mismatch.
|
|
1885
1981
|
struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32>, aux2: vec4<f32> };
|
|
1982
|
+
// ⚠️ A SECOND HAND-MAINTAINED COPY, exactly like Inst above — and this one is load-bearing in a way
|
|
1983
|
+
// that fails silently. If the cull pass skips a body's transform, it culls the sky bodies against
|
|
1984
|
+
// the frustum at their AUTHORED positions while the draw passes put them somewhere else: a moon
|
|
1985
|
+
// vanishes when the place it used to be leaves the screen. Keep in step with struct Body / bodyOf
|
|
1986
|
+
// in commonHeadWgsl.
|
|
1987
|
+
struct Body { rot: vec4<f32>, pos: vec4<f32>, range: vec4<u32> };
|
|
1886
1988
|
struct Args { vc: u32, ic: atomic<u32>, fv: u32, fi: u32 };
|
|
1887
1989
|
struct CullCam {
|
|
1888
1990
|
planes: array<vec4<f32>, 6>, cnt: vec4<f32>, // cnt.x = instance count
|
|
@@ -1894,17 +1996,32 @@ struct CullCam {
|
|
|
1894
1996
|
@group(0) @binding(2) var<storage, read_write> visible: array<u32>;
|
|
1895
1997
|
@group(0) @binding(3) var<uniform> cc: CullCam;
|
|
1896
1998
|
@group(0) @binding(4) var hiz: texture_2d<f32>;
|
|
1999
|
+
@group(0) @binding(${CULL_BODY_BINDING}) var<storage, read> bodies: array<Body>;
|
|
1897
2000
|
fn qr(q: vec4<f32>, v: vec3<f32>) -> vec3<f32> { let t = 2.0 * cross(q.xyz, v); return v + q.w * t + cross(q.xyz, t); }
|
|
2001
|
+
fn bodyNone() -> Body {
|
|
2002
|
+
return Body(vec4<f32>(0.0, 0.0, 0.0, 1.0), vec4<f32>(0.0, 0.0, 0.0, 0.0), vec4<u32>(0u, 0u, 0u, 0u));
|
|
2003
|
+
}
|
|
2004
|
+
fn bodyOf(gi: u32) -> Body {
|
|
2005
|
+
let hdr = bodies[0];
|
|
2006
|
+
if (hdr.range.y == 0u || gi < hdr.range.x || gi >= hdr.range.z) { return bodyNone(); }
|
|
2007
|
+
for (var b = 1u; b <= hdr.range.y; b = b + 1u) {
|
|
2008
|
+
let bd = bodies[b];
|
|
2009
|
+
if (gi >= bd.range.x && gi < bd.range.x + bd.range.y) { return bd; }
|
|
2010
|
+
}
|
|
2011
|
+
return bodyNone();
|
|
2012
|
+
}
|
|
1898
2013
|
@compute @workgroup_size(${CULL_WG})
|
|
1899
2014
|
fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
1900
2015
|
let i = gid.x;
|
|
1901
2016
|
if (i >= u32(cc.cnt.x)) { return; }
|
|
1902
2017
|
let inst = insts[i];
|
|
1903
|
-
|
|
2018
|
+
// The cull pass indexes the instance buffer DIRECTLY, so its instance index is already global.
|
|
2019
|
+
let bd = bodyOf(i);
|
|
2020
|
+
let c = bd.pos.xyz + qr(bd.rot, inst.pos.xyz);
|
|
1904
2021
|
let h = 0.5 * inst.scl.xyz;
|
|
1905
|
-
let bx = qr(inst.rot, vec3<f32>(h.x, 0.0, 0.0)); // rotated box semi-axes
|
|
1906
|
-
let by = qr(inst.rot, vec3<f32>(0.0, h.y, 0.0));
|
|
1907
|
-
let bz = qr(inst.rot, vec3<f32>(0.0, 0.0, h.z));
|
|
2022
|
+
let bx = qr(bd.rot, qr(inst.rot, vec3<f32>(h.x, 0.0, 0.0))); // rotated box semi-axes
|
|
2023
|
+
let by = qr(bd.rot, qr(inst.rot, vec3<f32>(0.0, h.y, 0.0)));
|
|
2024
|
+
let bz = qr(bd.rot, qr(inst.rot, vec3<f32>(0.0, 0.0, h.z)));
|
|
1908
2025
|
for (var p = 0u; p < 6u; p++) {
|
|
1909
2026
|
let pl = cc.planes[p];
|
|
1910
2027
|
let e = abs(dot(pl.xyz, bx)) + abs(dot(pl.xyz, by)) + abs(dot(pl.xyz, bz)); // box half-width along the normal
|
|
@@ -1999,7 +2116,8 @@ export const shadowWgsl = (seg, B, V, S) => commonWgsl(seg, B, V, S) +
|
|
|
1999
2116
|
// Always index [0]: each cascade's shadow pass binds its OWN Cam buffer whose lightVP[0] holds that
|
|
2000
2117
|
// cascade's matrix. One uniform per cascade costs a few hundred bytes and avoids dynamic offsets.
|
|
2001
2118
|
@vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> @builtin(position) vec4<f32> {
|
|
2002
|
-
|
|
2119
|
+
let gi = vis[ii];
|
|
2120
|
+
return cam.lightVP[0] * vec4(worldOfB(insts[gi], vi, gi), 1.0);
|
|
2003
2121
|
}
|
|
2004
2122
|
`;
|
|
2005
2123
|
export const mainWgsl = (seg, B, V, S) => commonWgsl(seg, B, V, S) +
|
|
@@ -2053,7 +2171,9 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2053
2171
|
}
|
|
2054
2172
|
|
|
2055
2173
|
@vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> VOut {
|
|
2056
|
-
let
|
|
2174
|
+
let gi = vis[ii];
|
|
2175
|
+
let inst = insts[gi];
|
|
2176
|
+
let bd = bodyOf(gi); // rigid-body transform for the range this instance belongs to (identity for terrain)
|
|
2057
2177
|
// ⚠️ PATCHWORK REUSES THE OCCUPANCY FLOATS, so the mask must be suppressed when it is on. tile == -2
|
|
2058
2178
|
// says aux2.xy carry six FACE MATERIALS rather than a neighbour mask; reading them as one would bevel and
|
|
2059
2179
|
// shade the cube off a "neighbourhood" made of material ids. There is nothing lost: a patchwork instance
|
|
@@ -2094,7 +2214,7 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2094
2214
|
}
|
|
2095
2215
|
|
|
2096
2216
|
let local = bevelPos(mask, sharp, home, cam.eye.w, BEV_ROLE[vi], BEV_AXIS[vi], cam.csm.z);
|
|
2097
|
-
let world = inst.pos.xyz + qrot(inst.rot, local * inst.scl.xyz);
|
|
2217
|
+
let world = bodyPos(bd, inst.pos.xyz + qrot(inst.rot, local * inst.scl.xyz));
|
|
2098
2218
|
var o: VOut;
|
|
2099
2219
|
o.clip = cam.camVP * vec4(world, 1.0);
|
|
2100
2220
|
o.world = world;
|
|
@@ -2204,7 +2324,10 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
2204
2324
|
}
|
|
2205
2325
|
}
|
|
2206
2326
|
}
|
|
2207
|
-
|
|
2327
|
+
// ⚠️ THE BODY ROTATION BELONGS HERE TOO. Transforming the position without the normal lights a
|
|
2328
|
+
// rotating moon as if it were still at its authored orientation — the terminator stops moving
|
|
2329
|
+
// with the geometry, which reads as flat shading rather than as a transform bug.
|
|
2330
|
+
o.nrm = normalize(bodyDir(bd, qrot(inst.rot, shade / inst.scl.xyz)));
|
|
2208
2331
|
o.hue = inst.pos.w;
|
|
2209
2332
|
o.emis = inst.scl.w;
|
|
2210
2333
|
// OBJECT-space block coord (per-sub-block units, anchored to the model, NOT world) so per-block AO moves +
|
|
@@ -2549,6 +2672,10 @@ struct AOut { @builtin(position) clip: vec4<f32>, @location(0) hue: f32, @locati
|
|
|
2549
2672
|
@vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> AOut {
|
|
2550
2673
|
let inst = insts[ii];
|
|
2551
2674
|
var o: AOut;
|
|
2675
|
+
// ⚠️ NO BODY TRANSFORM HERE, AND THAT IS NOT AN OVERSIGHT. This pass binds the OVERLAY buffer to
|
|
2676
|
+
// insts, not the scene instance buffer, so ii lives in a different index space entirely — the
|
|
2677
|
+
// body ranges mean nothing against it. Resolving a body here would transform whichever overlay cube
|
|
2678
|
+
// happened to land in a body's range, i.e. the place ghost would teleport into orbit near a moon.
|
|
2552
2679
|
o.clip = cam.camVP * vec4(worldOf(inst, vi), 1.0);
|
|
2553
2680
|
o.hue = inst.pos.w; o.a = inst.scl.w;
|
|
2554
2681
|
return o;
|
|
@@ -2572,6 +2699,12 @@ export const ALPHA_WGSL = alphaWgsl(B1.seg, B1.cube, B1.verts, B1.sec);
|
|
|
2572
2699
|
export class GpuShadowRenderer {
|
|
2573
2700
|
device;
|
|
2574
2701
|
instBuf;
|
|
2702
|
+
bodyBuf;
|
|
2703
|
+
/** Staging for the body table: f32 and u32 views over ONE buffer, because `Body.range` is u32 and the rest is f32. */
|
|
2704
|
+
bodyScratch = new ArrayBuffer((MAX_BODIES + 1) * BODY_F * 4);
|
|
2705
|
+
bodyF32 = new Float32Array(this.bodyScratch);
|
|
2706
|
+
bodyU32 = new Uint32Array(this.bodyScratch);
|
|
2707
|
+
bodyCount = 0;
|
|
2575
2708
|
sceneReady = false; // instBuf exists — setScene/setCapacity has run at least once
|
|
2576
2709
|
camBuf;
|
|
2577
2710
|
shadowTex = null; // null until the first frame picks a cascade count
|
|
@@ -2688,6 +2821,19 @@ export class GpuShadowRenderer {
|
|
|
2688
2821
|
const mainMod = device.createShaderModule({
|
|
2689
2822
|
code: mainWgsl(this.bev.seg, this.bev.cube, this.bev.verts, this.bev.sec),
|
|
2690
2823
|
});
|
|
2824
|
+
// The rigid-body table. Allocated once at full capacity and zero-filled, so `bodies[0].range.y`
|
|
2825
|
+
// is 0 — the "no bodies" sentinel — and every pass runs the identity path until a caller sets one.
|
|
2826
|
+
this.bodyBuf = device.createBuffer({
|
|
2827
|
+
size: (MAX_BODIES + 1) * BODY_F * 4,
|
|
2828
|
+
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
2829
|
+
});
|
|
2830
|
+
device.queue.writeBuffer(this.bodyBuf, 0, new Float32Array((MAX_BODIES + 1) * BODY_F));
|
|
2831
|
+
/** The `bodies` entry, identical in all three draw layouts. See `BODY_BINDING`. */
|
|
2832
|
+
const bodyEntry = {
|
|
2833
|
+
binding: BODY_BINDING,
|
|
2834
|
+
visibility: GPUShaderStage.VERTEX,
|
|
2835
|
+
buffer: { type: 'read-only-storage' },
|
|
2836
|
+
};
|
|
2691
2837
|
const shadowBGL = device.createBindGroupLayout({
|
|
2692
2838
|
entries: [
|
|
2693
2839
|
{
|
|
@@ -2705,6 +2851,7 @@ export class GpuShadowRenderer {
|
|
|
2705
2851
|
visibility: GPUShaderStage.VERTEX,
|
|
2706
2852
|
buffer: { type: 'read-only-storage' },
|
|
2707
2853
|
}, // light-visible list
|
|
2854
|
+
bodyEntry,
|
|
2708
2855
|
],
|
|
2709
2856
|
});
|
|
2710
2857
|
this.shadowPipe = device.createRenderPipeline({
|
|
@@ -2770,6 +2917,7 @@ export class GpuShadowRenderer {
|
|
|
2770
2917
|
visibility: GPUShaderStage.FRAGMENT,
|
|
2771
2918
|
buffer: { type: 'read-only-storage' },
|
|
2772
2919
|
}, // GI probe irradiance
|
|
2920
|
+
bodyEntry,
|
|
2773
2921
|
],
|
|
2774
2922
|
});
|
|
2775
2923
|
this.mainPipe = device.createRenderPipeline({
|
|
@@ -2818,6 +2966,10 @@ export class GpuShadowRenderer {
|
|
|
2818
2966
|
visibility: GPUShaderStage.VERTEX,
|
|
2819
2967
|
buffer: { type: 'read-only-storage' },
|
|
2820
2968
|
},
|
|
2969
|
+
// The overlay pass deliberately does NOT resolve bodies (its `insts` is the overlay buffer, a
|
|
2970
|
+
// different index space — see alphaWgsl), but commonHeadWgsl still DECLARES the binding, so the
|
|
2971
|
+
// layout has to carry it or pipeline creation fails on a layout/module mismatch.
|
|
2972
|
+
bodyEntry,
|
|
2821
2973
|
],
|
|
2822
2974
|
}));
|
|
2823
2975
|
this.alphaPipe = device.createRenderPipeline({
|
|
@@ -2860,6 +3012,7 @@ export class GpuShadowRenderer {
|
|
|
2860
3012
|
entries: [
|
|
2861
3013
|
{ binding: 0, resource: { buffer: this.camBuf } },
|
|
2862
3014
|
{ binding: 1, resource: { buffer: this.overlayBuf } },
|
|
3015
|
+
{ binding: BODY_BINDING, resource: { buffer: this.bodyBuf } },
|
|
2863
3016
|
],
|
|
2864
3017
|
});
|
|
2865
3018
|
// GPU-driven cull: compute pipeline shared by the camera + light dispatches (each with its own args/vis/uniform).
|
|
@@ -2893,6 +3046,11 @@ export class GpuShadowRenderer {
|
|
|
2893
3046
|
visibility: GPUShaderStage.COMPUTE,
|
|
2894
3047
|
texture: { sampleType: 'unfilterable-float' },
|
|
2895
3048
|
}, // Hi-Z (dummy until occlusion is used)
|
|
3049
|
+
{
|
|
3050
|
+
binding: CULL_BODY_BINDING,
|
|
3051
|
+
visibility: GPUShaderStage.COMPUTE,
|
|
3052
|
+
buffer: { type: 'read-only-storage' },
|
|
3053
|
+
}, // rigid-body table — MUST match the draw passes or bodies cull at the wrong place
|
|
2896
3054
|
],
|
|
2897
3055
|
});
|
|
2898
3056
|
this.cullPipe = device.createComputePipeline({
|
|
@@ -3019,6 +3177,7 @@ export class GpuShadowRenderer {
|
|
|
3019
3177
|
{ binding: 2, resource: { buffer: vis } },
|
|
3020
3178
|
{ binding: 3, resource: { buffer: cull } },
|
|
3021
3179
|
{ binding: 4, resource: this.occView },
|
|
3180
|
+
{ binding: CULL_BODY_BINDING, resource: { buffer: this.bodyBuf } },
|
|
3022
3181
|
],
|
|
3023
3182
|
});
|
|
3024
3183
|
}
|
|
@@ -3152,6 +3311,7 @@ export class GpuShadowRenderer {
|
|
|
3152
3311
|
{ binding: 0, resource: { buffer: this.shadowCamBufs[i] } },
|
|
3153
3312
|
{ binding: 1, resource: { buffer: this.instBuf } },
|
|
3154
3313
|
{ binding: 2, resource: { buffer: vis } },
|
|
3314
|
+
{ binding: BODY_BINDING, resource: { buffer: this.bodyBuf } },
|
|
3155
3315
|
],
|
|
3156
3316
|
}));
|
|
3157
3317
|
// One per vertex-count bucket: identical except for the slice of the visible list at binding 4.
|
|
@@ -3175,6 +3335,7 @@ export class GpuShadowRenderer {
|
|
|
3175
3335
|
{ binding: 8, resource: { buffer: this.giGridBuf } },
|
|
3176
3336
|
{ binding: 9, resource: { buffer: this.giProbeBuf } },
|
|
3177
3337
|
{ binding: 10, resource: { buffer: this.facePatchBuf } },
|
|
3338
|
+
{ binding: BODY_BINDING, resource: { buffer: this.bodyBuf } },
|
|
3178
3339
|
],
|
|
3179
3340
|
}));
|
|
3180
3341
|
}
|
|
@@ -3347,6 +3508,38 @@ export class GpuShadowRenderer {
|
|
|
3347
3508
|
setInstanceCount(n) {
|
|
3348
3509
|
this.count = Math.max(0, Math.min(n, (this.scratch.length / INST_F) | 0));
|
|
3349
3510
|
}
|
|
3511
|
+
/**
|
|
3512
|
+
* Give contiguous instance RANGES a rigid transform applied on the GPU, so a body that only moves
|
|
3513
|
+
* costs a ~48-byte table write instead of a re-upload of every cube it owns.
|
|
3514
|
+
*
|
|
3515
|
+
* This is the answer to the orbiting-sky-body cost: a planet is thousands of instances whose
|
|
3516
|
+
* positions relative to each other never change, and re-packing them in world space each frame was
|
|
3517
|
+
* ~283 KB/frame — which also dirtied the shared instance buffer, dragging the untouched terrain
|
|
3518
|
+
* segment's upload along with it. Upload the body ONCE in body-local space (`updateInstanceRange`),
|
|
3519
|
+
* then call this every frame with its orbit.
|
|
3520
|
+
*
|
|
3521
|
+
* r.updateInstanceRange(moonCubesInLocalSpace, 48000) // once
|
|
3522
|
+
* r.setBodyTransforms([{ first: 48000, count: moon.length, pos: orbitPos, rot: spinQuat }]) // per frame
|
|
3523
|
+
*
|
|
3524
|
+
* Ranges must not overlap; the first match wins. Positions are body-local, the transform is
|
|
3525
|
+
* rotate-then-translate, and a body has no scale (use `CubeInstance.sx/sy/sz` for that).
|
|
3526
|
+
*
|
|
3527
|
+
* ⚠️ THE RANGES ARE INSTANCE SLOTS, NOT IDENTITIES. They are matched against the instance's slot in
|
|
3528
|
+
* the buffer, so re-laying-out your segments silently re-parents whatever now occupies those slots.
|
|
3529
|
+
* Reserve fixed capacity per segment — the same discipline `updateInstanceRange` already asks for.
|
|
3530
|
+
*
|
|
3531
|
+
* Pass `[]` to clear. Bodies past the table's capacity (32) are ignored.
|
|
3532
|
+
*/
|
|
3533
|
+
setBodyTransforms(bodies) {
|
|
3534
|
+
const live = packBodyTable(bodies, this.bodyF32, this.bodyU32);
|
|
3535
|
+
// Only the header and the live entries — a stale tail is unreachable past range.y.
|
|
3536
|
+
this.device.queue.writeBuffer(this.bodyBuf, 0, this.bodyF32, 0, (live + 1) * BODY_F);
|
|
3537
|
+
this.bodyCount = live;
|
|
3538
|
+
}
|
|
3539
|
+
/** How many rigid bodies the last `setBodyTransforms` installed. */
|
|
3540
|
+
bodyTransformCount() {
|
|
3541
|
+
return this.bodyCount;
|
|
3542
|
+
}
|
|
3350
3543
|
/** Translucent overlay cubes drawn over the opaque scene; `emissive` carries per-cube alpha (0..1). The buffer
|
|
3351
3544
|
* GROWS to fit (no hard cap). Pass `eye` to SORT the cubes back-to-front (farthest first) so overlapping
|
|
3352
3545
|
* translucent cubes blend in the correct order — required for anything denser than the place ghost. By default
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ugly-game",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.22",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -15,20 +15,6 @@
|
|
|
15
15
|
"files": [
|
|
16
16
|
"dist"
|
|
17
17
|
],
|
|
18
|
-
"scripts": {
|
|
19
|
-
"preinstall": "npx only-allow pnpm",
|
|
20
|
-
"build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
|
|
21
|
-
"lint": "ugly-app lint",
|
|
22
|
-
"lint:fix": "ugly-app lint --fix",
|
|
23
|
-
"lint:wgsl": "node --experimental-strip-types tools/wgslGuard.mts && node --experimental-strip-types tools/wgslLint.mts",
|
|
24
|
-
"test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts",
|
|
25
|
-
"test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
|
|
26
|
-
"test:flight-browser": "playwright test -c playwright.flight.config.ts",
|
|
27
|
-
"test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
|
|
28
|
-
"test:types": "tsc -p tsconfig.test.json",
|
|
29
|
-
"pretty": "prettier --write .",
|
|
30
|
-
"prepublishOnly": "pnpm run pretty && ugly-app lint && tsc -p tsconfig.json && pnpm run lint:wgsl && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/"
|
|
31
|
-
},
|
|
32
18
|
"dependencies": {
|
|
33
19
|
"meshoptimizer": "^1.2.0"
|
|
34
20
|
},
|
|
@@ -44,5 +30,18 @@
|
|
|
44
30
|
"typescript": "^5.6.0",
|
|
45
31
|
"ugly-app": "^0.1.841",
|
|
46
32
|
"wesl": "0.7.28"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"preinstall": "npx only-allow pnpm",
|
|
36
|
+
"build": "tsc -p tsconfig.json && mkdir -p dist/assets/basis && cp src/assets/basis/basis_transcoder.js src/assets/basis/basis_transcoder.bin dist/assets/basis/",
|
|
37
|
+
"lint": "ugly-app lint",
|
|
38
|
+
"lint:fix": "ugly-app lint --fix",
|
|
39
|
+
"lint:wgsl": "node --experimental-strip-types tools/wgslGuard.mts && node --experimental-strip-types tools/wgslLint.mts",
|
|
40
|
+
"test": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/*.test.mts",
|
|
41
|
+
"test:flight": "node --experimental-strip-types --import ./tools/tsResolve.mjs --test src/flight*.test.mts",
|
|
42
|
+
"test:flight-browser": "playwright test -c playwright.flight.config.ts",
|
|
43
|
+
"test:flight-perf": "playwright test -c playwright.flight.config.ts --grep 'CPU and retained-memory'",
|
|
44
|
+
"test:types": "tsc -p tsconfig.test.json",
|
|
45
|
+
"pretty": "prettier --write ."
|
|
47
46
|
}
|
|
48
|
-
}
|
|
47
|
+
}
|