ugly-game 0.15.31 → 0.15.33

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.
@@ -1,3 +1,7 @@
1
+ /** One cube instance: world position, per-axis scale, a hue (< 0 ⇒ neutral ground), and an optional
2
+ * emissive flag (> 0 ⇒ the cube self-glows at full brightness, bypassing sun/shadow/AO — used for the
3
+ * place ghost, destroy marker, projectiles, and sparkles so effect cubes read as distinct from terrain). */
4
+ import { type Plane } from './mat4';
1
5
  import { type ProbeGrid } from './giProbes';
2
6
  export interface CubeInstance {
3
7
  x: number;
@@ -560,6 +564,59 @@ export declare const FACE_DIRS: readonly (readonly [number, number, number])[];
560
564
  * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
561
565
  * the buffer at all.
562
566
  */
567
+ /**
568
+ * THE VOLUME A DIRECTIONAL LIGHT'S SHADOW PASS SHOULD CULL AGAINST — which is NOT the box it renders
569
+ * with, and conflating the two is what makes the sun draw the whole world.
570
+ *
571
+ * A shadow caster only matters if it can darken a pixel the camera can actually see. That set is the
572
+ * camera frustum SWEPT TOWARD THE LIGHT: every point which, followed along the light's travel
573
+ * direction, eventually lands inside the camera's view. Everything else casts onto geometry nobody is
574
+ * looking at.
575
+ *
576
+ * The ortho box the map is rendered through cannot serve as that test. It is fitted to the SPHERE
577
+ * enclosing the camera frustum — deliberately, because a sphere is rotation invariant and that is
578
+ * what stops the shadow map shimmering as you turn — and a sphere's bounding cube is many times the
579
+ * frustum's own volume. Measured in all-mine-sky at N=512: a 0.32 G-unit³ frustum inside a 7.28 G
580
+ * unit³ cube, 22.7x, on a world only 512 units across. Every resident instance was inside it from
581
+ * every camera angle, and `lightVisible` sat at 2.09 M against the camera's 348 K.
582
+ *
583
+ * ⚠️ THE RULE IS EXACT, NOT AN APPROXIMATION OF ONE. Sweeping a convex volume along a direction `d`
584
+ * turns each bounding half-space `n·p + w >= 0` into either itself or the whole of space: substituting
585
+ * `p = r - t·d` gives `n·r + w >= t(n·d)`, and if `n·d <= 0` some `t >= 0` always satisfies it. So a
586
+ * plane survives the sweep exactly when its inward normal points along the sweep, and the others stop
587
+ * constraining anything. The intersection of the survivors contains the swept hull, so the test is
588
+ * conservative — it can keep a caster that does not matter, never drop one that does.
589
+ *
590
+ * Sanity, with the sun overhead: the frustum's BOTTOM plane is kept and its TOP plane is dropped.
591
+ * Geometry above you casts down into view; geometry below you cannot.
592
+ *
593
+ * ⚠️ WHAT THIS DOES NOT COVER: a receiver outside the camera frustum whose shadow is still seen —
594
+ * through a mirror, or a reflection pass that samples the same map. Those need the reflected frustum
595
+ * swept in as well, or this pass left uncalled for them.
596
+ *
597
+ * @param camVP the CAMERA's view-projection, not the light's
598
+ * @param sunTravel the direction light TRAVELS (from the sun outward), matching `frame`'s `sunDir`
599
+ */
600
+ export declare function casterPlanes(camVP: ArrayLike<number>, sunTravel: readonly [number, number, number],
601
+ /**
602
+ * Restrict the swept volume to ONE CASCADE'S DEPTH SLICE of the camera frustum.
603
+ *
604
+ * ⚠️ WITHOUT THIS EVERY CASCADE DRAWS THE WHOLE CASTER SET. Measured at N=512 with three cascades:
605
+ * cascade 0 covers camera depth [0, 81] and submitted 348 335 instances — the entire camera-visible
606
+ * set rather than the few in its own slice — because all three were culled against the same
607
+ * whole-frustum volume. Not wrong, since a cascade's own projection clips the remainder, but three
608
+ * times the vertex work it needs, and the reason a cascade still cost 10.5 ms instead of nearly
609
+ * nothing once caster culling was on.
610
+ *
611
+ * ⚠️ `eye` MUST BE IN THE SAME SPACE AS `camVP` — the floating-origin-rebased eye, not the world
612
+ * one. A mismatch shifts both split planes by the sector offset, which on a planet is zero and in
613
+ * deep space is 1024 units, so it would pass every test near the origin.
614
+ */
615
+ slice?: {
616
+ eye: readonly [number, number, number] | Float32Array;
617
+ near: number;
618
+ far: number;
619
+ }): Plane[];
563
620
  export declare const FACE_MASK_ALL = 63;
564
621
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
565
622
  export declare function faceExposed(faceMask: number, d: number): boolean;
@@ -678,6 +735,7 @@ export declare class GpuShadowRenderer {
678
735
  private eye;
679
736
  private bevel;
680
737
  private faceCull;
738
+ private casterCull;
681
739
  private fillet;
682
740
  private alphaPipe;
683
741
  private alphaBGL;
@@ -840,6 +898,17 @@ export declare class GpuShadowRenderer {
840
898
  * ⚠️ AND IT IS IGNORED WHILE A BEVEL IS SET. See `frame()` — the two are mutually exclusive.
841
899
  */
842
900
  setFaceCull(on: boolean): void;
901
+ /**
902
+ * Cull shadow casters against the camera frustum swept toward the sun, instead of against the
903
+ * cascade's own ortho box. See `casterPlanes` for why those are different volumes.
904
+ *
905
+ * ⚠️ IT CHANGES NO PIXEL, WHICH IS ALSO WHY IT NEEDS TO BE A TOGGLE. A change that is supposed to
906
+ * be invisible cannot be verified by looking at it — the A/B is `readCullStats().lightVisible` and
907
+ * the frame time, and both need the OFF case measured on the same machine in the same session.
908
+ * Off by default: a caller whose shadows are sampled by something other than the camera (a mirror,
909
+ * a reflection pass) must opt in knowingly.
910
+ */
911
+ setShadowCasterCull(on: boolean): void;
843
912
  setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
844
913
  /** Static scene: sizes the buffer to exactly these instances. */
845
914
  setScene(instances: CubeInstance[]): void;
@@ -1023,7 +1092,20 @@ export declare class GpuShadowRenderer {
1023
1092
  * dispatch cannot be inserted, and by then the cascade passes are already being encoded — so an
1024
1093
  * occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
1025
1094
  */
1026
- extraShadowCull?: (cp: GPUComputePassEncoder, lightVPs: Float32Array[]) => void, up?: [number, number, number], linearOut?: boolean): void;
1095
+ extraShadowCull?: (cp: GPUComputePassEncoder, lightVPs: Float32Array[]) => void, up?: [number, number, number], linearOut?: boolean,
1096
+ /**
1097
+ * Per-cascade camera-depth ranges, in the same order as `lightVP`.
1098
+ *
1099
+ * Only consulted when shadow caster culling is on, and only to narrow each cascade's caster
1100
+ * volume to its own slice — the cascade still RENDERS through `lightVP[i]` exactly as before.
1101
+ * Omit it and every cascade culls against the whole frustum swept toward the sun, which is
1102
+ * correct and was the first implementation; supplying it stops the cascades duplicating each
1103
+ * other's work. The caller has these already: they are `fitShadowCascades`' own `near`/`far`.
1104
+ */
1105
+ cascadeSplits?: {
1106
+ near: number;
1107
+ far: number;
1108
+ }[]): void;
1027
1109
  /** Read back the last frame's GPU cull result (camera-visible, light-visible). Does its OWN copy+submit so it's
1028
1110
  * safe to call from a live render loop (no per-frame readback cost, no race with the ongoing frames). IR/tests. */
1029
1111
  readCullStats(): Promise<{
package/dist/gpuShadow.js CHANGED
@@ -1111,6 +1111,85 @@ export const FACE_DIRS = [
1111
1111
  * never needs expressing — a cube with no visible face is dropped by the mesher and never reaches
1112
1112
  * the buffer at all.
1113
1113
  */
1114
+ /**
1115
+ * THE VOLUME A DIRECTIONAL LIGHT'S SHADOW PASS SHOULD CULL AGAINST — which is NOT the box it renders
1116
+ * with, and conflating the two is what makes the sun draw the whole world.
1117
+ *
1118
+ * A shadow caster only matters if it can darken a pixel the camera can actually see. That set is the
1119
+ * camera frustum SWEPT TOWARD THE LIGHT: every point which, followed along the light's travel
1120
+ * direction, eventually lands inside the camera's view. Everything else casts onto geometry nobody is
1121
+ * looking at.
1122
+ *
1123
+ * The ortho box the map is rendered through cannot serve as that test. It is fitted to the SPHERE
1124
+ * enclosing the camera frustum — deliberately, because a sphere is rotation invariant and that is
1125
+ * what stops the shadow map shimmering as you turn — and a sphere's bounding cube is many times the
1126
+ * frustum's own volume. Measured in all-mine-sky at N=512: a 0.32 G-unit³ frustum inside a 7.28 G
1127
+ * unit³ cube, 22.7x, on a world only 512 units across. Every resident instance was inside it from
1128
+ * every camera angle, and `lightVisible` sat at 2.09 M against the camera's 348 K.
1129
+ *
1130
+ * ⚠️ THE RULE IS EXACT, NOT AN APPROXIMATION OF ONE. Sweeping a convex volume along a direction `d`
1131
+ * turns each bounding half-space `n·p + w >= 0` into either itself or the whole of space: substituting
1132
+ * `p = r - t·d` gives `n·r + w >= t(n·d)`, and if `n·d <= 0` some `t >= 0` always satisfies it. So a
1133
+ * plane survives the sweep exactly when its inward normal points along the sweep, and the others stop
1134
+ * constraining anything. The intersection of the survivors contains the swept hull, so the test is
1135
+ * conservative — it can keep a caster that does not matter, never drop one that does.
1136
+ *
1137
+ * Sanity, with the sun overhead: the frustum's BOTTOM plane is kept and its TOP plane is dropped.
1138
+ * Geometry above you casts down into view; geometry below you cannot.
1139
+ *
1140
+ * ⚠️ WHAT THIS DOES NOT COVER: a receiver outside the camera frustum whose shadow is still seen —
1141
+ * through a mirror, or a reflection pass that samples the same map. Those need the reflected frustum
1142
+ * swept in as well, or this pass left uncalled for them.
1143
+ *
1144
+ * @param camVP the CAMERA's view-projection, not the light's
1145
+ * @param sunTravel the direction light TRAVELS (from the sun outward), matching `frame`'s `sunDir`
1146
+ */
1147
+ export function casterPlanes(camVP, sunTravel,
1148
+ /**
1149
+ * Restrict the swept volume to ONE CASCADE'S DEPTH SLICE of the camera frustum.
1150
+ *
1151
+ * ⚠️ WITHOUT THIS EVERY CASCADE DRAWS THE WHOLE CASTER SET. Measured at N=512 with three cascades:
1152
+ * cascade 0 covers camera depth [0, 81] and submitted 348 335 instances — the entire camera-visible
1153
+ * set rather than the few in its own slice — because all three were culled against the same
1154
+ * whole-frustum volume. Not wrong, since a cascade's own projection clips the remainder, but three
1155
+ * times the vertex work it needs, and the reason a cascade still cost 10.5 ms instead of nearly
1156
+ * nothing once caster culling was on.
1157
+ *
1158
+ * ⚠️ `eye` MUST BE IN THE SAME SPACE AS `camVP` — the floating-origin-rebased eye, not the world
1159
+ * one. A mismatch shifts both split planes by the sector offset, which on a planet is zero and in
1160
+ * deep space is 1024 units, so it would pass every test near the origin.
1161
+ */
1162
+ slice) {
1163
+ // Toward the light is the opposite of the way it travels — that is the direction a caster has to be
1164
+ // displaced along to reach the frustum it shadows.
1165
+ const len = Math.hypot(sunTravel[0], sunTravel[1], sunTravel[2]) || 1;
1166
+ const dx = -sunTravel[0] / len, dy = -sunTravel[1] / len, dz = -sunTravel[2] / len;
1167
+ // ⚠️ `[0,0,0,1]` IS THE VACUOUS PLANE, and it has to be one the shader cannot reject on. The cull
1168
+ // rejects when `dot(n, c) + w < -e` for a non-negative extent `e`; with a zero normal that reads
1169
+ // `1 < -e`, which is false for every instance. A large `w` with a real normal would also work and
1170
+ // would still cost a dot product per instance for nothing.
1171
+ const planes = frustumPlanes(camVP);
1172
+ if (slice) {
1173
+ // ⚠️ INDICES 4 AND 5 ARE NEAR AND FAR, asserted rather than assumed — `casterPlanes.test.mts`
1174
+ // pins it, because `frustumPlanes` builds all six by the same `combine(row, row, ±1)` expression
1175
+ // and nothing in the shape of the result says which pair is which. Getting it wrong would clip
1176
+ // the frustum sideways instead of in depth, which still looks like a working optimisation.
1177
+ const f = planes[4]; // the near plane's normal IS the view direction
1178
+ const fe = f[0] * slice.eye[0] + f[1] * slice.eye[1] + f[2] * slice.eye[2];
1179
+ planes[4] = [f[0], f[1], f[2], -fe - slice.near];
1180
+ planes[5] = [-f[0], -f[1], -f[2], fe + slice.far];
1181
+ }
1182
+ // ⚠️ `>= 0`, NOT `> 0`, AND THE DIFFERENCE IS NOT PEDANTRY. The sweep leaves a half-space alone
1183
+ // when `n·d >= 0` and only makes it vacuous when `n·d < 0`: at exactly zero the plane is PARALLEL
1184
+ // to the sweep, so sliding along `d` never crosses it and it still bounds the volume. Dropping
1185
+ // those is safe — a wider volume only over-keeps casters — but it is silently expensive, and it
1186
+ // hits the most ordinary case there is. Sun overhead, camera level: the near and far planes are
1187
+ // perpendicular to the sweep, both were being discarded, and a cascade's depth slice then
1188
+ // constrained nothing at all.
1189
+ return planes.map((pl) => pl[0] * dx + pl[1] * dy + pl[2] * dz >= -1e-6
1190
+ ? pl
1191
+ : [0, 0, 0, 1]);
1192
+ }
1114
1193
  export const FACE_MASK_ALL = 0b111111;
1115
1194
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
1116
1195
  export function faceExposed(faceMask, d) {
@@ -2994,6 +3073,7 @@ export class GpuShadowRenderer {
2994
3073
  eye = new Float32Array(3);
2995
3074
  bevel = 0; // see setBevel — 0 keeps the sharp cube exactly, and draws only 36 vertices
2996
3075
  faceCull = false; // see setFaceCull — off is today's all-six-faces cube, bit for bit
3076
+ casterCull = false; // see setShadowCasterCull — off culls casters against the ortho box
2997
3077
  fillet = 0; // the CONCAVE radius, set separately so the two halves can be judged apart
2998
3078
  alphaPipe;
2999
3079
  alphaBGL; // kept so the overlay buffer can grow and rebind on demand
@@ -3680,6 +3760,19 @@ export class GpuShadowRenderer {
3680
3760
  setFaceCull(on) {
3681
3761
  this.faceCull = on;
3682
3762
  }
3763
+ /**
3764
+ * Cull shadow casters against the camera frustum swept toward the sun, instead of against the
3765
+ * cascade's own ortho box. See `casterPlanes` for why those are different volumes.
3766
+ *
3767
+ * ⚠️ IT CHANGES NO PIXEL, WHICH IS ALSO WHY IT NEEDS TO BE A TOGGLE. A change that is supposed to
3768
+ * be invisible cannot be verified by looking at it — the A/B is `readCullStats().lightVisible` and
3769
+ * the frame time, and both need the OFF case measured on the same machine in the same session.
3770
+ * Off by default: a caller whose shadows are sampled by something other than the camera (a mirror,
3771
+ * a reflection pass) must opt in knowingly.
3772
+ */
3773
+ setShadowCasterCull(on) {
3774
+ this.casterCull = on;
3775
+ }
3683
3776
  setAtlas(tex, layout) {
3684
3777
  this.atlasView = tex.createView({ dimension: '2d-array' });
3685
3778
  const ids = Object.keys(layout.tiles).map(Number);
@@ -3969,8 +4062,16 @@ export class GpuShadowRenderer {
3969
4062
  mp.end();
3970
4063
  }
3971
4064
  /** Write a frustum's cull uniform: 6 planes + count + this frustum's VP + occlusion params (occ on the camera pass). */
3972
- writeCull(buf, vp, occlude, bucket = 0) {
3973
- const planes = frustumPlanes(vp);
4065
+ writeCull(buf, vp, occlude, bucket = 0,
4066
+ /**
4067
+ * Cull against THESE planes instead of the ones `vp` implies.
4068
+ *
4069
+ * The shadow pass is the reason this exists: what it RENDERS with and what it should CULL with
4070
+ * are not the same volume, and using one for both is what makes a directional light draw the
4071
+ * whole world. See `casterPlanes`.
4072
+ */
4073
+ planesOverride) {
4074
+ const planes = planesOverride ?? frustumPlanes(vp);
3974
4075
  const u = new Float32Array(52); // planes[24] + cnt[4] + camVP[16] + occ[4] + lod[4]
3975
4076
  for (let p = 0; p < 6; p++) {
3976
4077
  u[p * 4] = planes[p][0];
@@ -4090,7 +4191,17 @@ export class GpuShadowRenderer {
4090
4191
  * dispatch cannot be inserted, and by then the cascade passes are already being encoded — so an
4091
4192
  * occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
4092
4193
  */
4093
- extraShadowCull, up = [0, 1, 0], linearOut = false) {
4194
+ extraShadowCull, up = [0, 1, 0], linearOut = false,
4195
+ /**
4196
+ * Per-cascade camera-depth ranges, in the same order as `lightVP`.
4197
+ *
4198
+ * Only consulted when shadow caster culling is on, and only to narrow each cascade's caster
4199
+ * volume to its own slice — the cascade still RENDERS through `lightVP[i]` exactly as before.
4200
+ * Omit it and every cascade culls against the whole frustum swept toward the sun, which is
4201
+ * correct and was the first implementation; supplying it stops the cascades duplicating each
4202
+ * other's work. The caller has these already: they are `fitShadowCascades`' own `near`/`far`.
4203
+ */
4204
+ cascadeSplits) {
4094
4205
  // ⚠️ FREES FIRST, BEFORE ANYTHING READS THE BUFFER THIS FRAME. `clearInstanceRange` queues rather
4095
4206
  // than encodes, because a caller frees where its streaming lands and has no encoder there; this is
4096
4207
  // the first point in the frame that has one, and it is ahead of the cull that would otherwise read
@@ -4184,8 +4295,18 @@ export class GpuShadowRenderer {
4184
4295
  for (const a of this.lightArgs)
4185
4296
  this.device.queue.writeBuffer(a, 0, lightReset);
4186
4297
  this.writeCull(this.camCull, camVP, occludeNow, camBevel ? 1 : faceCull ? 2 : 0);
4187
- for (let i = 0; i < nc; i++)
4188
- this.writeCull(this.lightCull[i], mats[i], false);
4298
+ // ⚠️ THE CASCADES RENDER THROUGH `mats[i]` AND CULL AGAINST SOMETHING ELSE. That is the whole
4299
+ // point: the ortho box is sized for shadow-map STABILITY (see `casterPlanes`), not for deciding
4300
+ // who casts, and using it for both is what had the sun submitting six times the camera's geometry
4301
+ // every frame. The matrix handed to the draw is untouched, so nothing that still casts moves.
4302
+ for (let i = 0; i < nc; i++) {
4303
+ const split = cascadeSplits?.[i];
4304
+ this.writeCull(this.lightCull[i], mats[i], false, 0, this.casterCull
4305
+ ? casterPlanes(camVP, sunDir, split
4306
+ ? { eye: this.eye, near: split.near, far: split.far }
4307
+ : undefined)
4308
+ : undefined);
4309
+ }
4189
4310
  // ⚠️ THE PASS OPENS FOR EITHER REASON. It used to be gated on `this.count > 0` alone, which is
4190
4311
  // right for the cube cull and wrong for a caller's: a scene with no cubes but a forest of props
4191
4312
  // still needs somewhere to dispatch. Gating on both keeps the empty-scene case free.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.31",
3
+ "version": "0.15.33",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {