ugly-game 0.15.31 → 0.15.32

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,40 @@ 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]): Plane[];
563
601
  export declare const FACE_MASK_ALL = 63;
564
602
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
565
603
  export declare function faceExposed(faceMask: number, d: number): boolean;
@@ -678,6 +716,7 @@ export declare class GpuShadowRenderer {
678
716
  private eye;
679
717
  private bevel;
680
718
  private faceCull;
719
+ private casterCull;
681
720
  private fillet;
682
721
  private alphaPipe;
683
722
  private alphaBGL;
@@ -840,6 +879,17 @@ export declare class GpuShadowRenderer {
840
879
  * ⚠️ AND IT IS IGNORED WHILE A BEVEL IS SET. See `frame()` — the two are mutually exclusive.
841
880
  */
842
881
  setFaceCull(on: boolean): void;
882
+ /**
883
+ * Cull shadow casters against the camera frustum swept toward the sun, instead of against the
884
+ * cascade's own ortho box. See `casterPlanes` for why those are different volumes.
885
+ *
886
+ * ⚠️ IT CHANGES NO PIXEL, WHICH IS ALSO WHY IT NEEDS TO BE A TOGGLE. A change that is supposed to
887
+ * be invisible cannot be verified by looking at it — the A/B is `readCullStats().lightVisible` and
888
+ * the frame time, and both need the OFF case measured on the same machine in the same session.
889
+ * Off by default: a caller whose shadows are sampled by something other than the camera (a mirror,
890
+ * a reflection pass) must opt in knowingly.
891
+ */
892
+ setShadowCasterCull(on: boolean): void;
843
893
  setAtlas(tex: GPUTexture, layout: AtlasLayout): void;
844
894
  /** Static scene: sizes the buffer to exactly these instances. */
845
895
  setScene(instances: CubeInstance[]): void;
package/dist/gpuShadow.js CHANGED
@@ -1111,6 +1111,50 @@ 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
+ // Toward the light is the opposite of the way it travels — that is the direction a caster has to be
1149
+ // displaced along to reach the frustum it shadows.
1150
+ const len = Math.hypot(sunTravel[0], sunTravel[1], sunTravel[2]) || 1;
1151
+ const dx = -sunTravel[0] / len, dy = -sunTravel[1] / len, dz = -sunTravel[2] / len;
1152
+ // ⚠️ `[0,0,0,1]` IS THE VACUOUS PLANE, and it has to be one the shader cannot reject on. The cull
1153
+ // rejects when `dot(n, c) + w < -e` for a non-negative extent `e`; with a zero normal that reads
1154
+ // `1 < -e`, which is false for every instance. A large `w` with a real normal would also work and
1155
+ // would still cost a dot product per instance for nothing.
1156
+ return frustumPlanes(camVP).map((pl) => pl[0] * dx + pl[1] * dy + pl[2] * dz > 1e-6 ? pl : [0, 0, 0, 1]);
1157
+ }
1114
1158
  export const FACE_MASK_ALL = 0b111111;
1115
1159
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
1116
1160
  export function faceExposed(faceMask, d) {
@@ -2994,6 +3038,7 @@ export class GpuShadowRenderer {
2994
3038
  eye = new Float32Array(3);
2995
3039
  bevel = 0; // see setBevel — 0 keeps the sharp cube exactly, and draws only 36 vertices
2996
3040
  faceCull = false; // see setFaceCull — off is today's all-six-faces cube, bit for bit
3041
+ casterCull = false; // see setShadowCasterCull — off culls casters against the ortho box
2997
3042
  fillet = 0; // the CONCAVE radius, set separately so the two halves can be judged apart
2998
3043
  alphaPipe;
2999
3044
  alphaBGL; // kept so the overlay buffer can grow and rebind on demand
@@ -3680,6 +3725,19 @@ export class GpuShadowRenderer {
3680
3725
  setFaceCull(on) {
3681
3726
  this.faceCull = on;
3682
3727
  }
3728
+ /**
3729
+ * Cull shadow casters against the camera frustum swept toward the sun, instead of against the
3730
+ * cascade's own ortho box. See `casterPlanes` for why those are different volumes.
3731
+ *
3732
+ * ⚠️ IT CHANGES NO PIXEL, WHICH IS ALSO WHY IT NEEDS TO BE A TOGGLE. A change that is supposed to
3733
+ * be invisible cannot be verified by looking at it — the A/B is `readCullStats().lightVisible` and
3734
+ * the frame time, and both need the OFF case measured on the same machine in the same session.
3735
+ * Off by default: a caller whose shadows are sampled by something other than the camera (a mirror,
3736
+ * a reflection pass) must opt in knowingly.
3737
+ */
3738
+ setShadowCasterCull(on) {
3739
+ this.casterCull = on;
3740
+ }
3683
3741
  setAtlas(tex, layout) {
3684
3742
  this.atlasView = tex.createView({ dimension: '2d-array' });
3685
3743
  const ids = Object.keys(layout.tiles).map(Number);
@@ -3969,8 +4027,16 @@ export class GpuShadowRenderer {
3969
4027
  mp.end();
3970
4028
  }
3971
4029
  /** 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);
4030
+ writeCull(buf, vp, occlude, bucket = 0,
4031
+ /**
4032
+ * Cull against THESE planes instead of the ones `vp` implies.
4033
+ *
4034
+ * The shadow pass is the reason this exists: what it RENDERS with and what it should CULL with
4035
+ * are not the same volume, and using one for both is what makes a directional light draw the
4036
+ * whole world. See `casterPlanes`.
4037
+ */
4038
+ planesOverride) {
4039
+ const planes = planesOverride ?? frustumPlanes(vp);
3974
4040
  const u = new Float32Array(52); // planes[24] + cnt[4] + camVP[16] + occ[4] + lod[4]
3975
4041
  for (let p = 0; p < 6; p++) {
3976
4042
  u[p * 4] = planes[p][0];
@@ -4184,8 +4250,13 @@ export class GpuShadowRenderer {
4184
4250
  for (const a of this.lightArgs)
4185
4251
  this.device.queue.writeBuffer(a, 0, lightReset);
4186
4252
  this.writeCull(this.camCull, camVP, occludeNow, camBevel ? 1 : faceCull ? 2 : 0);
4253
+ // ⚠️ THE CASCADES RENDER THROUGH `mats[i]` AND CULL AGAINST SOMETHING ELSE. That is the whole
4254
+ // point: the ortho box is sized for shadow-map STABILITY (see `casterPlanes`), not for deciding
4255
+ // who casts, and using it for both is what had the sun submitting six times the camera's geometry
4256
+ // every frame. The matrix handed to the draw is untouched, so nothing that still casts moves.
4257
+ const casters = this.casterCull ? casterPlanes(camVP, sunDir) : undefined;
4187
4258
  for (let i = 0; i < nc; i++)
4188
- this.writeCull(this.lightCull[i], mats[i], false);
4259
+ this.writeCull(this.lightCull[i], mats[i], false, 0, casters);
4189
4260
  // ⚠️ THE PASS OPENS FOR EITHER REASON. It used to be gated on `this.count > 0` alone, which is
4190
4261
  // right for the cube cull and wrong for a caller's: a scene with no cubes but a forest of props
4191
4262
  // 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.32",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {