ugly-game 0.15.32 → 0.15.34

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.
@@ -597,7 +597,26 @@ export declare const FACE_DIRS: readonly (readonly [number, number, number])[];
597
597
  * @param camVP the CAMERA's view-projection, not the light's
598
598
  * @param sunTravel the direction light TRAVELS (from the sun outward), matching `frame`'s `sunDir`
599
599
  */
600
- export declare function casterPlanes(camVP: ArrayLike<number>, sunTravel: readonly [number, number, number]): Plane[];
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[];
601
620
  export declare const FACE_MASK_ALL = 63;
602
621
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
603
622
  export declare function faceExposed(faceMask: number, d: number): boolean;
@@ -1073,7 +1092,20 @@ export declare class GpuShadowRenderer {
1073
1092
  * dispatch cannot be inserted, and by then the cascade passes are already being encoded — so an
1074
1093
  * occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
1075
1094
  */
1076
- 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;
1077
1109
  /** Read back the last frame's GPU cull result (camera-visible, light-visible). Does its OWN copy+submit so it's
1078
1110
  * safe to call from a live render loop (no per-frame readback cost, no race with the ongoing frames). IR/tests. */
1079
1111
  readCullStats(): Promise<{
package/dist/gpuShadow.js CHANGED
@@ -1144,7 +1144,22 @@ export const FACE_DIRS = [
1144
1144
  * @param camVP the CAMERA's view-projection, not the light's
1145
1145
  * @param sunTravel the direction light TRAVELS (from the sun outward), matching `frame`'s `sunDir`
1146
1146
  */
1147
- export function casterPlanes(camVP, sunTravel) {
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) {
1148
1163
  // Toward the light is the opposite of the way it travels — that is the direction a caster has to be
1149
1164
  // displaced along to reach the frustum it shadows.
1150
1165
  const len = Math.hypot(sunTravel[0], sunTravel[1], sunTravel[2]) || 1;
@@ -1153,7 +1168,27 @@ export function casterPlanes(camVP, sunTravel) {
1153
1168
  // rejects when `dot(n, c) + w < -e` for a non-negative extent `e`; with a zero normal that reads
1154
1169
  // `1 < -e`, which is false for every instance. A large `w` with a real normal would also work and
1155
1170
  // 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]);
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]);
1157
1192
  }
1158
1193
  export const FACE_MASK_ALL = 0b111111;
1159
1194
  /** Is face `d` drawn? Bit d set ⇒ yes. 0 is the unknown sentinel ⇒ every face. */
@@ -4156,7 +4191,17 @@ export class GpuShadowRenderer {
4156
4191
  * dispatch cannot be inserted, and by then the cascade passes are already being encoded — so an
4157
4192
  * occluder that wants to cull per cascade has to be handed every cascade matrix up front, here.
4158
4193
  */
4159
- 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) {
4160
4205
  // ⚠️ FREES FIRST, BEFORE ANYTHING READS THE BUFFER THIS FRAME. `clearInstanceRange` queues rather
4161
4206
  // than encodes, because a caller frees where its streaming lands and has no encoder there; this is
4162
4207
  // the first point in the frame that has one, and it is ahead of the cull that would otherwise read
@@ -4254,9 +4299,14 @@ export class GpuShadowRenderer {
4254
4299
  // point: the ortho box is sized for shadow-map STABILITY (see `casterPlanes`), not for deciding
4255
4300
  // who casts, and using it for both is what had the sun submitting six times the camera's geometry
4256
4301
  // 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;
4258
- for (let i = 0; i < nc; i++)
4259
- this.writeCull(this.lightCull[i], mats[i], false, 0, casters);
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
+ }
4260
4310
  // ⚠️ THE PASS OPENS FOR EITHER REASON. It used to be gated on `this.count > 0` alone, which is
4261
4311
  // right for the cube cull and wrong for a caller's: a scene with no cubes but a forest of props
4262
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.32",
3
+ "version": "0.15.34",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {