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