ugly-game 0.15.78 → 0.15.79

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.
@@ -103,6 +103,15 @@ export interface PropRendererOptions extends Partial<PropLook> {
103
103
  format: GPUTextureFormat;
104
104
  transferWGSL: string;
105
105
  };
106
+ /** Optional shared scene fog. WGSL defines `propFog(color, delta, legacy, uniform)`
107
+ * returning linear RGB. `delta` is relative to the camera and `legacy` is
108
+ * the frame's RGB and density. The uniform type must have 16-byte alignment
109
+ * or less and fit in `uniformWords` floats, a positive multiple of four. */
110
+ fog?: {
111
+ wgsl: string;
112
+ uniformType: string;
113
+ uniformWords: number;
114
+ };
106
115
  /** Compile material/topology inspection channels. Off by default. On the
107
116
  * first topology view, an expanded corner stream is built for wire/density. */
108
117
  inspection?: boolean;
@@ -346,6 +355,7 @@ export declare class GpuPropsRenderer {
346
355
  private drawBGL;
347
356
  private occPipe;
348
357
  private uBuf;
358
+ private readonly fogUniformWords;
349
359
  private readonly pointLightWords;
350
360
  /**
351
361
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
@@ -513,7 +523,7 @@ export declare class GpuPropsRenderer {
513
523
  * a single shared uniform written once per cascade during encoding does not work — see `lightBufs`.
514
524
  */
515
525
  shadowPass(pass: GPURenderPassEncoder, lightVP: Float32Array, cascade?: number): void;
516
- 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, channel?: PropInspectionChannel, sceneTexture?: GPUTexture): void;
526
+ 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, channel?: PropInspectionChannel, sceneTexture?: GPUTexture, fogUniform?: Float32Array): void;
517
527
  /** Draw over the presented compositor result, testing its final depth.
518
528
  * Reuses the same uploaded instances, material bindings and frame uniforms. */
519
529
  framePresentation(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, sceneTexture?: GPUTexture): void;
@@ -20,7 +20,7 @@ export const MAX_CASCADES = 4; // must match gpuShadow's
20
20
  export const MAX_PROP_POINT_LIGHTS = 8;
21
21
  const POINT_LIGHT_WORDS = 4 + MAX_PROP_POINT_LIGHTS * 16;
22
22
  const PROP_UNIFORM_WORDS = 72 + 16 * MAX_CASCADES + 8 + POINT_LIGHT_WORDS;
23
- const drawShader = (inspect, split, transfer = '') => /* wgsl */ `
23
+ const drawShader = (inspect, split, transfer = '', fog) => /* wgsl */ `
24
24
  // wind = (dirX, dirZ, amplitude, phase) — sway, amplitude scaled by the world's air density
25
25
  // surf = (grainScale, grainDepth, roughness, emissive)
26
26
  // grade = (hueShift, saturation, valueLift, unused)
@@ -34,8 +34,15 @@ const drawShader = (inspect, split, transfer = '') => /* wgsl */ `
34
34
  // look2 = (shadow filter 0 pcf3 / 1 pcf5 / 2 pcf7 / 3 poisson, radius in texels,
35
35
  // scene-environment max lod, scene-environment irradiance lod)
36
36
  struct PointLight { positionRadius: vec4<f32>, colorBounce: vec4<f32>, roomCenter: vec4<f32>, roomHalfSize: vec4<f32> };
37
- 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}>, look: vec4<f32>, look2: vec4<f32>, pointMeta: vec4<f32>, pointLights: array<PointLight, ${MAX_PROP_POINT_LIGHTS}> };
37
+ ${fog?.wgsl ?? ''}
38
+ 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}>, look: vec4<f32>, look2: vec4<f32>, pointMeta: vec4<f32>, pointLights: array<PointLight, ${MAX_PROP_POINT_LIGHTS}>${fog ? `, fogUniform: ${fog.uniformType}` : ''} };
38
39
  @group(0) @binding(0) var<uniform> u: U;
40
+ fn applyPropFog(color: vec3<f32>, world: vec3<f32>) -> vec3<f32> {
41
+ let delta = world - u.cam.xyz;
42
+ ${fog
43
+ ? 'return propFog(color, delta, u.fog, u.fogUniform);'
44
+ : 'return mix(color, u.fog.xyz, clamp(1.0-exp(-length(delta)*u.fog.w),0.0,1.0));'}
45
+ }
39
46
  // One instance. 'm' is the placement; d0/d1/d2 are the per-instance DEFORM, which is what lets
40
47
  // thousands of visibly different plants share a single mesh and a single draw call. Baking each
41
48
  // variant on the CPU instead costs one model, one vertex buffer and one draw call per plant —
@@ -423,8 +430,7 @@ fn shade(i: VO) -> vec4<f32> {
423
430
  base = hsvShift(base, u.grade.x, max(u.grade.y, 0.0), max(u.grade.z, 0.0));
424
431
  ${inspect ? inspectionFragment : ''}
425
432
  if (mtl.optics.y > 1.5) {
426
- let fog = clamp(1.0-exp(-length(i.world-u.cam.xyz)*u.fog.w),0.0,1.0);
427
- return vec4(mix(propEmission(tex.rgb,i.c,normalize(i.n),i.world),u.fog.xyz,fog),mtl.mat.x);
433
+ return vec4(applyPropFog(propEmission(tex.rgb,i.c,normalize(i.n),i.world),i.world),mtl.mat.x);
428
434
  }
429
435
  let spec = u.surf.z;
430
436
  var lit = base * (u.amb.xyz * ff + d * u.sunCol.xyz * sh * (1.0 + spec * 0.6))
@@ -523,8 +529,6 @@ fn shade(i: VO) -> vec4<f32> {
523
529
  // clipping reflected softboxes to a flat white patch.
524
530
  lit = toneMap(hdr);
525
531
  }
526
- let dist = length(i.world - u.cam.xyz);
527
- let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
528
532
  // mtl.mat.x is the model's opacity, 1 for every model that does not set it — the same constant
529
533
  // this returned before, so the opaque pipeline draws exactly what it always did.
530
534
  var opacity = surfaceOpacity(N,i.world);
@@ -553,7 +557,7 @@ fn shade(i: VO) -> vec4<f32> {
553
557
  }
554
558
  // A display-referred render texture (rear-view camera) is already lit.
555
559
  if (mtl.optics.y > 0.5) { return vec4(tex.rgb, mtl.mat.x); }
556
- return vec4(mix(lit, u.fog.xyz, f), opacity);
560
+ return vec4(applyPropFog(lit, i.world), opacity);
557
561
  }
558
562
  @fragment fn fs(i: VO) -> @location(0) vec4<f32> {
559
563
  let color = shade(i);
@@ -753,6 +757,7 @@ export class GpuPropsRenderer {
753
757
  drawBGL;
754
758
  occPipe;
755
759
  uBuf;
760
+ fogUniformWords;
756
761
  pointLightWords = new Float32Array(POINT_LIGHT_WORDS);
757
762
  /**
758
763
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
@@ -899,7 +904,7 @@ export class GpuPropsRenderer {
899
904
  constructor(device, format, options = {}) {
900
905
  this.device = device;
901
906
  this.format = format;
902
- const { inspection = false, sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', presentation, ...look } = options;
907
+ const { inspection = false, sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', presentation, fog, ...look } = options;
903
908
  // Runtime guard for untyped callers: WebGPU only guarantees 1 and 4.
904
909
  if (![1, 4].includes(sampleCount))
905
910
  throw new RangeError(`Unsupported prop sampleCount ${String(sampleCount)}`);
@@ -908,11 +913,19 @@ export class GpuPropsRenderer {
908
913
  this.sampleCount = sampleCount;
909
914
  this.depth = depth;
910
915
  this.presentation = presentation;
916
+ this.fogUniformWords = fog?.uniformWords ?? 0;
917
+ if (fog &&
918
+ (!Number.isInteger(fog.uniformWords) ||
919
+ fog.uniformWords <= 0 ||
920
+ fog.uniformWords % 4 !== 0 ||
921
+ (PROP_UNIFORM_WORDS + fog.uniformWords) * 4 >
922
+ device.limits.maxUniformBufferBindingSize))
923
+ throw new RangeError('Invalid prop fog uniform size');
911
924
  this.setLook(look);
912
925
  this.inspection = inspection;
913
926
  this.srgbTarget = format.endsWith('-srgb');
914
927
  const sm = device.createShaderModule({
915
- code: drawShader(inspection, !!presentation),
928
+ code: drawShader(inspection, !!presentation, '', fog),
916
929
  });
917
930
  // The three groups the SHADER declares, written out so BOTH render pipelines share them. Under
918
931
  // `layout: 'auto'` each pipeline would mint its own and the bind groups built elsewhere in this
@@ -1072,7 +1085,7 @@ export class GpuPropsRenderer {
1072
1085
  fragment: {
1073
1086
  ...base.fragment,
1074
1087
  module: (presentationModule ??= device.createShaderModule({
1075
- code: drawShader(inspection, true, presentation.transferWGSL),
1088
+ code: drawShader(inspection, true, presentation.transferWGSL, fog),
1076
1089
  })),
1077
1090
  targets: [
1078
1091
  {
@@ -1127,7 +1140,7 @@ export class GpuPropsRenderer {
1127
1140
  },
1128
1141
  });
1129
1142
  this.uBuf = device.createBuffer({
1130
- size: PROP_UNIFORM_WORDS * 4,
1143
+ size: (PROP_UNIFORM_WORDS + this.fogUniformWords) * 4,
1131
1144
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
1132
1145
  });
1133
1146
  for (let c = 0; c < MAX_CASCADES; c++)
@@ -1826,7 +1839,7 @@ export class GpuPropsRenderer {
1826
1839
  frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, up,
1827
1840
  // Optional sun shadow-receive: pass the terrain's lightVP + shadow-map view + comparison sampler and props sit
1828
1841
  // in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
1829
- lightVP, shadowView, shadowSamp, channel = 'lit', sceneTexture) {
1842
+ lightVP, shadowView, shadowSamp, channel = 'lit', sceneTexture, fogUniform) {
1830
1843
  if (this.glassInstances.length)
1831
1844
  this.glassVP.set(camVP);
1832
1845
  this.glassCopyPixels.scene = this.glassCopyPixels.presentation = 0;
@@ -1834,7 +1847,12 @@ export class GpuPropsRenderer {
1834
1847
  throw new Error('Prop inspection must be enabled at construction.');
1835
1848
  const receive = !!(lightVP && shadowView && shadowSamp);
1836
1849
  const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
1837
- const u = new Float32Array(PROP_UNIFORM_WORDS);
1850
+ const u = new Float32Array(PROP_UNIFORM_WORDS + this.fogUniformWords);
1851
+ if (fogUniform) {
1852
+ if (fogUniform.length !== this.fogUniformWords || !this.fogUniformWords)
1853
+ throw new RangeError('Prop fog uniform does not match the renderer');
1854
+ u.set(fogUniform, PROP_UNIFORM_WORDS);
1855
+ }
1838
1856
  u.set(camVP, 0);
1839
1857
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
1840
1858
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.78",
3
+ "version": "0.15.79",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {