ugly-game 0.15.67 → 0.15.69

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,6 +1,20 @@
1
1
  /// <reference types="@webgpu/types" preserve="true" />
2
2
  import { type DepthConvention } from '../depthConvention.js';
3
3
  export declare const MAX_CASCADES = 4;
4
+ export declare const MAX_PROP_POINT_LIGHTS = 8;
5
+ /** Caller-selected local lights, in the same rebased coordinates as frame().
6
+ * Colour is linear irradiance on a fully facing diffuse surface. Lights are
7
+ * unshadowed; optional room bounds restrict their contribution spatially. */
8
+ export interface PropPointLight {
9
+ position: readonly [number, number, number];
10
+ color: readonly [number, number, number];
11
+ radius: number;
12
+ room?: {
13
+ center: readonly [number, number, number];
14
+ halfSize: readonly [number, number, number];
15
+ bounce?: number;
16
+ };
17
+ }
4
18
  /**
5
19
  * Per-world surface + motion parameters.
6
20
  *
@@ -35,8 +49,10 @@ export interface PropLook {
35
49
  /**
36
50
  * 'legacy' (default) is the historical `hdr / (1 + 0.35·hdr)` highlight compression.
37
51
  * 'aces' is Stephen Hill's fitted ACES (RRT+ODT) curve, applied after `exposure`.
52
+ * 'linear' emits exposed linear HDR radiance without tone mapping or sRGB
53
+ * conversion, for a scene target whose compositor owns the display transform.
38
54
  */
39
- toneMapping: 'legacy' | 'aces';
55
+ toneMapping: 'legacy' | 'aces' | 'linear';
40
56
  /** Linear multiplier on HDR radiance before tone mapping. Default 1. */
41
57
  exposure: number;
42
58
  /**
@@ -276,6 +292,7 @@ export declare class GpuPropsRenderer {
276
292
  private drawBGL;
277
293
  private occPipe;
278
294
  private uBuf;
295
+ private readonly pointLightWords;
279
296
  /**
280
297
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
281
298
  * an optimization.
@@ -347,6 +364,9 @@ export declare class GpuPropsRenderer {
347
364
  * default to turn it back off. Takes effect on the next `frame()`.
348
365
  */
349
366
  setLook(look: Partial<PropLook>): void;
367
+ /** Replace the nearest local lights; [] clears them. Invalid entries are
368
+ * ignored. No source arrays or GPU textures are retained or owned here. */
369
+ setPointLights(lights: readonly PropPointLight[]): void;
350
370
  /** The current photoreal controls. */
351
371
  getLook(): Readonly<PropLook>;
352
372
  /** Advance the wind phase. Pass the frame delta; sway is skipped entirely when windAmp is 0. */
@@ -9,6 +9,9 @@
9
9
  import { frustumPlanes } from '../mat4';
10
10
  import { FORWARD_DEPTH, nearerCompare, } from '../depthConvention.js';
11
11
  export const MAX_CASCADES = 4; // must match gpuShadow's
12
+ export const MAX_PROP_POINT_LIGHTS = 8;
13
+ const POINT_LIGHT_WORDS = 4 + MAX_PROP_POINT_LIGHTS * 16;
14
+ const PROP_UNIFORM_WORDS = 72 + 16 * MAX_CASCADES + 8 + POINT_LIGHT_WORDS;
12
15
  const SHADER = /* wgsl */ `
13
16
  // wind = (dirX, dirZ, amplitude, phase) — sway, amplitude scaled by the world's air density
14
17
  // surf = (grainScale, grainDepth, roughness, emissive)
@@ -18,11 +21,12 @@ const SHADER = /* wgsl */ `
18
21
  // byte-identical. lightVP remains the legacy cascade-0 slot; shading reads cascades instead.
19
22
  // look + look2 (the opt-in photoreal controls, see PropLook) are appended after the cascades for the
20
23
  // same reason, and every one of their defaults selects the exact expressions that ran before them:
21
- // look = (toneMapping 0 legacy / 1 aces, exposure, environment-diffuse intensity (0 ⇒ flat amb),
24
+ // look = (toneMapping 0 legacy / 1 aces / 2 linear HDR, exposure, environment-diffuse intensity (0 ⇒ flat amb),
22
25
  // environment specular for every PBR material (0/1))
