ugly-game 0.15.68 → 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
  *
@@ -278,6 +292,7 @@ export declare class GpuPropsRenderer {
278
292
  private drawBGL;
279
293
  private occPipe;
280
294
  private uBuf;
295
+ private readonly pointLightWords;
281
296
  /**
282
297
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
283
298
  * an optimization.
@@ -349,6 +364,9 @@ export declare class GpuPropsRenderer {
349
364
  * default to turn it back off. Takes effect on the next `frame()`.
350
365
  */
351
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;
352
370
  /** The current photoreal controls. */
353
371
  getLook(): Readonly<PropLook>;
354
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)
@@ -22,7 +25,8 @@ const SHADER = /* wgsl */ `
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
@@ -419,7 +423,28 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
419
423
  * mtl.paint.x * mix(vec3(1.0),albedo,0.35)
420
424
  + (u.sunCol.xyz*pow(nh,72.0)*nl*sh*3.0 + env*0.18)
421
425
  * sparkle * mix(vec3(1.0),albedo,0.35);
422
- 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;
423
448
  // Smooth highlight compression preserves saturated paint rather than
424
449
  // clipping reflected softboxes to a flat white patch.
425
450
  lit = toneMap(hdr);
@@ -609,6 +634,7 @@ export class GpuPropsRenderer {
609
634
  drawBGL;
610
635
  occPipe;
611
636
  uBuf;
637
+ pointLightWords = new Float32Array(POINT_LIGHT_WORDS);
612
638
  /**
613
639
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
614
640
  * an optimization.
@@ -704,6 +730,41 @@ export class GpuPropsRenderer {
704
730
  next[key] = value;
705
731
  this.look = next;
706
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
+ }
707
768
  /** The current photoreal controls. */
708
769
  getLook() {
709
770
  return { ...this.look };
@@ -905,7 +966,7 @@ export class GpuPropsRenderer {
905
966
  },
906
967
  });
907
968
  this.uBuf = device.createBuffer({
908
- size: 128 + 16 * 9 + 16 + 64 * MAX_CASCADES + 32, // + csm vec4 + the cascade matrices + look, look2
969
+ size: PROP_UNIFORM_WORDS * 4,
909
970
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
910
971
  });
911
972
  for (let c = 0; c < MAX_CASCADES; c++)
@@ -1489,7 +1550,7 @@ export class GpuPropsRenderer {
1489
1550
  lightVP, shadowView, shadowSamp) {
1490
1551
  const receive = !!(lightVP && shadowView && shadowSamp);
1491
1552
  const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
1492
- const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES + 8);
1553
+ const u = new Float32Array(PROP_UNIFORM_WORDS);
1493
1554
  u.set(camVP, 0);
1494
1555
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
1495
1556
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
@@ -1526,6 +1587,7 @@ export class GpuPropsRenderer {
1526
1587
  envMaxLod,
1527
1588
  Math.max(0, Math.min(envMaxLod, look.environmentDiffuseLod ?? envMaxLod - 3)),
1528
1589
  ], lookAt);
1590
+ u.set(this.pointLightWords, lookAt + 8);
1529
1591
  this.device.queue.writeBuffer(this.uBuf, 0, u);
1530
1592
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
1531
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.68",
3
+ "version": "0.15.69",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {