ugly-game 0.9.0 → 0.11.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/assets/gpuProps.d.ts +2 -1
- package/dist/assets/gpuProps.js +35 -11
- package/dist/assets/gpuSkin.d.ts +2 -1
- package/dist/assets/gpuSkin.js +34 -11
- package/dist/gpuShadow.d.ts +27 -3
- package/dist/gpuShadow.js +216 -76
- package/dist/gpuShadowMap.js +6 -0
- package/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export declare const MAX_CASCADES = 4;
|
|
1
2
|
/**
|
|
2
3
|
* Per-world surface + motion parameters.
|
|
3
4
|
*
|
|
@@ -130,5 +131,5 @@ export declare class GpuPropsRenderer {
|
|
|
130
131
|
setInstances(instances: PropInstance[]): void;
|
|
131
132
|
/** Occluder pass: draw all instanced props' depth into the shared shadow map from the sun's ortho view. */
|
|
132
133
|
shadowPass(pass: GPURenderPassEncoder, lightVP: Float32Array): void;
|
|
133
|
-
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, up?: [number, number, number], lightVP?: Float32Array, shadowView?: GPUTextureView, shadowSamp?: GPUSampler): void;
|
|
134
|
+
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, up?: [number, number, number], lightVP?: Float32Array | Float32Array[], shadowView?: GPUTextureView, shadowSamp?: GPUSampler): void;
|
|
134
135
|
}
|
package/dist/assets/gpuProps.js
CHANGED
|
@@ -3,11 +3,15 @@
|
|
|
3
3
|
// model (tree/rock/bush/…) many times at per-instance world transforms, lit + fogged to match the
|
|
4
4
|
// terrain. Runs in a load pass after the terrain so props depth-test against the surface.
|
|
5
5
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
6
|
+
export const MAX_CASCADES = 4; // must match gpuShadow's
|
|
6
7
|
const SHADER = /* wgsl */ `
|
|
7
8
|
// wind = (dirX, dirZ, amplitude, phase) — sway, amplitude scaled by the world's air density
|
|
8
9
|
// surf = (grainScale, grainDepth, roughness, emissive)
|
|
9
10
|
// grade = (hueShift, saturation, valueLift, unused)
|
|
10
|
-
|
|
11
|
+
// csm + cascades are APPENDED, not woven in: every field above keeps the byte offset it had, so the
|
|
12
|
+
// existing writes in frame() are untouched and a caller that knows nothing about cascades stays
|
|
13
|
+
// byte-identical. lightVP remains the legacy cascade-0 slot; shading reads cascades instead.
|
|
14
|
+
struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<f32>, amb: vec4<f32>, cam: vec4<f32>, fog: vec4<f32>, up: vec4<f32>, wind: vec4<f32>, surf: vec4<f32>, grade: vec4<f32>, csm: vec4<f32>, cascades: array<mat4x4<f32>, ${MAX_CASCADES}> };
|
|
11
15
|
@group(0) @binding(0) var<uniform> u: U;
|
|
12
16
|
// One instance. 'm' is the placement; d0/d1/d2 are the per-instance DEFORM, which is what lets
|
|
13
17
|
// thousands of visibly different plants share a single mesh and a single draw call. Baking each
|
|
@@ -20,7 +24,7 @@ struct Inst { m: mat4x4<f32>, d0: vec4<f32>, d1: vec4<f32>, d2: vec4<f32> };
|
|
|
20
24
|
@group(0) @binding(1) var<storage,read> models: array<Inst>;
|
|
21
25
|
@group(2) @binding(0) var albedoTex: texture_2d<f32>;
|
|
22
26
|
@group(2) @binding(1) var albedoSamp: sampler;
|
|
23
|
-
@group(1) @binding(0) var shadowMap:
|
|
27
|
+
@group(1) @binding(0) var shadowMap: texture_depth_2d_array; // the SAME cascaded sun shadow map the terrain cubes sample
|
|
24
28
|
@group(1) @binding(1) var shadowSamp: sampler_comparison;
|
|
25
29
|
struct VO { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) n: vec3<f32>, @location(2) c: vec3<f32>, @location(3) l: vec3<f32>, @location(4) uv: vec2<f32> };
|
|
26
30
|
// WIND SWAY. Displacement is weighted by height above the instance's own anchor, so a trunk stays
|
|
@@ -106,21 +110,35 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
|
|
|
106
110
|
return max(dot(N, L), 0.0);
|
|
107
111
|
}
|
|
108
112
|
// PCF 3×3 sun shadow — mirrors gpuShadow.shadowFactor so props sit in the SAME shadow as the terrain. 1 = lit, 0 = shadowed.
|
|
109
|
-
fn
|
|
110
|
-
let lp = u.
|
|
113
|
+
fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>) -> f32 {
|
|
114
|
+
let lp = u.cascades[layer] * vec4(world, 1.0);
|
|
111
115
|
let proj = lp.xyz / lp.w;
|
|
112
116
|
let uv = proj.xy * vec2(0.5, -0.5) + vec2(0.5, 0.5);
|
|
113
|
-
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
114
|
-
|
|
117
|
+
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
118
|
+
*inside = false; return 1.0;
|
|
119
|
+
}
|
|
120
|
+
*inside = true;
|
|
121
|
+
let bias = max(0.0006 * (1.0 - ndl), 0.0004) * (1.0 + f32(layer) * 1.5);
|
|
115
122
|
let texel = 1.0 / 2048.0;
|
|
116
123
|
var s = 0.0;
|
|
117
124
|
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
118
125
|
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
119
|
-
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, proj.z - bias);
|
|
126
|
+
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, layer, proj.z - bias);
|
|
120
127
|
}
|
|
121
128
|
}
|
|
122
129
|
return s / 9.0;
|
|
123
130
|
}
|
|
131
|
+
// Tightest cascade that CONTAINS the point wins — the same rule gpuShadow uses, so a prop and the
|
|
132
|
+
// terrain under it always resolve against the same map and cannot disagree about being in shadow.
|
|
133
|
+
fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
|
|
134
|
+
let n = i32(u.csm.x);
|
|
135
|
+
var inside = false;
|
|
136
|
+
for (var c = 0; c < n; c = c + 1) {
|
|
137
|
+
let f = pcfLayer(world, ndl, c, &inside);
|
|
138
|
+
if (inside) { return f; }
|
|
139
|
+
}
|
|
140
|
+
return 1.0;
|
|
141
|
+
}
|
|
124
142
|
// ── PROCEDURAL SURFACE ───────────────────────────────────────────────────────
|
|
125
143
|
// Detail is generated in the shader rather than sampled from a texture. Three reasons, all of them
|
|
126
144
|
// load-bearing for a procedurally-populated universe: generated meshes have unreliable UVs (so
|
|
@@ -340,7 +358,7 @@ export class GpuPropsRenderer {
|
|
|
340
358
|
},
|
|
341
359
|
});
|
|
342
360
|
this.uBuf = device.createBuffer({
|
|
343
|
-
size: 128 + 16 * 9
|
|
361
|
+
size: 128 + 16 * 9 + 16 + 64 * MAX_CASCADES, // + csm vec4 + the cascade matrices
|
|
344
362
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
345
363
|
});
|
|
346
364
|
this.lightBuf = device.createBuffer({
|
|
@@ -352,7 +370,7 @@ export class GpuPropsRenderer {
|
|
|
352
370
|
format: 'depth32float',
|
|
353
371
|
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT,
|
|
354
372
|
});
|
|
355
|
-
this.dummyShadowView = dummy.createView();
|
|
373
|
+
this.dummyShadowView = dummy.createView({ dimension: '2d-array' }); // the binding is an array now
|
|
356
374
|
this.dummyShadowSamp = device.createSampler({ compare: 'less' });
|
|
357
375
|
}
|
|
358
376
|
/**
|
|
@@ -566,9 +584,10 @@ export class GpuPropsRenderer {
|
|
|
566
584
|
// in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
|
|
567
585
|
lightVP, shadowView, shadowSamp) {
|
|
568
586
|
const receive = !!(lightVP && shadowView && shadowSamp);
|
|
569
|
-
const
|
|
587
|
+
const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
|
|
588
|
+
const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES);
|
|
570
589
|
u.set(camVP, 0);
|
|
571
|
-
u.set(
|
|
590
|
+
u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
|
|
572
591
|
u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
|
|
573
592
|
u.set([sunCol[0], sunCol[1], sunCol[2], 0], 36);
|
|
574
593
|
u.set([amb[0], amb[1], amb[2], 0], 40);
|
|
@@ -582,6 +601,11 @@ export class GpuPropsRenderer {
|
|
|
582
601
|
u.set([w.windDir[0], w.windDir[1], w.windAmp, this.phase], 56);
|
|
583
602
|
u.set([w.grainScale, w.grainDepth, w.roughness, w.emissive], 60);
|
|
584
603
|
u.set([w.hueShift, w.saturation, w.valueLift, 0], 64);
|
|
604
|
+
// csm.x = how many cascades are live; the matrices follow. Zero when not receiving, so the
|
|
605
|
+
// selection loop exits at once and returns fully lit — the old no-shadow path exactly.
|
|
606
|
+
u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
|
|
607
|
+
for (let i = 0; i < Math.min(MAX_CASCADES, mats.length); i++)
|
|
608
|
+
u.set(mats[i], 72 + i * 16);
|
|
585
609
|
this.device.queue.writeBuffer(this.uBuf, 0, u);
|
|
586
610
|
// group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
|
|
587
611
|
const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
|
package/dist/assets/gpuSkin.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface SkinCreature {
|
|
|
8
8
|
palette: Float32Array;
|
|
9
9
|
model: Float32Array;
|
|
10
10
|
}
|
|
11
|
+
export declare const MAX_CASCADES = 4;
|
|
11
12
|
export declare class GpuSkinRenderer {
|
|
12
13
|
private device;
|
|
13
14
|
private skinPipe;
|
|
@@ -22,5 +23,5 @@ export declare class GpuSkinRenderer {
|
|
|
22
23
|
setCreatures(list: SkinCreature[]): void;
|
|
23
24
|
/** Deform + draw every creature with the shared scene lighting. load=true composites over the world;
|
|
24
25
|
* otherwise clears to `clear`. sun/sunCol/amb/cam/fog/fogDensity match the terrain & prop renderers. */
|
|
25
|
-
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, clear: [number, number, number], load?: boolean, lightVP?: Float32Array, shadowView?: GPUTextureView, shadowSamp?: GPUSampler): void;
|
|
26
|
+
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, camVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], amb: [number, number, number], camPos: [number, number, number], fog: [number, number, number], fogDensity: number, clear: [number, number, number], load?: boolean, lightVP?: Float32Array | Float32Array[], shadowView?: GPUTextureView, shadowSamp?: GPUSampler): void;
|
|
26
27
|
}
|
package/dist/assets/gpuSkin.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// lighting model as the terrain/prop renderers — sun lambert on smooth per-vertex normals + ambient +
|
|
5
5
|
// distance fog — so creatures sit in the scene consistently instead of looking flat/mismatched.
|
|
6
6
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
7
|
+
export const MAX_CASCADES = 4; // must match gpuShadow's
|
|
7
8
|
const SKIN = /* wgsl */ `
|
|
8
9
|
@group(0) @binding(0) var<storage,read> rest: array<vec4f>;
|
|
9
10
|
@group(0) @binding(1) var<storage,read> joints: array<vec4u>;
|
|
@@ -21,30 +22,46 @@ fn rot(m: mat4x4f, v: vec3f) -> vec3f { return mat3x3f(m[0].xyz, m[1].xyz, m[2].
|
|
|
21
22
|
deformed[i]=vec4f(a.xyz,1.0); defN[i]=vec4f(normalize(b),0.0);
|
|
22
23
|
}`;
|
|
23
24
|
const DRAW = /* wgsl */ `
|
|
24
|
-
|
|
25
|
+
// csm + cascades are APPENDED so every field above keeps its byte offset — a caller that knows
|
|
26
|
+
// nothing about cascades writes exactly the bytes it always did. lightVP stays as the legacy slot.
|
|
27
|
+
struct U { mvp: mat4x4<f32>, lightVP: mat4x4<f32>, sun: vec4<f32>, sunCol: vec4<f32>, amb: vec4<f32>, cam: vec4<f32>, fog: vec4<f32>, model: mat4x4<f32>, csm: vec4<f32>, cascades: array<mat4x4<f32>, ${MAX_CASCADES}> };
|
|
25
28
|
@group(0) @binding(0) var<uniform> u: U;
|
|
26
|
-
@group(1) @binding(0) var shadowMap:
|
|
29
|
+
@group(1) @binding(0) var shadowMap: texture_depth_2d_array; // the SAME cascaded sun shadow map the terrain cubes sample
|
|
27
30
|
@group(1) @binding(1) var shadowSamp: sampler_comparison;
|
|
28
31
|
struct VOut { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) col: vec3<f32>, @location(2) n: vec3<f32> };
|
|
29
32
|
// o.world must be the WORLD position (model·p), not the local deformed p — else the fog distance is computed
|
|
30
33
|
// against the world-space camera and blows up on a planet surface (huge coords), washing creatures into the fog.
|
|
31
34
|
@vertex fn vs(@location(0) p: vec3<f32>, @location(1) c: vec3<f32>, @location(2) nrm: vec3<f32>) -> VOut { var o: VOut; o.pos=u.mvp*vec4(p,1.0); o.world=(u.model*vec4(p,1.0)).xyz; o.col=c; o.n=nrm; return o; }
|
|
32
35
|
// PCF 3×3 sun shadow — mirrors gpuShadow.shadowFactor so creatures sit in the SAME shadow as the terrain. 1 = lit, 0 = shadowed.
|
|
33
|
-
fn
|
|
34
|
-
let lp = u.
|
|
36
|
+
fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>) -> f32 {
|
|
37
|
+
let lp = u.cascades[layer] * vec4(world, 1.0);
|
|
35
38
|
let proj = lp.xyz / lp.w;
|
|
36
39
|
let uv = proj.xy * vec2(0.5, -0.5) + vec2(0.5, 0.5);
|
|
37
|
-
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
38
|
-
|
|
40
|
+
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
41
|
+
*inside = false; return 1.0;
|
|
42
|
+
}
|
|
43
|
+
*inside = true;
|
|
44
|
+
let bias = max(0.0006 * (1.0 - ndl), 0.0004) * (1.0 + f32(layer) * 1.5);
|
|
39
45
|
let texel = 1.0 / 2048.0;
|
|
40
46
|
var s = 0.0;
|
|
41
47
|
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
42
48
|
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
43
|
-
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, proj.z - bias);
|
|
49
|
+
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, layer, proj.z - bias);
|
|
44
50
|
}
|
|
45
51
|
}
|
|
46
52
|
return s / 9.0;
|
|
47
53
|
}
|
|
54
|
+
// Tightest cascade CONTAINING the point — the same rule gpuShadow and gpuProps use, so a creature
|
|
55
|
+
// and the ground it stands on can never disagree about being in shadow.
|
|
56
|
+
fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
|
|
57
|
+
let n = i32(u.csm.x);
|
|
58
|
+
var inside = false;
|
|
59
|
+
for (var c = 0; c < n; c = c + 1) {
|
|
60
|
+
let f = pcfLayer(world, ndl, c, &inside);
|
|
61
|
+
if (inside) { return f; }
|
|
62
|
+
}
|
|
63
|
+
return 1.0;
|
|
64
|
+
}
|
|
48
65
|
@fragment fn fs(i: VOut) -> @location(0) vec4<f32> {
|
|
49
66
|
let d = clamp(dot(normalize(i.n), -normalize(u.sun.xyz)), 0.0, 1.0);
|
|
50
67
|
let sh = mix(1.0, shadowFactor(i.world, d), u.sun.w); // u.sun.w = shadow-receive enable (0 ⇒ off, byte-identical for demo)
|
|
@@ -106,7 +123,7 @@ export class GpuSkinRenderer {
|
|
|
106
123
|
format: 'depth32float',
|
|
107
124
|
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT,
|
|
108
125
|
});
|
|
109
|
-
this.dummyShadowView = dummy.createView();
|
|
126
|
+
this.dummyShadowView = dummy.createView({ dimension: '2d-array' }); // the binding is an array now
|
|
110
127
|
this.dummyShadowSamp = device.createSampler({ compare: 'less' });
|
|
111
128
|
}
|
|
112
129
|
setCreatures(list) {
|
|
@@ -167,7 +184,7 @@ export class GpuSkinRenderer {
|
|
|
167
184
|
],
|
|
168
185
|
});
|
|
169
186
|
const uBuf = dev.createBuffer({
|
|
170
|
-
size: 96 + 16 * 7 + 64, // +
|
|
187
|
+
size: 96 + 16 * 7 + 64 + 16 + 64 * MAX_CASCADES, // + csm vec4 + the cascade matrices
|
|
171
188
|
usage: U.UNIFORM | U.COPY_DST,
|
|
172
189
|
});
|
|
173
190
|
const uBG = dev.createBindGroup({
|
|
@@ -196,6 +213,7 @@ export class GpuSkinRenderer {
|
|
|
196
213
|
// sit in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
|
|
197
214
|
lightVP, shadowView, shadowSamp) {
|
|
198
215
|
const receive = !!(lightVP && shadowView && shadowSamp);
|
|
216
|
+
const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
|
|
199
217
|
const cp = enc.beginComputePass();
|
|
200
218
|
cp.setPipeline(this.skinPipe);
|
|
201
219
|
for (const p of this.packed) {
|
|
@@ -204,15 +222,20 @@ export class GpuSkinRenderer {
|
|
|
204
222
|
}
|
|
205
223
|
cp.end();
|
|
206
224
|
for (const p of this.packed) {
|
|
207
|
-
const u = new Float32Array(68);
|
|
225
|
+
const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES);
|
|
208
226
|
u.set(mul16(camVP, p.model), 0);
|
|
209
|
-
u.set(
|
|
227
|
+
u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
|
|
210
228
|
u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
|
|
211
229
|
u.set([sunCol[0], sunCol[1], sunCol[2], 0], 36);
|
|
212
230
|
u.set([amb[0], amb[1], amb[2], 0], 40);
|
|
213
231
|
u.set([camPos[0], camPos[1], camPos[2], 0], 44);
|
|
214
232
|
u.set([fog[0], fog[1], fog[2], fogDensity], 48);
|
|
215
233
|
u.set(p.model, 52); // world model matrix (for correct fog distance in the vs)
|
|
234
|
+
// csm.x = 0 when not receiving → the loop exits at once and returns fully lit, which is
|
|
235
|
+
// exactly the old no-shadow path.
|
|
236
|
+
u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
|
|
237
|
+
for (let i = 0; i < Math.min(MAX_CASCADES, mats.length); i++)
|
|
238
|
+
u.set(mats[i], 72 + i * 16);
|
|
216
239
|
this.device.queue.writeBuffer(p.uBuf, 0, u);
|
|
217
240
|
}
|
|
218
241
|
// group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
|
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -30,18 +30,26 @@ export declare const CUBE: {
|
|
|
30
30
|
export declare class GpuShadowRenderer {
|
|
31
31
|
private device;
|
|
32
32
|
private instBuf;
|
|
33
|
+
private sceneReady;
|
|
33
34
|
private camBuf;
|
|
34
35
|
private shadowTex;
|
|
35
36
|
private shadowView;
|
|
37
|
+
private shadowArrayView;
|
|
38
|
+
private shadowLayerViews;
|
|
39
|
+
private shadowCamBufs;
|
|
36
40
|
private shadowPipe;
|
|
37
41
|
private mainPipe;
|
|
38
|
-
private
|
|
42
|
+
private shadowBGs;
|
|
39
43
|
private mainBG;
|
|
40
44
|
private shadowBGL;
|
|
41
45
|
private mainBGL;
|
|
42
46
|
private shadowSampler;
|
|
43
47
|
private count;
|
|
44
48
|
private cam;
|
|
49
|
+
private shadowCam;
|
|
50
|
+
private cascades;
|
|
51
|
+
/** Width of the blend into the next cascade, in uv (0 = hard switch). Small: it costs a 2nd PCF. */
|
|
52
|
+
cascadeBand: number;
|
|
45
53
|
private atlasView;
|
|
46
54
|
private atlasSampler;
|
|
47
55
|
private tileRectBuf;
|
|
@@ -85,6 +93,11 @@ export declare class GpuShadowRenderer {
|
|
|
85
93
|
private makeCullBuf;
|
|
86
94
|
private cullBG;
|
|
87
95
|
private makeCullResources;
|
|
96
|
+
/** (Re)allocate the shadow array + per-cascade uniforms for `n` cascades. Idempotent per count. */
|
|
97
|
+
private ensureCascades;
|
|
98
|
+
/** Allocate the shadow array + per-cascade uniforms. Split from ensureCascades so makeBindGroups
|
|
99
|
+
* can guarantee resources exist without recursing back into it. */
|
|
100
|
+
private allocCascades;
|
|
88
101
|
/** (Re)create the coarse Hi-Z for a `w×h` depth target and rebind the cull groups to it. Idempotent per size. */
|
|
89
102
|
private ensureHiZ;
|
|
90
103
|
private scratch;
|
|
@@ -116,14 +129,25 @@ export declare class GpuShadowRenderer {
|
|
|
116
129
|
private writeCull;
|
|
117
130
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
118
131
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
119
|
-
*
|
|
132
|
+
*
|
|
133
|
+
* CASCADES: this is the 2d-ARRAY view of every cascade layer. GpuPropsRenderer and
|
|
134
|
+
* GpuSkinRenderer sample `texture_depth_2d_array` and run the same tightest-box-containing
|
|
135
|
+
* selection, so a prop or creature resolves against exactly the cascade the terrain beneath it
|
|
136
|
+
* does — pass them the same matrix ARRAY given to `frame`.
|
|
137
|
+
*
|
|
138
|
+
* NOTE: recreated when the cascade COUNT changes, so re-read it after a frame that changes it
|
|
139
|
+
* rather than caching the view across such a change. */
|
|
120
140
|
get shadowMapView(): GPUTextureView;
|
|
141
|
+
/** A plain 2d view of layer 0 — the tightest cascade — for a consumer that still wants one map. */
|
|
142
|
+
get shadowMapLayer0View(): GPUTextureView;
|
|
143
|
+
/** How many cascade layers are currently allocated. */
|
|
144
|
+
get cascadeCount(): number;
|
|
121
145
|
get shadowMapSampler(): GPUSampler;
|
|
122
146
|
get shadowMapSize(): number;
|
|
123
147
|
/** camVP/lightVP: column-major Float32Array(16). sunDir: travel direction. Pass `occlusion` (the depth target +
|
|
124
148
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
125
149
|
* depth each frame and the NEXT frame's cull uses it (hole-free: the first frame + any resize skip occlusion). */
|
|
126
|
-
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, sceneDepth: GPUTextureView, camVP: Float32Array, lightVP: Float32Array, sunDir: [number, number, number], sunCol: [number, number, number], ambient: number, load?: boolean, occlusion?: {
|
|
150
|
+
frame(enc: GPUCommandEncoder, colorView: GPUTextureView, sceneDepth: GPUTextureView, camVP: Float32Array, lightVP: Float32Array | Float32Array[], sunDir: [number, number, number], sunCol: [number, number, number], ambient: number, load?: boolean, occlusion?: {
|
|
127
151
|
depth: GPUTextureView;
|
|
128
152
|
w: number;
|
|
129
153
|
h: number;
|
package/dist/gpuShadow.js
CHANGED
|
@@ -13,6 +13,15 @@
|
|
|
13
13
|
* place ghost, destroy marker, projectiles, and sparkles so effect cubes read as distinct from terrain). */
|
|
14
14
|
import { frustumPlanes } from './mat4';
|
|
15
15
|
const SHADOW_SIZE = 2048;
|
|
16
|
+
// CASCADED SHADOW MAPS. One ortho map has to choose between resolution and reach: AMS ran a single
|
|
17
|
+
// 90-block box against a 280-block draw distance, so everything past it was simply unshadowed and
|
|
18
|
+
// the far terrain read flat. Cascades split the range into nested boxes — tight and crisp near the
|
|
19
|
+
// camera, loose and far behind it — each rendered into its own layer of a depth-2d-array.
|
|
20
|
+
//
|
|
21
|
+
// A caller passing ONE matrix gets exactly the old behaviour, byte for byte: one layer, one cull,
|
|
22
|
+
// one sample, and the selection loop terminates immediately. That keeps the five demo pages and
|
|
23
|
+
// AMS's existing props/doors/skin path unmoved until each opts in.
|
|
24
|
+
const MAX_CASCADES = 4;
|
|
16
25
|
const CULL_WG = 64; // compute-cull workgroup size
|
|
17
26
|
const INST_F = 16; // floats per instance: pos.xyz+hue, scl.xyz+emissive, rot quat, aux (tile,up,_,_)
|
|
18
27
|
const OVERLAY_CAP = 512; // max translucent overlay cubes per frame (the place ghost + headroom)
|
|
@@ -53,12 +62,13 @@ export const CUBE = buildCube();
|
|
|
53
62
|
const COMMON_WGSL = /* wgsl */ `
|
|
54
63
|
struct Cam {
|
|
55
64
|
camVP: mat4x4<f32>,
|
|
56
|
-
lightVP: mat4x4<f32>,
|
|
65
|
+
lightVP: array<mat4x4<f32>, ${MAX_CASCADES}>, // CASCADES, tightest first. A 1-cascade caller uses only [0].
|
|
57
66
|
sun: vec4<f32>, // xyz = direction light travels, w = ambient
|
|
58
67
|
sunCol: vec4<f32>, // xyz = sun colour, w = shadow map size (texels)
|
|
59
68
|
tint: vec4<f32>, // rgb = per-planet tint · w = tint strength (0 = none)
|
|
60
69
|
atlas: vec4<f32>, // x = useAtlas (1/0)
|
|
61
|
-
up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w
|
|
70
|
+
up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w = linear-HDR flag
|
|
71
|
+
csm: vec4<f32>, // x = active cascade count · y = crossfade band in uv units
|
|
62
72
|
};
|
|
63
73
|
struct Inst { pos: vec4<f32>, scl: vec4<f32>, rot: vec4<f32>, aux: vec4<f32> }; // pos.w = hue (<0 ⇒ ground) · scl.w = emissive/alpha · rot = quaternion · aux.x = atlas tile (<0 ⇒ hue) · aux.y = up-face code
|
|
64
74
|
@group(0) @binding(0) var<uniform> cam: Cam;
|
|
@@ -196,13 +206,15 @@ const HIZ_DOWN = 8; // source pixels per Hi-Z texel (coarse ⇒ conservative, ch
|
|
|
196
206
|
const SHADOW_WGSL = COMMON_WGSL +
|
|
197
207
|
/* wgsl */ `
|
|
198
208
|
@group(0) @binding(2) var<storage, read> vis: array<u32>;
|
|
209
|
+
// Always index [0]: each cascade's shadow pass binds its OWN Cam buffer whose lightVP[0] holds that
|
|
210
|
+
// cascade's matrix. One uniform per cascade costs a few hundred bytes and avoids dynamic offsets.
|
|
199
211
|
@vertex fn vs(@builtin(vertex_index) vi: u32, @builtin(instance_index) ii: u32) -> @builtin(position) vec4<f32> {
|
|
200
|
-
return cam.lightVP * vec4(worldOf(insts[vis[ii]], vi), 1.0);
|
|
212
|
+
return cam.lightVP[0] * vec4(worldOf(insts[vis[ii]], vi), 1.0);
|
|
201
213
|
}
|
|
202
214
|
`;
|
|
203
215
|
const MAIN_WGSL = COMMON_WGSL +
|
|
204
216
|
/* wgsl */ `
|
|
205
|
-
@group(0) @binding(2) var shadowMap:
|
|
217
|
+
@group(0) @binding(2) var shadowMap: texture_depth_2d_array;
|
|
206
218
|
@group(0) @binding(3) var shadowSamp: sampler_comparison;
|
|
207
219
|
@group(0) @binding(4) var<storage, read> vis: array<u32>;
|
|
208
220
|
@group(0) @binding(5) var atlasTex: texture_2d<f32>;
|
|
@@ -236,23 +248,56 @@ fn faceVec(code: f32) -> vec3<f32> {
|
|
|
236
248
|
return o;
|
|
237
249
|
}
|
|
238
250
|
|
|
239
|
-
// PCF
|
|
240
|
-
|
|
241
|
-
|
|
251
|
+
// PCF 3x3 against ONE cascade layer. Returns 1 = fully lit, 0 = fully shadowed; the 'inside' out-param
|
|
252
|
+
// reports whether the point actually fell in this cascade's box, so the caller can try the next one.
|
|
253
|
+
fn pcfLayer(world: vec3<f32>, ndl: f32, layer: i32, inside: ptr<function, bool>, edge: ptr<function, f32>) -> f32 {
|
|
254
|
+
let lp = cam.lightVP[layer] * vec4(world, 1.0);
|
|
242
255
|
let proj = lp.xyz / lp.w;
|
|
243
256
|
let uv = proj.xy * vec2(0.5, -0.5) + vec2(0.5, 0.5);
|
|
244
|
-
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
245
|
-
|
|
257
|
+
if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || proj.z > 1.0 || proj.z < 0.0) {
|
|
258
|
+
*inside = false; *edge = 0.0; return 1.0;
|
|
259
|
+
}
|
|
260
|
+
*inside = true;
|
|
261
|
+
// How close to this cascade's border we are, 0 at the middle and 1 at the rim — drives the blend
|
|
262
|
+
// into the next cascade so the resolution change is a gradient rather than a visible seam.
|
|
263
|
+
let m = max(max(abs(uv.x - 0.5), abs(uv.y - 0.5)) * 2.0, abs(proj.z * 2.0 - 1.0));
|
|
264
|
+
*edge = clamp((m - (1.0 - cam.csm.y)) / max(cam.csm.y, 1e-5), 0.0, 1.0);
|
|
265
|
+
// Bias scales with the cascade's world extent: a far cascade covers far more world per texel, so
|
|
266
|
+
// the depth slope across one texel is larger and a near-cascade bias would let acne back in.
|
|
267
|
+
let scale = 1.0 + f32(layer) * 1.5;
|
|
268
|
+
let bias = max(0.0006 * (1.0 - ndl), 0.0004) * scale;
|
|
246
269
|
let texel = 1.0 / cam.sunCol.w;
|
|
247
270
|
var s = 0.0;
|
|
248
271
|
for (var dy = -1; dy <= 1; dy = dy + 1) {
|
|
249
272
|
for (var dx = -1; dx <= 1; dx = dx + 1) {
|
|
250
|
-
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, proj.z - bias);
|
|
273
|
+
s = s + textureSampleCompareLevel(shadowMap, shadowSamp, uv + vec2(f32(dx), f32(dy)) * texel, layer, proj.z - bias);
|
|
251
274
|
}
|
|
252
275
|
}
|
|
253
276
|
return s / 9.0;
|
|
254
277
|
}
|
|
255
278
|
|
|
279
|
+
// Walk the cascades tightest-first and use the first that contains the point. Selection by
|
|
280
|
+
// CONTAINMENT rather than by view depth means no split distances have to be shipped to the shader
|
|
281
|
+
// and kept in sync with the matrices — the boxes themselves are the authority.
|
|
282
|
+
fn shadowFactor(world: vec3<f32>, ndl: f32) -> f32 {
|
|
283
|
+
let n = i32(cam.csm.x);
|
|
284
|
+
var inside = false; var edge = 0.0;
|
|
285
|
+
for (var c = 0; c < n; c = c + 1) {
|
|
286
|
+
let f = pcfLayer(world, ndl, c, &inside, &edge);
|
|
287
|
+
if (inside) {
|
|
288
|
+
// Near this cascade's rim, cross-fade into the next one (if there is a next one) so the
|
|
289
|
+
// resolution step never draws a hard line across the ground.
|
|
290
|
+
if (edge > 0.0 && c + 1 < n) {
|
|
291
|
+
var i2 = false; var e2 = 0.0;
|
|
292
|
+
let g = pcfLayer(world, ndl, c + 1, &i2, &e2);
|
|
293
|
+
if (i2) { return mix(f, g, edge); }
|
|
294
|
+
}
|
|
295
|
+
return f;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return 1.0; // beyond every cascade → unshadowed, same as the old single-map out-of-range case
|
|
299
|
+
}
|
|
300
|
+
|
|
256
301
|
// Per-block edge darkening (baked AO) — mirrors shared/voxelShade.ts blockShade so a flat face shows its
|
|
257
302
|
// individual voxels instead of reading as one uniform slab. fx,fz ∈ [0,1) within the block face.
|
|
258
303
|
fn blockShade(fx: f32, fz: f32) -> f32 {
|
|
@@ -335,18 +380,27 @@ struct AOut { @builtin(position) clip: vec4<f32>, @location(0) hue: f32, @locati
|
|
|
335
380
|
export class GpuShadowRenderer {
|
|
336
381
|
device;
|
|
337
382
|
instBuf;
|
|
383
|
+
sceneReady = false; // instBuf exists — setScene/setCapacity has run at least once
|
|
338
384
|
camBuf;
|
|
339
|
-
shadowTex;
|
|
340
|
-
shadowView;
|
|
385
|
+
shadowTex = null; // null until the first frame picks a cascade count
|
|
386
|
+
shadowView; // layer-0 2d view — the back-compat getter for props/skin
|
|
387
|
+
shadowArrayView; // the 2d-array view the main pass samples
|
|
388
|
+
shadowLayerViews = []; // one render-attachment view per cascade
|
|
389
|
+
shadowCamBufs = [];
|
|
341
390
|
shadowPipe;
|
|
342
391
|
mainPipe;
|
|
343
|
-
|
|
392
|
+
shadowBGs = [];
|
|
344
393
|
mainBG;
|
|
345
394
|
shadowBGL;
|
|
346
395
|
mainBGL;
|
|
347
396
|
shadowSampler;
|
|
348
397
|
count = 0;
|
|
349
|
-
|
|
398
|
+
// camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm
|
|
399
|
+
cam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4);
|
|
400
|
+
shadowCam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4); // scratch for a per-cascade copy
|
|
401
|
+
cascades = 0; // layers currently allocated; 0 until the first frame sizes it
|
|
402
|
+
/** Width of the blend into the next cascade, in uv (0 = hard switch). Small: it costs a 2nd PCF. */
|
|
403
|
+
cascadeBand = 0.12;
|
|
350
404
|
atlasView;
|
|
351
405
|
atlasSampler;
|
|
352
406
|
tileRectBuf;
|
|
@@ -367,10 +421,10 @@ export class GpuShadowRenderer {
|
|
|
367
421
|
camVis;
|
|
368
422
|
camCull;
|
|
369
423
|
camCullBG;
|
|
370
|
-
lightArgs;
|
|
371
|
-
lightVis;
|
|
372
|
-
lightCull;
|
|
373
|
-
lightCullBG;
|
|
424
|
+
lightArgs = [];
|
|
425
|
+
lightVis = [];
|
|
426
|
+
lightCull = [];
|
|
427
|
+
lightCullBG = [];
|
|
374
428
|
statsBuf; // readback: [camVisible, lightVisible] for the IR/tests
|
|
375
429
|
/** Last frame's visible counts (main-pass camera cull, shadow-pass light cull) + total — for IR/telemetry. */
|
|
376
430
|
lastCull = { total: 0, camVisible: 0, lightVisible: 0 };
|
|
@@ -390,12 +444,6 @@ export class GpuShadowRenderer {
|
|
|
390
444
|
size: this.cam.byteLength,
|
|
391
445
|
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
392
446
|
});
|
|
393
|
-
this.shadowTex = device.createTexture({
|
|
394
|
-
size: [SHADOW_SIZE, SHADOW_SIZE],
|
|
395
|
-
format: 'depth32float',
|
|
396
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
397
|
-
});
|
|
398
|
-
this.shadowView = this.shadowTex.createView();
|
|
399
447
|
const shadowMod = device.createShaderModule({ code: SHADOW_WGSL });
|
|
400
448
|
const mainMod = device.createShaderModule({ code: MAIN_WGSL });
|
|
401
449
|
const shadowBGL = device.createBindGroupLayout({
|
|
@@ -442,7 +490,7 @@ export class GpuShadowRenderer {
|
|
|
442
490
|
{
|
|
443
491
|
binding: 2,
|
|
444
492
|
visibility: GPUShaderStage.FRAGMENT,
|
|
445
|
-
texture: { sampleType: 'depth' },
|
|
493
|
+
texture: { sampleType: 'depth', viewDimension: '2d-array' }, // cascades live in array layers
|
|
446
494
|
},
|
|
447
495
|
{
|
|
448
496
|
binding: 3,
|
|
@@ -669,17 +717,65 @@ export class GpuShadowRenderer {
|
|
|
669
717
|
this.camArgs = cam.args;
|
|
670
718
|
this.camVis = cam.vis;
|
|
671
719
|
this.camCull = cam.cull;
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
this.
|
|
720
|
+
// One light frustum per cascade: each box culls independently, so a far cascade still draws the
|
|
721
|
+
// distant occluders a tight near box would have thrown away.
|
|
722
|
+
const n = Math.max(1, this.cascades);
|
|
723
|
+
this.lightArgs = [];
|
|
724
|
+
this.lightVis = [];
|
|
725
|
+
this.lightCull = [];
|
|
726
|
+
for (let i = 0; i < n; i++) {
|
|
727
|
+
const lit = this.makeCullBuf();
|
|
728
|
+
this.lightArgs.push(lit.args);
|
|
729
|
+
this.lightVis.push(lit.vis);
|
|
730
|
+
this.lightCull.push(lit.cull);
|
|
731
|
+
}
|
|
676
732
|
this.camCullBG = this.cullBG(this.camArgs, this.camVis, this.camCull);
|
|
677
|
-
this.lightCullBG = this.
|
|
733
|
+
this.lightCullBG = this.lightArgs.map((a, i) => this.cullBG(a, this.lightVis[i], this.lightCull[i]));
|
|
678
734
|
this.statsBuf = this.device.createBuffer({
|
|
679
735
|
size: 4 * 4 * 2,
|
|
680
736
|
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.MAP_READ,
|
|
681
737
|
});
|
|
682
738
|
}
|
|
739
|
+
/** (Re)allocate the shadow array + per-cascade uniforms for `n` cascades. Idempotent per count. */
|
|
740
|
+
ensureCascades(n) {
|
|
741
|
+
if (this.cascades === n && this.shadowTex)
|
|
742
|
+
return;
|
|
743
|
+
this.allocCascades(n);
|
|
744
|
+
// setScene may not have run yet (no instance buffer to bind), in which case makeBindGroups will
|
|
745
|
+
// pick these up when it does.
|
|
746
|
+
if (this.sceneReady)
|
|
747
|
+
this.makeBindGroups();
|
|
748
|
+
}
|
|
749
|
+
/** Allocate the shadow array + per-cascade uniforms. Split from ensureCascades so makeBindGroups
|
|
750
|
+
* can guarantee resources exist without recursing back into it. */
|
|
751
|
+
allocCascades(n) {
|
|
752
|
+
this.shadowTex?.destroy();
|
|
753
|
+
for (const b of this.shadowCamBufs)
|
|
754
|
+
b.destroy();
|
|
755
|
+
this.cascades = n;
|
|
756
|
+
this.shadowTex = this.device.createTexture({
|
|
757
|
+
size: [SHADOW_SIZE, SHADOW_SIZE, n],
|
|
758
|
+
format: 'depth32float',
|
|
759
|
+
dimension: '2d',
|
|
760
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT | GPUTextureUsage.TEXTURE_BINDING,
|
|
761
|
+
});
|
|
762
|
+
this.shadowArrayView = this.shadowTex.createView({ dimension: '2d-array' });
|
|
763
|
+
this.shadowLayerViews = [];
|
|
764
|
+
for (let i = 0; i < n; i++)
|
|
765
|
+
this.shadowLayerViews.push(this.shadowTex.createView({
|
|
766
|
+
dimension: '2d',
|
|
767
|
+
baseArrayLayer: i,
|
|
768
|
+
arrayLayerCount: 1,
|
|
769
|
+
}));
|
|
770
|
+
// Layer 0 is the tightest cascade — the one a caller that only understands a single map wants.
|
|
771
|
+
this.shadowView = this.shadowLayerViews[0];
|
|
772
|
+
this.shadowCamBufs = [];
|
|
773
|
+
for (let i = 0; i < n; i++)
|
|
774
|
+
this.shadowCamBufs.push(this.device.createBuffer({
|
|
775
|
+
size: this.cam.byteLength,
|
|
776
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
777
|
+
}));
|
|
778
|
+
}
|
|
683
779
|
/** (Re)create the coarse Hi-Z for a `w×h` depth target and rebind the cull groups to it. Idempotent per size. */
|
|
684
780
|
ensureHiZ(w, h) {
|
|
685
781
|
const hw = Math.max(1, Math.ceil(w / HIZ_DOWN)), hh = Math.max(1, Math.ceil(h / HIZ_DOWN));
|
|
@@ -696,7 +792,7 @@ export class GpuShadowRenderer {
|
|
|
696
792
|
this.hizView = this.hizTex.createView();
|
|
697
793
|
this.occView = this.hizView; // the cull now samples the real Hi-Z
|
|
698
794
|
this.camCullBG = this.cullBG(this.camArgs, this.camVis, this.camCull);
|
|
699
|
-
this.lightCullBG = this.
|
|
795
|
+
this.lightCullBG = this.lightArgs.map((a, i) => this.cullBG(a, this.lightVis[i], this.lightCull[i]));
|
|
700
796
|
}
|
|
701
797
|
scratch = new Float32Array(0);
|
|
702
798
|
fill(scratch, instances, n) {
|
|
@@ -721,21 +817,25 @@ export class GpuShadowRenderer {
|
|
|
721
817
|
}
|
|
722
818
|
}
|
|
723
819
|
makeBindGroups() {
|
|
820
|
+
// A caller can setScene before the first frame has said how many cascades it wants; default to
|
|
821
|
+
// one, which is the old single-map behaviour, and frame() will re-allocate if it asks for more.
|
|
822
|
+
if (!this.shadowTex)
|
|
823
|
+
this.allocCascades(1);
|
|
724
824
|
this.makeCullResources(); // (re)create the cull buffers against the fresh instBuf/cap first — the draw passes bind their visible lists
|
|
725
|
-
this.
|
|
825
|
+
this.shadowBGs = this.lightVis.map((vis, i) => this.device.createBindGroup({
|
|
726
826
|
layout: this.shadowBGL,
|
|
727
827
|
entries: [
|
|
728
|
-
{ binding: 0, resource: { buffer: this.
|
|
828
|
+
{ binding: 0, resource: { buffer: this.shadowCamBufs[i] } },
|
|
729
829
|
{ binding: 1, resource: { buffer: this.instBuf } },
|
|
730
|
-
{ binding: 2, resource: { buffer:
|
|
830
|
+
{ binding: 2, resource: { buffer: vis } },
|
|
731
831
|
],
|
|
732
|
-
});
|
|
832
|
+
}));
|
|
733
833
|
this.mainBG = this.device.createBindGroup({
|
|
734
834
|
layout: this.mainBGL,
|
|
735
835
|
entries: [
|
|
736
836
|
{ binding: 0, resource: { buffer: this.camBuf } },
|
|
737
837
|
{ binding: 1, resource: { buffer: this.instBuf } },
|
|
738
|
-
{ binding: 2, resource: this.
|
|
838
|
+
{ binding: 2, resource: this.shadowArrayView },
|
|
739
839
|
{ binding: 3, resource: this.shadowSampler },
|
|
740
840
|
{ binding: 4, resource: { buffer: this.camVis } },
|
|
741
841
|
{ binding: 5, resource: this.atlasView },
|
|
@@ -781,6 +881,7 @@ export class GpuShadowRenderer {
|
|
|
781
881
|
this.cap = this.count;
|
|
782
882
|
this.scratch = new Float32Array(this.count * INST_F);
|
|
783
883
|
this.fill(this.scratch, instances, this.count);
|
|
884
|
+
this.sceneReady = true;
|
|
784
885
|
this.instBuf = this.device.createBuffer({
|
|
785
886
|
size: this.scratch.byteLength,
|
|
786
887
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
@@ -792,6 +893,7 @@ export class GpuShadowRenderer {
|
|
|
792
893
|
setCapacity(cap) {
|
|
793
894
|
this.cap = cap;
|
|
794
895
|
this.scratch = new Float32Array(cap * INST_F);
|
|
896
|
+
this.sceneReady = true;
|
|
795
897
|
this.instBuf = this.device.createBuffer({
|
|
796
898
|
size: this.scratch.byteLength,
|
|
797
899
|
usage: GPUBufferUsage.STORAGE | GPUBufferUsage.COPY_DST,
|
|
@@ -886,10 +988,25 @@ export class GpuShadowRenderer {
|
|
|
886
988
|
}
|
|
887
989
|
/** The sun shadow-map depth view + its comparison sampler + texel size. Feed these into GpuPropsRenderer/
|
|
888
990
|
* GpuSkinRenderer.frame(lightVP, …) so mesh props & creatures RECEIVE the same sun shadow the cubes sample.
|
|
889
|
-
*
|
|
991
|
+
*
|
|
992
|
+
* CASCADES: this is the 2d-ARRAY view of every cascade layer. GpuPropsRenderer and
|
|
993
|
+
* GpuSkinRenderer sample `texture_depth_2d_array` and run the same tightest-box-containing
|
|
994
|
+
* selection, so a prop or creature resolves against exactly the cascade the terrain beneath it
|
|
995
|
+
* does — pass them the same matrix ARRAY given to `frame`.
|
|
996
|
+
*
|
|
997
|
+
* NOTE: recreated when the cascade COUNT changes, so re-read it after a frame that changes it
|
|
998
|
+
* rather than caching the view across such a change. */
|
|
890
999
|
get shadowMapView() {
|
|
1000
|
+
return this.shadowArrayView;
|
|
1001
|
+
}
|
|
1002
|
+
/** A plain 2d view of layer 0 — the tightest cascade — for a consumer that still wants one map. */
|
|
1003
|
+
get shadowMapLayer0View() {
|
|
891
1004
|
return this.shadowView;
|
|
892
1005
|
}
|
|
1006
|
+
/** How many cascade layers are currently allocated. */
|
|
1007
|
+
get cascadeCount() {
|
|
1008
|
+
return this.cascades;
|
|
1009
|
+
}
|
|
893
1010
|
get shadowMapSampler() {
|
|
894
1011
|
return this.shadowSampler;
|
|
895
1012
|
}
|
|
@@ -900,26 +1017,41 @@ export class GpuShadowRenderer {
|
|
|
900
1017
|
* its size) to enable Hi-Z occlusion culling on the camera pass — the renderer rebuilds a coarse Hi-Z from that
|
|
901
1018
|
* depth each frame and the NEXT frame's cull uses it (hole-free: the first frame + any resize skip occlusion). */
|
|
902
1019
|
frame(enc, colorView, sceneDepth, camVP, lightVP, sunDir, sunCol, ambient, load = false, occlusion, tint = [1, 1, 1, 0], extraShadow, up = [0, 1, 0], linearOut = false) {
|
|
1020
|
+
// One matrix or many — a single Float32Array is the 1-cascade case and stays byte-identical.
|
|
1021
|
+
const mats = Array.isArray(lightVP) ? lightVP : [lightVP];
|
|
1022
|
+
const nc = Math.max(1, Math.min(MAX_CASCADES, mats.length));
|
|
1023
|
+
this.ensureCascades(nc);
|
|
1024
|
+
const L = 16; // lightVP[0] starts here
|
|
1025
|
+
const S = 16 + MAX_CASCADES * 16; // ...and the scalars start after the whole array
|
|
903
1026
|
this.cam.set(camVP, 0);
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
this.cam[
|
|
907
|
-
this.cam[
|
|
908
|
-
this.cam[
|
|
909
|
-
this.cam[
|
|
910
|
-
this.cam[
|
|
911
|
-
this.cam[
|
|
912
|
-
this.cam[
|
|
913
|
-
this.cam[
|
|
914
|
-
this.cam[
|
|
915
|
-
this.cam[
|
|
916
|
-
this.cam[
|
|
917
|
-
this.cam[
|
|
918
|
-
this.cam[
|
|
919
|
-
this.cam[
|
|
920
|
-
this.cam[
|
|
921
|
-
this.cam[
|
|
1027
|
+
for (let i = 0; i < nc; i++)
|
|
1028
|
+
this.cam.set(mats[i], L + i * 16);
|
|
1029
|
+
this.cam[S + 0] = sunDir[0];
|
|
1030
|
+
this.cam[S + 1] = sunDir[1];
|
|
1031
|
+
this.cam[S + 2] = sunDir[2];
|
|
1032
|
+
this.cam[S + 3] = ambient;
|
|
1033
|
+
this.cam[S + 4] = sunCol[0];
|
|
1034
|
+
this.cam[S + 5] = sunCol[1];
|
|
1035
|
+
this.cam[S + 6] = sunCol[2];
|
|
1036
|
+
this.cam[S + 7] = SHADOW_SIZE;
|
|
1037
|
+
this.cam[S + 8] = tint[0];
|
|
1038
|
+
this.cam[S + 9] = tint[1];
|
|
1039
|
+
this.cam[S + 10] = tint[2];
|
|
1040
|
+
this.cam[S + 11] = tint[3];
|
|
1041
|
+
this.cam[S + 12] = this.useAtlas;
|
|
1042
|
+
this.cam[S + 16] = up[0];
|
|
1043
|
+
this.cam[S + 17] = up[1];
|
|
1044
|
+
this.cam[S + 18] = up[2];
|
|
1045
|
+
this.cam[S + 19] = linearOut ? 1 : 0; // up.w = linear-HDR output flag
|
|
1046
|
+
this.cam[S + 20] = nc; // csm.x = active cascade count
|
|
1047
|
+
this.cam[S + 21] = nc > 1 ? this.cascadeBand : 0; // csm.y = crossfade band (pointless with one box)
|
|
922
1048
|
this.device.queue.writeBuffer(this.camBuf, 0, this.cam);
|
|
1049
|
+
// Each cascade's shadow pass reads its OWN copy with that cascade's matrix parked at lightVP[0].
|
|
1050
|
+
for (let i = 0; i < nc; i++) {
|
|
1051
|
+
this.shadowCam.set(this.cam);
|
|
1052
|
+
this.shadowCam.set(mats[i], L);
|
|
1053
|
+
this.device.queue.writeBuffer(this.shadowCamBufs[i], 0, this.shadowCam);
|
|
1054
|
+
}
|
|
923
1055
|
if (occlusion)
|
|
924
1056
|
this.ensureHiZ(occlusion.w, occlusion.h);
|
|
925
1057
|
const occludeNow = !!occlusion && this.hizReady; // only once a Hi-Z from a prior frame exists (never on frame 1/resize)
|
|
@@ -928,34 +1060,42 @@ export class GpuShadowRenderer {
|
|
|
928
1060
|
// compacts its survivors into a visible list + counts them in instanceCount, which the matching drawIndirect consumes.
|
|
929
1061
|
const resetArgs = new Uint32Array([36, 0, 0, 0]);
|
|
930
1062
|
this.device.queue.writeBuffer(this.camArgs, 0, resetArgs);
|
|
931
|
-
|
|
1063
|
+
for (const a of this.lightArgs)
|
|
1064
|
+
this.device.queue.writeBuffer(a, 0, resetArgs);
|
|
932
1065
|
this.writeCull(this.camCull, camVP, occludeNow);
|
|
933
|
-
|
|
1066
|
+
for (let i = 0; i < nc; i++)
|
|
1067
|
+
this.writeCull(this.lightCull[i], mats[i], false);
|
|
934
1068
|
if (this.count > 0) {
|
|
935
1069
|
const cp = enc.beginComputePass();
|
|
936
1070
|
cp.setPipeline(this.cullPipe);
|
|
937
1071
|
cp.setBindGroup(0, this.camCullBG);
|
|
938
1072
|
cp.dispatchWorkgroups(Math.ceil(this.count / CULL_WG));
|
|
939
|
-
|
|
940
|
-
|
|
1073
|
+
for (let i = 0; i < nc; i++) {
|
|
1074
|
+
cp.setBindGroup(0, this.lightCullBG[i]);
|
|
1075
|
+
cp.dispatchWorkgroups(Math.ceil(this.count / CULL_WG));
|
|
1076
|
+
}
|
|
941
1077
|
cp.end();
|
|
942
1078
|
}
|
|
943
|
-
// Pass 1: occluder depth from the sun
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
1079
|
+
// Pass 1: occluder depth from the sun — one pass per cascade, into its own array layer.
|
|
1080
|
+
for (let i = 0; i < nc; i++) {
|
|
1081
|
+
const sp = enc.beginRenderPass({
|
|
1082
|
+
colorAttachments: [],
|
|
1083
|
+
depthStencilAttachment: {
|
|
1084
|
+
view: this.shadowLayerViews[i],
|
|
1085
|
+
depthClearValue: 1.0,
|
|
1086
|
+
depthLoadOp: 'clear',
|
|
1087
|
+
depthStoreOp: 'store',
|
|
1088
|
+
},
|
|
1089
|
+
});
|
|
1090
|
+
sp.setPipeline(this.shadowPipe);
|
|
1091
|
+
sp.setBindGroup(0, this.shadowBGs[i]);
|
|
1092
|
+
sp.drawIndirect(this.lightArgs[i], 0);
|
|
1093
|
+
// The app casts its own occluders (mesh props/trees) into EVERY cascade — an occluder missing
|
|
1094
|
+
// from a far cascade would leave a tree lit at distance while its neighbours are shadowed.
|
|
1095
|
+
if (extraShadow)
|
|
1096
|
+
extraShadow(sp, mats[i]);
|
|
1097
|
+
sp.end();
|
|
1098
|
+
}
|
|
959
1099
|
// Pass 2: camera, shadow-sampled (camera-frustum + Hi-Z-occlusion-culled).
|
|
960
1100
|
const mp = enc.beginRenderPass({
|
|
961
1101
|
colorAttachments: [
|
|
@@ -1012,7 +1152,7 @@ export class GpuShadowRenderer {
|
|
|
1012
1152
|
async readCullStats() {
|
|
1013
1153
|
const enc = this.device.createCommandEncoder();
|
|
1014
1154
|
enc.copyBufferToBuffer(this.camArgs, 0, this.statsBuf, 0, 16); // args[1] = instanceCount (the visible count)
|
|
1015
|
-
enc.copyBufferToBuffer(this.lightArgs, 0, this.statsBuf, 16, 16);
|
|
1155
|
+
enc.copyBufferToBuffer(this.lightArgs[0], 0, this.statsBuf, 16, 16);
|
|
1016
1156
|
this.device.queue.submit([enc.finish()]);
|
|
1017
1157
|
await this.statsBuf.mapAsync(GPUMapMode.READ);
|
|
1018
1158
|
const a = new Uint32Array(this.statsBuf.getMappedRange().slice(0));
|
package/dist/gpuShadowMap.js
CHANGED
|
@@ -8,6 +8,12 @@
|
|
|
8
8
|
export const SHADOW_SIZE = 2048;
|
|
9
9
|
// Drop-in WGSL: paste into a receiver fragment shader. Expects `shadowMap` (binding 2) + `shadowSamp`
|
|
10
10
|
// (binding 3) + a `lightVP` mat4 and texel size in scope. Returns 1 = lit, 0 = fully shadowed (PCF 3×3).
|
|
11
|
+
//
|
|
12
|
+
// SINGLE-MAP ONLY. `GpuShadowRenderer` now renders CASCADES into a depth-2d-array and selects the
|
|
13
|
+
// tightest box containing the point (see gpuShadow.ts, and the mirrors in assets/gpuProps.ts and
|
|
14
|
+
// assets/gpuSkin.ts). This helper still takes one `texture_depth_2d`, which is correct for its own
|
|
15
|
+
// consumer — `GpuShadowMap` below is a single 2048² map — but a receiver that wants cascade reach
|
|
16
|
+
// should copy the selection loop from one of those three rather than paste this.
|
|
11
17
|
export const SHADOW_WGSL = /* wgsl */ `
|
|
12
18
|
fn shadowFactor(lightVP: mat4x4<f32>, world: vec3<f32>, ndl: f32, texel: f32,
|
|
13
19
|
shadowMap: texture_depth_2d, shadowSamp: sampler_comparison) -> f32 {
|