ugly-game 0.14.13 → 0.14.15
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 +32 -0
- package/dist/gpuShadow.js +149 -6
- package/package.json +1 -1
package/dist/gpuShadow.d.ts
CHANGED
|
@@ -82,6 +82,9 @@ export declare class GpuShadowRenderer {
|
|
|
82
82
|
* softened, and nothing replaced it.
|
|
83
83
|
*/
|
|
84
84
|
private atlasAO;
|
|
85
|
+
private matQuality;
|
|
86
|
+
private matCutoff;
|
|
87
|
+
private eye;
|
|
85
88
|
private alphaPipe;
|
|
86
89
|
private alphaBGL;
|
|
87
90
|
private overlayBuf;
|
|
@@ -143,6 +146,35 @@ export declare class GpuShadowRenderer {
|
|
|
143
146
|
* adjusted. Safe to call every frame — it writes one float into the camera uniform.
|
|
144
147
|
*/
|
|
145
148
|
setAtlasAO(strength: number): void;
|
|
149
|
+
/**
|
|
150
|
+
* MATERIAL QUALITY — how much surface relief textured blocks get.
|
|
151
|
+
*
|
|
152
|
+
* 0 flat. Bit-for-bit the look before this existed, and the DEFAULT, so a caller that never
|
|
153
|
+
* calls this is unchanged.
|
|
154
|
+
* 1 relief lighting: the tile's own luminance becomes a height field, the shading normal is
|
|
155
|
+
* tilted by its slope, and a roughness-driven specular lobe rides on top.
|
|
156
|
+
* 2 …plus parallax occlusion, so a surface has real depth at grazing angles.
|
|
157
|
+
*
|
|
158
|
+
* A DIAL RATHER THAN A BUILD FLAG on purpose: this is the term a caller wants to give up first
|
|
159
|
+
* when frames get expensive, and a renderer that made you rebuild to do it is one nobody would.
|
|
160
|
+
* Safe to call every frame — it writes one float into the camera uniform.
|
|
161
|
+
*
|
|
162
|
+
* `cutoff` is the world-space distance past which parallax stops. Parallax is a per-pixel ray
|
|
163
|
+
* march and is sub-pixel on a distant block, so paying for it there is waste; the step count also
|
|
164
|
+
* fades toward the cutoff rather than switching off, which is what keeps the LOD change invisible.
|
|
165
|
+
*/
|
|
166
|
+
setMaterialQuality(quality: 0 | 1 | 2, cutoff?: number): void;
|
|
167
|
+
/**
|
|
168
|
+
* The camera position in WORLD space.
|
|
169
|
+
*
|
|
170
|
+
* Needed by the material path (the parallax view ray and the specular half-vector) and by nothing
|
|
171
|
+
* else, which is why it is a setter rather than another positional argument on `frame()`. Leaving
|
|
172
|
+
* it unset is harmless while material quality is 0: every consumer is gated behind that dial.
|
|
173
|
+
*
|
|
174
|
+
* ⚠️ Under a FLOATING ORIGIN, pass the REBASED eye — the same frame the instances are in — or the
|
|
175
|
+
* highlight will sit somewhere else entirely.
|
|
176
|
+
*/
|
|
177
|
+
setEye(x: number, y: number, z: number): void;
|
|
146
178
|
setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
|
|
147
179
|
/** Static scene: sizes the buffer to exactly these instances. */
|
|
148
180
|
setScene(instances: CubeInstance[]): void;
|
package/dist/gpuShadow.js
CHANGED
|
@@ -67,9 +67,10 @@ struct Cam {
|
|
|
67
67
|
sun: vec4<f32>, // xyz = direction light travels, w = ambient
|
|
68
68
|
sunCol: vec4<f32>, // xyz = sun colour, w = shadow map size (texels)
|
|
69
69
|
tint: vec4<f32>, // rgb = per-planet tint · w = tint strength (0 = none)
|
|
70
|
-
atlas: vec4<f32>, // x = useAtlas (1/0) · y =
|
|
70
|
+
atlas: vec4<f32>, // x = useAtlas (1/0) · y = per-voxel AO surviving on textured blocks · z = MATERIAL quality (0 flat · 1 normal-mapped · 2 + parallax) · w = parallax cutoff distance in world units
|
|
71
71
|
up: vec4<f32>, // xyz = local gravity up (for hemisphere face-shading readability); w = linear-HDR flag
|
|
72
72
|
csm: vec4<f32>, // x = active cascade count · y = crossfade band in uv units
|
|
73
|
+
eye: vec4<f32>, // xyz = camera position in world space (material relief + specular); w unused
|
|
73
74
|
};
|
|
74
75
|
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 · aux.z/aux.w = CONTACT DECAL (0,0 = none; see setAtlasAO/CubeInstance.decal*)
|
|
75
76
|
@group(0) @binding(0) var<uniform> cam: Cam;
|
|
@@ -351,6 +352,10 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
|
|
|
351
352
|
else if (an.x >= an.z) { fx = fract(i.blk.y); fz = fract(i.blk.z); bx = i.blk.y; bz = i.blk.z; dfx = vec2(dblkx.y, dblky.y); dfz = vec2(dblkx.z, dblky.z); }
|
|
352
353
|
else { fx = fract(i.blk.x); fz = fract(i.blk.y); bx = i.blk.x; bz = i.blk.y; dfx = vec2(dblkx.x, dblky.x); dfz = vec2(dblkx.y, dblky.y); }
|
|
353
354
|
var base = select(mix(vec3(0.34), hsv(i.hue), 0.62), vec3(0.6, 0.62, 0.66), i.hue < 0.0);
|
|
355
|
+
// Surface relief and roughness, filled in by the ATLAS branch when material quality is on. The zero/one
|
|
356
|
+
// defaults are the flat look exactly: no normal tilt, fully rough (no specular lobe).
|
|
357
|
+
var matN = vec2(0.0, 0.0);
|
|
358
|
+
var matRough = 1.0;
|
|
354
359
|
if (cam.atlas.x > 0.5 && i.tile >= 0.0) { // ATLAS: pick top/side/bottom LAYER from the face vs the up axis
|
|
355
360
|
let d = dot(i.objN, faceVec(i.upc));
|
|
356
361
|
var variant = 1u; // side
|
|
@@ -359,10 +364,74 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
|
|
|
359
364
|
// No UV inset. The old 4% inset existed because a GRID atlas bleeds its neighbours in at every
|
|
360
365
|
// block boundary under linear filtering, drawing a seam grid. Array layers have no neighbours to
|
|
361
366
|
// bleed from, so each tile gets its full 0..1 range back — 8% of every texture that was discarded.
|
|
362
|
-
|
|
367
|
+
var uv = fract(vec2(fx, fz));
|
|
368
|
+
let gx = vec2(dfx.x, dfz.x);
|
|
369
|
+
let gy = vec2(dfx.y, dfz.y);
|
|
370
|
+
// ══ MATERIAL ══════════════════════════════════════════════════════════════════════════════
|
|
371
|
+
// "Blocks look like flat boxes." They were: one colour fetch, no surface relief, and a purely
|
|
372
|
+
// diffuse response, so a stone face and an ice face differed only in hue.
|
|
373
|
+
//
|
|
374
|
+
// THE HEIGHT FIELD IS THE TEXTURE'S OWN LUMINANCE. No new art, no second atlas, no cook-step
|
|
375
|
+
// change — mortar lines, grain and crystal facets are already darker than what surrounds them,
|
|
376
|
+
// which is exactly the signal a height map carries. It is an approximation and it is the right
|
|
377
|
+
// one here: the alternative is authoring a height map for every tile in the game.
|
|
378
|
+
//
|
|
379
|
+
// QUALITY IS A DIAL, NOT A BUILD FLAG (cam.atlas.z). 0 reproduces the old look bit for bit, so
|
|
380
|
+
// a caller that never calls setMaterialQuality is unchanged. 1 adds relief lighting. 2 adds
|
|
381
|
+
// parallax. The caller drops the dial when frames get expensive; see setMaterialQuality.
|
|
382
|
+
let mq = cam.atlas.z;
|
|
383
|
+
// …AND IT SCALES WITH DISTANCE. Parallax is a per-pixel ray march, so paying for it on a block
|
|
384
|
+
// forty metres away — where it is sub-pixel — is pure waste. cam.atlas.w is where it stops.
|
|
385
|
+
var near = 0.0;
|
|
386
|
+
if (mq >= 1.5) {
|
|
387
|
+
near = 1.0 - smoothstep(cam.atlas.w * 0.6, cam.atlas.w, length(i.world - cam.eye.xyz));
|
|
388
|
+
}
|
|
389
|
+
if (near > 0.01) {
|
|
390
|
+
// PARALLAX OCCLUSION, in the face's own tangent frame. fx/fz ARE the tangent coordinates —
|
|
391
|
+
// that is the whole reason this is affordable on a voxel: every surface is axis-aligned, so
|
|
392
|
+
// the tangent basis is the two in-face axes and needs no per-vertex tangent at all.
|
|
393
|
+
let vDir = normalize(cam.eye.xyz - i.world);
|
|
394
|
+
// The view vector projected into the face plane, in (fx, fz) units.
|
|
395
|
+
var tA: vec3<f32>; var tB: vec3<f32>;
|
|
396
|
+
if (an.y >= an.x && an.y >= an.z) { tA = vec3(1.,0.,0.); tB = vec3(0.,0.,1.); }
|
|
397
|
+
else if (an.x >= an.z) { tA = vec3(0.,1.,0.); tB = vec3(0.,0.,1.); }
|
|
398
|
+
else { tA = vec3(1.,0.,0.); tB = vec3(0.,1.,0.); }
|
|
399
|
+
let vT = vec2(dot(vDir, tA), dot(vDir, tB));
|
|
400
|
+
let vN = max(abs(dot(vDir, i.objN)), 0.15);
|
|
401
|
+
// STEPS SCALE WITH GRAZING ANGLE. Head-on there is nothing to occlude and 4 steps is plenty;
|
|
402
|
+
// at a grazing angle the ray crosses many texels and needs more. This is the difference
|
|
403
|
+
// between parallax costing ~4 fetches and ~24 on the same frame.
|
|
404
|
+
let steps = i32(mix(4.0, 20.0, 1.0 - vN) * near) + 1;
|
|
405
|
+
let DEPTH = 0.055; // metres of apparent relief — a voxel is 1 m
|
|
406
|
+
let delta = (vT / vN) * DEPTH / f32(steps);
|
|
407
|
+
var h = 1.0;
|
|
408
|
+
var cur = uv;
|
|
409
|
+
let dh = 1.0 / f32(steps);
|
|
410
|
+
for (var st = 0; st < steps; st = st + 1) {
|
|
411
|
+
let sampleH = dot(textureSampleGrad(atlasTex, atlasSamp, fract(cur), layer, gx, gy).rgb, vec3(0.299, 0.587, 0.114));
|
|
412
|
+
if (sampleH >= h) { break; }
|
|
413
|
+
h = h - dh;
|
|
414
|
+
cur = cur - delta;
|
|
415
|
+
}
|
|
416
|
+
uv = fract(mix(uv, cur, near));
|
|
417
|
+
}
|
|
363
418
|
// Gradients, not LOD 0: this is what actually reads the mip chain.
|
|
364
|
-
let texc = textureSampleGrad(atlasTex, atlasSamp, uv, layer,
|
|
419
|
+
let texc = textureSampleGrad(atlasTex, atlasSamp, uv, layer, gx, gy).rgb;
|
|
365
420
|
base = mix(texc, texc * cam.tint.rgb, cam.tint.w); // per-planet tint
|
|
421
|
+
if (mq >= 0.5) {
|
|
422
|
+
// RELIEF NORMAL from the luminance slope. Two extra fetches, always — this is the term that
|
|
423
|
+
// does most of the work, and it is what stops a wall reading as a printed photograph.
|
|
424
|
+
let e = 0.5 / 64.0; // half a texel at the atlas's working resolution
|
|
425
|
+
let lC = dot(texc, vec3(0.299, 0.587, 0.114));
|
|
426
|
+
let lU = dot(textureSampleGrad(atlasTex, atlasSamp, fract(uv + vec2(e, 0.0)), layer, gx, gy).rgb, vec3(0.299, 0.587, 0.114));
|
|
427
|
+
let lV = dot(textureSampleGrad(atlasTex, atlasSamp, fract(uv + vec2(0.0, e)), layer, gx, gy).rgb, vec3(0.299, 0.587, 0.114));
|
|
428
|
+
matN = vec2((lC - lU), (lC - lV)) * 2.2;
|
|
429
|
+
// ROUGHNESS FROM THE TILE ITSELF. A bright, low-contrast tile (ice, metal, polished stone)
|
|
430
|
+
// reads smooth; a dark, busy one (soil, bark, rubble) reads rough. Derived rather than
|
|
431
|
+
// tabulated so a new block gets a sensible response the day it is added, with no table to
|
|
432
|
+
// forget to update — the failure mode a per-material table has every time.
|
|
433
|
+
matRough = clamp(0.35 + 1.6 * abs(lU - lV) + 0.5 * (1.0 - lC), 0.18, 1.0);
|
|
434
|
+
}
|
|
366
435
|
}
|
|
367
436
|
// ── PROP CONTACT DECAL ────────────────────────────────────────────────────────────────────────
|
|
368
437
|
// A plant on a 1 m cube reads as PASTED ON because the block under it is untouched: the same flat
|
|
@@ -417,11 +486,41 @@ fn fixedFill(N: vec3<f32>, up: vec3<f32>) -> f32 {
|
|
|
417
486
|
// cam.sun.w is the fill/ambient brightness knob; the fill's directional term (0.5..1.35) keeps every face's
|
|
418
487
|
// orientation legible day or night, while the sun adds bright, shadow-casting key light on top during the day.
|
|
419
488
|
let fill = cam.sun.w * (0.5 + 0.85 * fixedFill(N, cam.up.xyz));
|
|
420
|
-
|
|
489
|
+
// RELIEF LIGHTING. Perturb the shading normal by the height slope, in the same two in-face axes the atlas is
|
|
490
|
+
// sampled along, then re-light with it. ndl above stays the SHADOW-side term (it decides occlusion and must
|
|
491
|
+
// keep using the geometric normal, or a block would shadow itself from its own texture grain).
|
|
492
|
+
var Nm = N;
|
|
493
|
+
var ndlM = ndl;
|
|
494
|
+
if (cam.atlas.z >= 0.5) {
|
|
495
|
+
var tA2: vec3<f32>; var tB2: vec3<f32>;
|
|
496
|
+
if (an.y >= an.x && an.y >= an.z) { tA2 = vec3(1.,0.,0.); tB2 = vec3(0.,0.,1.); }
|
|
497
|
+
else if (an.x >= an.z) { tA2 = vec3(0.,1.,0.); tB2 = vec3(0.,0.,1.); }
|
|
498
|
+
else { tA2 = vec3(1.,0.,0.); tB2 = vec3(0.,1.,0.); }
|
|
499
|
+
Nm = normalize(N + (tA2 * matN.x + tB2 * matN.y));
|
|
500
|
+
ndlM = max(dot(Nm, -cam.sun.xyz), 0.0);
|
|
501
|
+
}
|
|
502
|
+
// SPECULAR — Blinn-Phong off the relief normal, shadowed with the same term as the key so a block in shade
|
|
503
|
+
// does not glint. The exponent comes from the derived roughness, so ice and polished stone catch a tight
|
|
504
|
+
// highlight while soil and bark stay matte. Bounded at the top: an unclamped lobe on a bright tile blows out
|
|
505
|
+
// to white and reads as plastic, which is the opposite of the report this answers.
|
|
506
|
+
// BRANCHED, not multiplied by zero. A pow() costs the same whether its result is scaled to nothing or not, and
|
|
507
|
+
// this is the default path for every caller that has not opted in — so quality 0 must be free, not merely
|
|
508
|
+
// invisible. The branch is uniform across the draw (it reads a uniform), so it costs no divergence.
|
|
509
|
+
var spec = vec3(0.0);
|
|
510
|
+
var keyN = ndl;
|
|
511
|
+
if (cam.atlas.z >= 0.5) {
|
|
512
|
+
keyN = ndlM;
|
|
513
|
+
let vE = normalize(cam.eye.xyz - i.world);
|
|
514
|
+
let hV = normalize(vE - cam.sun.xyz);
|
|
515
|
+
let specP = mix(96.0, 6.0, matRough);
|
|
516
|
+
let specK = mix(0.30, 0.02, matRough);
|
|
517
|
+
spec = cam.sunCol.xyz * pow(max(dot(Nm, hV), 0.0), specP) * specK * sh * step(0.01, ndl);
|
|
518
|
+
}
|
|
519
|
+
let key = cam.sunCol.xyz * keyN * sh; // direct sun (shadowed) — primary light
|
|
421
520
|
// INDIRECT: baked one-bounce irradiance, zero unless setGI supplied probes — additive over the
|
|
422
521
|
// existing look rather than a replacement for the ambient knob.
|
|
423
522
|
let gi = giAt(i.world, N);
|
|
424
|
-
let lit = base * (fill + key + gi) * ao;
|
|
523
|
+
let lit = base * (fill + key + gi) * ao + spec;
|
|
425
524
|
// cam.up.w > 0.5 selects LINEAR-HDR output (unclamped, no gamma) for an HDR pipeline that tonemaps downstream;
|
|
426
525
|
// otherwise the classic gamma-encoded LDR path (all existing consumers). Keeps both looks from one shader.
|
|
427
526
|
if (cam.up.w > 0.5) { return vec4(max(lit, vec3(0.0)), 1.0); }
|
|
@@ -467,7 +566,7 @@ export class GpuShadowRenderer {
|
|
|
467
566
|
giProbeBuf;
|
|
468
567
|
count = 0;
|
|
469
568
|
// camVP(16) + lightVP[MAX_CASCADES](16 each) + sun + sunCol + tint + atlas + up + csm
|
|
470
|
-
cam = new Float32Array(16 + MAX_CASCADES * 16 +
|
|
569
|
+
cam = new Float32Array(16 + MAX_CASCADES * 16 + 7 * 4);
|
|
471
570
|
shadowCam = new Float32Array(16 + MAX_CASCADES * 16 + 6 * 4); // scratch for a per-cascade copy
|
|
472
571
|
cascades = 0; // layers currently allocated; 0 until the first frame sizes it
|
|
473
572
|
/** Width of the blend into the next cascade, in uv (0 = hard switch). Small: it costs a 2nd PCF. */
|
|
@@ -485,6 +584,9 @@ export class GpuShadowRenderer {
|
|
|
485
584
|
* softened, and nothing replaced it.
|
|
486
585
|
*/
|
|
487
586
|
atlasAO = 0;
|
|
587
|
+
matQuality = 0; // see setMaterialQuality — 0 keeps the pre-material look exactly
|
|
588
|
+
matCutoff = 28; // world units; parallax fades out before here
|
|
589
|
+
eye = new Float32Array(3);
|
|
488
590
|
alphaPipe;
|
|
489
591
|
alphaBGL; // kept so the overlay buffer can grow and rebind on demand
|
|
490
592
|
overlayBuf;
|
|
@@ -965,6 +1067,42 @@ export class GpuShadowRenderer {
|
|
|
965
1067
|
setAtlasAO(strength) {
|
|
966
1068
|
this.atlasAO = Math.max(0, Math.min(1, strength));
|
|
967
1069
|
}
|
|
1070
|
+
/**
|
|
1071
|
+
* MATERIAL QUALITY — how much surface relief textured blocks get.
|
|
1072
|
+
*
|
|
1073
|
+
* 0 flat. Bit-for-bit the look before this existed, and the DEFAULT, so a caller that never
|
|
1074
|
+
* calls this is unchanged.
|
|
1075
|
+
* 1 relief lighting: the tile's own luminance becomes a height field, the shading normal is
|
|
1076
|
+
* tilted by its slope, and a roughness-driven specular lobe rides on top.
|
|
1077
|
+
* 2 …plus parallax occlusion, so a surface has real depth at grazing angles.
|
|
1078
|
+
*
|
|
1079
|
+
* A DIAL RATHER THAN A BUILD FLAG on purpose: this is the term a caller wants to give up first
|
|
1080
|
+
* when frames get expensive, and a renderer that made you rebuild to do it is one nobody would.
|
|
1081
|
+
* Safe to call every frame — it writes one float into the camera uniform.
|
|
1082
|
+
*
|
|
1083
|
+
* `cutoff` is the world-space distance past which parallax stops. Parallax is a per-pixel ray
|
|
1084
|
+
* march and is sub-pixel on a distant block, so paying for it there is waste; the step count also
|
|
1085
|
+
* fades toward the cutoff rather than switching off, which is what keeps the LOD change invisible.
|
|
1086
|
+
*/
|
|
1087
|
+
setMaterialQuality(quality, cutoff = 28) {
|
|
1088
|
+
this.matQuality = quality;
|
|
1089
|
+
this.matCutoff = Math.max(1, cutoff);
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
* The camera position in WORLD space.
|
|
1093
|
+
*
|
|
1094
|
+
* Needed by the material path (the parallax view ray and the specular half-vector) and by nothing
|
|
1095
|
+
* else, which is why it is a setter rather than another positional argument on `frame()`. Leaving
|
|
1096
|
+
* it unset is harmless while material quality is 0: every consumer is gated behind that dial.
|
|
1097
|
+
*
|
|
1098
|
+
* ⚠️ Under a FLOATING ORIGIN, pass the REBASED eye — the same frame the instances are in — or the
|
|
1099
|
+
* highlight will sit somewhere else entirely.
|
|
1100
|
+
*/
|
|
1101
|
+
setEye(x, y, z) {
|
|
1102
|
+
this.eye[0] = x;
|
|
1103
|
+
this.eye[1] = y;
|
|
1104
|
+
this.eye[2] = z;
|
|
1105
|
+
}
|
|
968
1106
|
setAtlas(tex, layout) {
|
|
969
1107
|
this.atlasView = tex.createView({ dimension: '2d-array' });
|
|
970
1108
|
const ids = Object.keys(layout.tiles).map(Number);
|
|
@@ -1182,12 +1320,17 @@ export class GpuShadowRenderer {
|
|
|
1182
1320
|
this.cam[S + 11] = tint[3];
|
|
1183
1321
|
this.cam[S + 12] = this.useAtlas;
|
|
1184
1322
|
this.cam[S + 13] = this.atlasAO;
|
|
1323
|
+
this.cam[S + 14] = this.matQuality; // atlas.z — 0 flat · 1 relief · 2 + parallax
|
|
1324
|
+
this.cam[S + 15] = this.matCutoff; // atlas.w — where parallax stops paying for itself
|
|
1185
1325
|
this.cam[S + 16] = up[0];
|
|
1186
1326
|
this.cam[S + 17] = up[1];
|
|
1187
1327
|
this.cam[S + 18] = up[2];
|
|
1188
1328
|
this.cam[S + 19] = linearOut ? 1 : 0; // up.w = linear-HDR output flag
|
|
1189
1329
|
this.cam[S + 20] = nc; // csm.x = active cascade count
|
|
1190
1330
|
this.cam[S + 21] = nc > 1 ? this.cascadeBand : 0; // csm.y = crossfade band (pointless with one box)
|
|
1331
|
+
this.cam[S + 24] = this.eye[0];
|
|
1332
|
+
this.cam[S + 25] = this.eye[1];
|
|
1333
|
+
this.cam[S + 26] = this.eye[2];
|
|
1191
1334
|
this.device.queue.writeBuffer(this.camBuf, 0, this.cam);
|
|
1192
1335
|
// Each cascade's shadow pass reads its OWN copy with that cascade's matrix parked at lightVP[0].
|
|
1193
1336
|
for (let i = 0; i < nc; i++) {
|