23
26
  // look2 = (shadow filter 0 pcf3 / 1 pcf5 / 2 pcf7 / 3 poisson, radius in texels,
24
27
  // scene-environment max lod, scene-environment irradiance lod)
25
- 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> };
28
+ struct PointLight { positionRadius: vec4<f32>, colorBounce: vec4<f32>, roomCenter: vec4<f32>, roomHalfSize: vec4<f32> };
29
+ 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}> };
26
30
  @group(0) @binding(0) var<uniform> u: U;
27
31
  // One instance. 'm' is the placement; d0/d1/d2 are the per-instance DEFORM, which is what lets
28
32
  // thousands of visibly different plants share a single mesh and a single draw call. Baking each
@@ -310,6 +314,7 @@ fn acesFitted(c: vec3<f32>) -> vec3<f32> {
310
314
  // Linear HDR → display sRGB. look.x = 0 is the historical highlight compression, untouched.
311
315
  fn toneMap(hdr: vec3<f32>) -> vec3<f32> {
312
316
  let x = hdr * u.look.y;
317
+ if (u.look.x > 1.5) { return max(x, vec3(0.0)); }
313
318
  if (u.look.x > 0.5) { return linearToSrgb(acesFitted(max(x, vec3(0.0)))); }
314
319
  return linearToSrgb(x / (vec3(1.0) + x * 0.35));
315
320
  }
@@ -418,7 +423,28 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
418
423
  * mtl.paint.x * mix(vec3(1.0),albedo,0.35)
419
424
  + (u.sunCol.xyz*pow(nh,72.0)*nl*sh*3.0 + env*0.18)
420
425
  * sparkle * mix(vec3(1.0),albedo,0.35);
421
- let hdr = (flakes + direct + diffuse * (1.0-mtl.surface.y*0.12) + reflected + sheen) * (vec3(1.0)-coatF) + coating;
426
+ var localLight = vec3(0.0);
427
+ for (var li = 0u; li < min(u32(u.pointMeta.x), ${MAX_PROP_POINT_LIGHTS}u); li++) {
428
+ let light = u.pointLights[li];
429
+ if (light.roomCenter.w > 0.5 && any(abs(i.world-light.roomCenter.xyz) > light.roomHalfSize.xyz)) { continue; }
430
+ let delta = light.positionRadius.xyz-i.world;
431
+ let d2 = dot(delta,delta);
432
+ let r2 = light.positionRadius.w*light.positionRadius.w;
433
+ if (d2 >= r2) { continue; }
434
+ let localL = delta*inverseSqrt(max(d2,0.0001));
435
+ let halfVector = V+localL;
436
+ let localH = halfVector*inverseSqrt(max(dot(halfVector,halfVector),0.00000001));
437
+ let localNl = max(dot(N,localL),0.0);
438
+ let localF = fresnel(f0,max(dot(V,localH),0.0));
439
+ let window = 1.0-d2/r2;
440
+ let attenuation = window*window/(1.0+0.04*d2);
441
+ let facing = clamp((dot(N,localL)+0.3)/1.3,0.0,1.0);
442
+ let localDiffuse = (vec3(1.0)-localF)*(1.0-metal)*albedo*facing;
443
+ let localSpec = localF*ggx(max(dot(N,localH),0.0),nv,localNl,rough)*localNl*3.14159265;
444
+ let bounce = albedo*(1.0-metal)*light.colorBounce.w*window*(dot(N,localL)*0.5+0.5);
445
+ localLight += light.colorBounce.rgb*((localDiffuse+localSpec)*attenuation+bounce)*ao;
446
+ }
447
+ let hdr = (flakes + direct + localLight + diffuse * (1.0-mtl.surface.y*0.12) + reflected + sheen) * (vec3(1.0)-coatF) + coating;
422
448
  // Smooth highlight compression preserves saturated paint rather than
423
449
  // clipping reflected softboxes to a flat white patch.
424
450
  lit = toneMap(hdr);
@@ -608,6 +634,7 @@ export class GpuPropsRenderer {
608
634
  drawBGL;
609
635
  occPipe;
610
636
  uBuf;
637
+ pointLightWords = new Float32Array(POINT_LIGHT_WORDS);
611
638
  /**
612
639
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
613
640
  * an optimization.
@@ -703,6 +730,41 @@ export class GpuPropsRenderer {
703
730
  next[key] = value;
704
731
  this.look = next;
705
732
  }
733
+ /** Replace the nearest local lights; [] clears them. Invalid entries are
734
+ * ignored. No source arrays or GPU textures are retained or owned here. */
735
+ setPointLights(lights) {
736
+ const words = this.pointLightWords;
737
+ let count = 0;
738
+ for (const light of lights) {
739
+ if (count === MAX_PROP_POINT_LIGHTS)
740
+ break;
741
+ const room = light.room;
742
+ if (!Number.isFinite(light.radius) ||
743
+ light.radius <= 0 ||
744
+ !light.position.every(Number.isFinite) ||
745
+ !light.color.every(Number.isFinite) ||
746
+ (room &&
747
+ (!room.center.every(Number.isFinite) ||
748
+ !room.halfSize.every((v) => Number.isFinite(v) && v >= 0))))
749
+ continue;
750
+ const at = 4 + count++ * 16;
751
+ words.set(light.position, at);
752
+ words[at + 3] = light.radius;
753
+ for (let c = 0; c < 3; c++)
754
+ words[at + 4 + c] = Math.max(0, light.color[c]);
755
+ const bounce = room?.bounce ?? 0;
756
+ words[at + 7] = Number.isFinite(bounce)
757
+ ? Math.max(0, Math.min(1, bounce))
758
+ : 0;
759
+ words.fill(0, at + 8, at + 16);
760
+ if (room) {
761
+ words.set(room.center, at + 8);
762
+ words[at + 11] = 1;
763
+ words.set(room.halfSize, at + 12);
764
+ }
765
+ }
766
+ words[0] = count;
767
+ }
706
768
  /** The current photoreal controls. */
707
769
  getLook() {
708
770
  return { ...this.look };
@@ -904,7 +966,7 @@ export class GpuPropsRenderer {
904
966
  },
905
967
  });
906
968
  this.uBuf = device.createBuffer({
907
- size: 128 + 16 * 9 + 16 + 64 * MAX_CASCADES + 32, // + csm vec4 + the cascade matrices + look, look2
969
+ size: PROP_UNIFORM_WORDS * 4,
908
970
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
909
971
  });
910
972
  for (let c = 0; c < MAX_CASCADES; c++)
@@ -1488,7 +1550,7 @@ export class GpuPropsRenderer {
1488
1550
  lightVP, shadowView, shadowSamp) {
1489
1551
  const receive = !!(lightVP && shadowView && shadowSamp);
1490
1552
  const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
1491
- const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES + 8);
1553
+ const u = new Float32Array(PROP_UNIFORM_WORDS);
1492
1554
  u.set(camVP, 0);
1493
1555
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
1494
1556
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
@@ -1516,7 +1578,7 @@ export class GpuPropsRenderer {
1516
1578
  const envMaxLod = env ? env.mipLevelCount - 1 : 0;
1517
1579
  const lookAt = 72 + 16 * MAX_CASCADES;
1518
1580
  u.set([
1519
- look.toneMapping === 'aces' ? 1 : 0,
1581
+ look.toneMapping === 'linear' ? 2 : look.toneMapping === 'aces' ? 1 : 0,
1520
1582
  Number.isFinite(look.exposure) ? Math.max(0, look.exposure) : 1,
1521
1583
  env && look.environmentDiffuse > 0 ? look.environmentDiffuse : 0,
1522
1584
  env && look.environmentSpecular ? 1 : 0,
@@ -1525,6 +1587,7 @@ export class GpuPropsRenderer {
1525
1587
  envMaxLod,
1526
1588
  Math.max(0, Math.min(envMaxLod, look.environmentDiffuseLod ?? envMaxLod - 3)),
1527
1589
  ], lookAt);
1590
+ u.set(this.pointLightWords, lookAt + 8);
1528
1591
  this.device.queue.writeBuffer(this.uBuf, 0, u);
1529
1592
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
1530
1593
  const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.67",
3
+ "version": "0.15.69",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {