ugly-game 0.15.68 → 0.15.70

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
  *
@@ -90,6 +104,8 @@ export interface PropRendererOptions extends Partial<PropLook> {
90
104
  * that maps near to 1 and far to 0. The sun-shadow occluder keeps its own depth domain.
91
105
  */
92
106
  depth?: DepthConvention;
107
+ /** Format of the caller's sun-shadow depth attachment. Default depth32float. */
108
+ shadowDepthFormat?: 'depth16unorm' | 'depth32float';
93
109
  }
94
110
  export interface PropMesh {
95
111
  /** Offline-cooked deformation bounds. Omit for procedural meshes. */
@@ -278,6 +294,7 @@ export declare class GpuPropsRenderer {
278
294
  private drawBGL;
279
295
  private occPipe;
280
296
  private uBuf;
297
+ private readonly pointLightWords;
281
298
  /**
282
299
  * The occluder's per-cascade uniform — lightVP + wind. ONE PER CASCADE, and that is a bug fix, not
283
300
  * an optimization.
@@ -349,6 +366,9 @@ export declare class GpuPropsRenderer {
349
366
  * default to turn it back off. Takes effect on the next `frame()`.
350
367
  */
351
368
  setLook(look: Partial<PropLook>): void;
369
+ /** Replace the nearest local lights; [] clears them. Invalid entries are
370
+ * ignored. No source arrays or GPU textures are retained or owned here. */
371
+ setPointLights(lights: readonly PropPointLight[]): void;
352
372
  /** The current photoreal controls. */
353
373
  getLook(): Readonly<PropLook>;
354
374
  /** 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 };
@@ -715,10 +776,12 @@ export class GpuPropsRenderer {
715
776
  }
716
777
  constructor(device, format, options = {}) {
717
778
  this.device = device;
718
- const { sampleCount = 1, depth = FORWARD_DEPTH, ...look } = options;
779
+ const { sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', ...look } = options;
719
780
  // Runtime guard for untyped callers: WebGPU only guarantees 1 and 4.
720
781
  if (![1, 4].includes(sampleCount))
721
782
  throw new RangeError(`Unsupported prop sampleCount ${String(sampleCount)}`);
783
+ if (!['depth16unorm', 'depth32float'].includes(shadowDepthFormat))
784
+ throw new RangeError(`Unsupported prop shadowDepthFormat ${shadowDepthFormat}`);
722
785
  this.sampleCount = sampleCount;
723
786
  this.depth = depth;
724
787
  this.setLook(look);
@@ -895,7 +958,7 @@ export class GpuPropsRenderer {
895
958
  },
896
959
  primitive: { topology: 'triangle-list', cullMode: 'none' },
897
960
  depthStencil: {
898
- format: 'depth32float',
961
+ format: shadowDepthFormat,
899
962
  depthWriteEnabled: true,
900
963
  depthCompare: 'less',
901
964
  // Offset the caster by its rasterized slope: a constant receiver bias
@@ -905,7 +968,7 @@ export class GpuPropsRenderer {
905
968
  },
906
969
  });
907
970
  this.uBuf = device.createBuffer({
908
- size: 128 + 16 * 9 + 16 + 64 * MAX_CASCADES + 32, // + csm vec4 + the cascade matrices + look, look2
971
+ size: PROP_UNIFORM_WORDS * 4,
909
972
  usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
910
973
  });
911
974
  for (let c = 0; c < MAX_CASCADES; c++)
@@ -922,7 +985,7 @@ export class GpuPropsRenderer {
922
985
  });
923
986
  const dummy = device.createTexture({
924
987
  size: [1, 1],
925
- format: 'depth32float',
988
+ format: shadowDepthFormat,
926
989
  usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.RENDER_ATTACHMENT,
927
990
  });
928
991
  this.dummyShadowView = dummy.createView({ dimension: '2d-array' }); // the binding is an array now
@@ -1489,7 +1552,7 @@ export class GpuPropsRenderer {
1489
1552
  lightVP, shadowView, shadowSamp) {
1490
1553
  const receive = !!(lightVP && shadowView && shadowSamp);
1491
1554
  const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
1492
- const u = new Float32Array(68 + 4 + 16 * MAX_CASCADES + 8);
1555
+ const u = new Float32Array(PROP_UNIFORM_WORDS);
1493
1556
  u.set(camVP, 0);
1494
1557
  u.set(mats[0] ?? IDENTITY4, 16); // legacy cascade-0 slot, kept so the offsets above never move
1495
1558
  u.set([sunDir[0], sunDir[1], sunDir[2], receive ? 1 : 0], 32); // sun.w = shadow-receive enable
@@ -1526,6 +1589,7 @@ export class GpuPropsRenderer {
1526
1589
  envMaxLod,
1527
1590
  Math.max(0, Math.min(envMaxLod, look.environmentDiffuseLod ?? envMaxLod - 3)),
1528
1591
  ], lookAt);
1592
+ u.set(this.pointLightWords, lookAt + 8);
1529
1593
  this.device.queue.writeBuffer(this.uBuf, 0, u);
1530
1594
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
1531
1595
  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.70",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {