ugly-game 0.15.72 → 0.15.74

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,5 +1,7 @@
1
1
  /// <reference types="@webgpu/types" preserve="true" />
2
2
  import { type DepthConvention } from '../depthConvention.js';
3
+ import { type PropInspectionChannel } from './propInspection.js';
4
+ export type { PropInspectionChannel } from './propInspection.js';
3
5
  export declare const MAX_CASCADES = 4;
4
6
  export declare const MAX_PROP_POINT_LIGHTS = 8;
5
7
  /** Caller-selected local lights, in the same rebased coordinates as frame().
@@ -91,6 +93,9 @@ export interface PropLook {
91
93
  }
92
94
  /** Constructor options. All optional; omitting them is the historical renderer. */
93
95
  export interface PropRendererOptions extends Partial<PropLook> {
96
+ /** Compile material/topology inspection channels. Off by default. On the
97
+ * first topology view, an expanded corner stream is built for wire/density. */
98
+ inspection?: boolean;
94
99
  /**
95
100
  * MSAA sample count for every pipeline that draws into the caller's colour/depth views
96
101
  * (opaque, blended, and every cull/depth-write variant). Default 1. With 4, `frame()` must be
@@ -150,6 +155,8 @@ export interface PropMesh {
150
155
  diffuseTransmission?: number;
151
156
  /** Roughness of the clear lacquer layer; default 0.12. */
152
157
  clearcoatRoughness?: number;
158
+ /** Fraction of the clearcoat's directional-light reflection, 0..1 (default 0). */
159
+ directClearcoat?: number;
153
160
  /** Stable fine metallic flake strength; zero disables. */
154
161
  metalFlakes?: number;
155
162
  /** Flake cells per object-space unit (usually meters). Default 320. */
@@ -282,6 +289,9 @@ export interface PropInstance {
282
289
  }
283
290
  export declare class GpuPropsRenderer {
284
291
  private device;
292
+ private inspection;
293
+ private srgbTarget;
294
+ private inspectionGeometry?;
285
295
  private pipe;
286
296
  /**
287
297
  * The same pipeline with source-alpha blending and depth writes off, for models with `alpha < 1`.
@@ -397,7 +407,7 @@ export declare class GpuPropsRenderer {
397
407
  * model moves to the blended pipeline and out of shadowPass from the next frame, exactly as if
398
408
  * `setModels` had been given that alpha; back at 1 it is opaque and casts again.
399
409
  */
400
- setMaterial(model: number, surface: Pick<PropMesh, 'alpha' | 'roughness' | 'metalness' | 'clearcoat' | 'clearcoatRoughness' | 'reflectionStrength' | 'specularIntensity' | 'metalFlakes' | 'flakeScale' | 'pearlStrength'>): void;
410
+ setMaterial(model: number, surface: Pick<PropMesh, 'alpha' | 'roughness' | 'metalness' | 'clearcoat' | 'clearcoatRoughness' | 'directClearcoat' | 'reflectionStrength' | 'specularIntensity' | 'metalFlakes' | 'flakeScale' | 'pearlStrength'>): void;
401
411
  setModels(meshes: PropMesh[]): void;
402
412
  /**
403
413
  * Group instances by model and upload the per-model instance buffers.
@@ -453,5 +463,5 @@ export declare class GpuPropsRenderer {
453
463
  * a single shared uniform written once per cascade during encoding does not work — see `lightBufs`.
454
464
  */
455
465
  shadowPass(pass: GPURenderPassEncoder, lightVP: Float32Array, cascade?: number): void;
456
- 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): void;
466
+ 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): void;
457
467
  }
@@ -8,11 +8,12 @@
8
8
  // Gribb-Hartmann is exactly how a cull ends up disagreeing with the projection it is culling for.
9
9
  import { frustumPlanes } from '../mat4';
10
10
  import { FORWARD_DEPTH, nearerCompare, } from '../depthConvention.js';
11
+ import { inspectionFragment, propInspectionChannels, PropInspectionGeometry, } from './propInspection.js';
11
12
  export const MAX_CASCADES = 4; // must match gpuShadow's
12
13
  export const MAX_PROP_POINT_LIGHTS = 8;
13
14
  const POINT_LIGHT_WORDS = 4 + MAX_PROP_POINT_LIGHTS * 16;
14
15
  const PROP_UNIFORM_WORDS = 72 + 16 * MAX_CASCADES + 8 + POINT_LIGHT_WORDS;
15
- const SHADER = /* wgsl */ `
16
+ const drawShader = (inspect) => /* wgsl */ `
16
17
  // wind = (dirX, dirZ, amplitude, phase) — sway, amplitude scaled by the world's air density
17
18
  // surf = (grainScale, grainDepth, roughness, emissive)
18
19
  // grade = (hueShift, saturation, valueLift, unused)
@@ -55,7 +56,7 @@ struct Mat { mat: vec4<f32>, coat: vec4<f32>, maps: vec4<f32>, surface: vec4<f32
55
56
  // Renderer-wide linear HDR equirect (PropLook.environment). A 1×1 dummy when unset, and then never
56
57
  // sampled: look.z and look.w are forced to 0 on the CPU without it.
57
58
  @group(1) @binding(2) var sceneEnvTex: texture_2d<f32>;
58
- struct VO { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) n: vec3<f32>, @location(2) c: vec3<f32>, @location(3) l: vec3<f32>, @location(4) uv: vec2<f32> };
59
+ struct VO { @builtin(position) pos: vec4<f32>, @location(0) world: vec3<f32>, @location(1) n: vec3<f32>, @location(2) c: vec3<f32>, @location(3) l: vec3<f32>, @location(4) uv: vec2<f32> ${inspect ? ', @location(5) @interpolate(linear) bary:vec3<f32>' : ''} };
59
60
  // WIND SWAY. Displacement is weighted by height above the instance's own anchor, so a trunk stays
60
61
  // planted while the canopy moves, and the phase is offset by world position so a grove never sways
61
62
  // in unison. Amplitude comes from the world's air density: an airless world is perfectly, eerily
@@ -117,7 +118,7 @@ fn deformNormal(n: vec3<f32>, py: f32, d0: vec4<f32>, d1: vec4<f32>, d2: vec4<f3
117
118
  let ca = cos(a); let sa = sin(a);
118
119
  return normalize(vec3(v.x * ca - v.z * sa, v.y, v.x * sa + v.z * ca));
119
120
  }
120
- @vertex fn vs(@location(0) p: vec3<f32>, @location(1) nrm: vec3<f32>, @location(2) col: vec3<f32>, @location(3) uv: vec2<f32>, @builtin(instance_index) ii: u32) -> VO {
121
+ @vertex fn vs(@location(0) p: vec3<f32>, @location(1) nrm: vec3<f32>, @location(2) col: vec3<f32>, @location(3) uv: vec2<f32>, @builtin(instance_index) ii: u32 ${inspect ? ', @builtin(vertex_index) vi:u32' : ''}) -> VO {
121
122
  let inst = models[ii];
122
123
  let m = inst.m;
123
124
  let lp = deformLocal(p, inst.d0, inst.d1, inst.d2);
@@ -127,7 +128,9 @@ fn deformNormal(n: vec3<f32>, py: f32, d0: vec4<f32>, d1: vec4<f32>, d2: vec4<f3
127
128
  let sc = length(m[0].xyz);
128
129
  world = sway(lp, world, upv, sc * inst.d2.w);
129
130
  var o: VO; o.pos = u.mvp * vec4(world, 1.0); o.world = world; o.n = (m * vec4(ln, 0.0)).xyz;
130
- o.c = col * inst.d2.y * inst.d3.rgb; o.l = lp; o.uv = uv; return o;
131
+ o.c = col * inst.d2.y * inst.d3.rgb; o.l = lp; o.uv = uv;
132
+ ${inspect ? 'o.bary = vec3<f32>(f32(vi%3u==0u),f32(vi%3u==1u),f32(vi%3u==2u));' : ''}
133
+ return o;
131
134
  }
132
135
  // same fixed directional FILL as the terrain cubes (gpuShadow.fixedFill) so plants/rocks read the same 3D shape,
133
136
  // day or night, under one shared light. up.w > 0.5 signals a valid local-up was supplied (else fall back to flat).
@@ -329,12 +332,35 @@ fn ggx(nh: f32, nv: f32, nl: f32, rough: f32) -> f32 {
329
332
  let G = (nv / max(nv * (1.0-k) + k, 0.0001)) * (nl / max(nl * (1.0-k) + k, 0.0001));
330
333
  return D * G / max(4.0 * nv * nl, 0.0001);
331
334
  }
335
+ // Smith-GGX for the transparent dielectric layer. Widen the microfacet
336
+ // distribution by the normal variance across one pixel to retain unresolved
337
+ // key-light reflections when a curved surface becomes small on screen.
338
+ fn directCoat(n:vec3<f32>, nh:f32, nv:f32, nl:f32, rough:f32) -> f32 {
339
+ let dx = dpdx(n); let dy = dpdy(n);
340
+ let variance = min(0.18, 0.25*(dot(dx,dx)+dot(dy,dy)));
341
+ let a2 = min(1.0, pow(rough,4.0)+variance);
342
+ let denom = (1.0-nh*nh) + nh*nh*a2;
343
+ let D = a2 / max(3.14159265*denom*denom,1e-12);
344
+ let gv = nl*sqrt(a2+(1.0-a2)*nv*nv);
345
+ let gl = nv*sqrt(a2+(1.0-a2)*nl*nl);
346
+ return nl*D*0.5/max(gv+gl,0.0001);
347
+ }
332
348
  fn srgbToLinear(c: vec3<f32>) -> vec3<f32> {
333
349
  return select(c / 12.92, pow((c + 0.055) / 1.055, vec3(2.4)), c > vec3(0.04045));
334
350
  }
335
351
  fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
336
352
  return select(c * 12.92, 1.055 * pow(max(c, vec3(0.0)), vec3(1.0/2.4)) - 0.055, c > vec3(0.0031308));
337
353
  }
354
+ fn effectiveRoughness(orm:vec3<f32>) -> f32 {
355
+ return clamp(mtl.mat.z * select(1.0,orm.g,mtl.maps.y>0.5), 0.045, 1.0);
356
+ }
357
+ fn effectiveAO(orm:vec3<f32>) -> f32 { return select(1.0,orm.r,mtl.maps.y>0.5); }
358
+ fn surfaceOpacity(n:vec3<f32>, world:vec3<f32>) -> f32 {
359
+ if (mtl.optics.x <= 0.0) { return mtl.mat.x; }
360
+ let nv = clamp(abs(dot(n,normalize(u.cam.xyz-world))),0.0,1.0);
361
+ let F = 0.04 + 0.96*pow(1.0-nv,5.0);
362
+ return clamp(1.0-mtl.optics.x*(1.0-F),0.0,1.0);
363
+ }
338
364
  @fragment fn fs(i: VO) -> @location(0) vec4<f32> {
339
365
  var N = normalize(i.n);
340
366
  let materialUV=i.uv*mtl.maps.w;
@@ -371,6 +397,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
371
397
  base = base * (1.0 - u.surf.y * 0.5 + u.surf.y * grain);
372
398
  }
373
399
  base = hsvShift(base, u.grade.x, max(u.grade.y, 0.0), max(u.grade.z, 0.0));
400
+ ${inspect ? inspectionFragment : ''}
374
401
  let spec = u.surf.z;
375
402
  var lit = base * (u.amb.xyz * ff + d * u.sunCol.xyz * sh * (1.0 + spec * 0.6))
376
403
  + base * u.surf.w; // emissive — bioluminescence on dark worlds
@@ -385,7 +412,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
385
412
  let nl = max(dot(N, L), 0.0);
386
413
  let nh = max(dot(N, H), 0.0);
387
414
  let vh = max(dot(V, H), 0.0);
388
- let rough = clamp(mtl.mat.z * select(1.0,orm.g,mtl.maps.y>0.5), 0.045, 1.0);
415
+ let rough = effectiveRoughness(orm);
389
416
  let metal = clamp(mtl.mat.w * select(1.0,orm.b,mtl.maps.y>0.5), 0.0, 1.0);
390
417
  // Vertex/instance tints are display sRGB, while albedo textures use sRGB GPU
391
418
  // formats (and arrive decoded). Legacy props keep their historical path.
@@ -410,10 +437,16 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
410
437
  ambient=textureSampleLevel(sceneEnvTex,albedoSamp,equirectUV(N),u.look2.w).rgb*u.look.z;
411
438
  skyOcclusion=1.0;
412
439
  }
413
- let ao=select(1.0,orm.r,mtl.maps.y>0.5);
440
+ let ao=effectiveAO(orm);
414
441
  let diffuse = albedo * ambient * skyOcclusion * (1.0-metal)*ao;
415
442
  let coatF = fresnel(vec3(0.04),nv) * mtl.coat.x;
416
- let coating = environment(R,mtl.surface.z) * coatF * mtl.coat.y;
443
+ var coating = environment(R,mtl.surface.z) * coatF * mtl.coat.y;
444
+ if (mtl.optics.w > 0.0 && mtl.coat.x > 0.0) {
445
+ // A wet film reflects the directional key as well as the environment.
446
+ // The key follows incident radiance and receiver shadows in world space.
447
+ let amount = mtl.coat.x*mtl.optics.w*directCoat(N,nh,nv,nl,mtl.surface.z);
448
+ coating += fresnel(vec3<f32>(0.04),vh)*amount*u.sunCol.xyz*sh*2.5;
449
+ }
417
450
  // A soft fiber lobe distinguishes woven upholstery from a glossy dielectric.
418
451
  // Neutral defaults preserve the existing PBR response in other consumers.
419
452
  let sheen = albedo * mtl.surface.y * (u.sunCol.xyz * nl * sh * pow(1.0-vh,5.0)
@@ -463,14 +496,11 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
463
496
  let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
464
497
  // mtl.mat.x is the model's opacity, 1 for every model that does not set it — the same constant
465
498
  // this returned before, so the opaque pipeline draws exactly what it always did.
466
- var opacity = mtl.mat.x;
499
+ let opacity = surfaceOpacity(N,i.world);
467
500
  // Thin glazing reflects more at grazing angles and transmits the rendered
468
501
  // scene at normal incidence. This does not approximate thick-glass refraction.
469
502
  if (mtl.optics.x > 0.0) {
470
503
  let V = normalize(u.cam.xyz-i.world);
471
- let nv = clamp(abs(dot(N,V)),0.0,1.0);
472
- let F = 0.04 + 0.96*pow(1.0-nv,5.0);
473
- opacity = clamp(1.0-mtl.optics.x*(1.0-F),0.0,1.0);
474
504
  let radiance = environment(reflect(-V,N),max(mtl.mat.z,0.045));
475
505
  lit = toneMap(radiance);
476
506
  }
@@ -629,6 +659,9 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
629
659
  `;
630
660
  export class GpuPropsRenderer {
631
661
  device;
662
+ inspection;
663
+ srgbTarget;
664
+ inspectionGeometry;
632
665
  pipe;
633
666
  /**
634
667
  * The same pipeline with source-alpha blending and depth writes off, for models with `alpha < 1`.
@@ -786,7 +819,7 @@ export class GpuPropsRenderer {
786
819
  }
787
820
  constructor(device, format, options = {}) {
788
821
  this.device = device;
789
- const { sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', ...look } = options;
822
+ const { inspection = false, sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', ...look } = options;
790
823
  // Runtime guard for untyped callers: WebGPU only guarantees 1 and 4.
791
824
  if (![1, 4].includes(sampleCount))
792
825
  throw new RangeError(`Unsupported prop sampleCount ${String(sampleCount)}`);
@@ -795,7 +828,9 @@ export class GpuPropsRenderer {
795
828
  this.sampleCount = sampleCount;
796
829
  this.depth = depth;
797
830
  this.setLook(look);
798
- const sm = device.createShaderModule({ code: SHADER });
831
+ this.inspection = inspection;
832
+ this.srgbTarget = format.endsWith('-srgb');
833
+ const sm = device.createShaderModule({ code: drawShader(inspection) });
799
834
  // The three groups the SHADER declares, written out so BOTH render pipelines share them. Under
800
835
  // `layout: 'auto'` each pipeline would mint its own and the bind groups built elsewhere in this
801
836
  // class could only ever be used by one of them.
@@ -1078,6 +1113,7 @@ export class GpuPropsRenderer {
1078
1113
  reflectionStrength: 5,
1079
1114
  specularIntensity: 12,
1080
1115
  clearcoatRoughness: 14,
1116
+ directClearcoat: 23,
1081
1117
  metalFlakes: 16,
1082
1118
  flakeScale: 17,
1083
1119
  pearlStrength: 18,
@@ -1107,6 +1143,9 @@ export class GpuPropsRenderer {
1107
1143
  this.device.queue.writeBuffer(packed.matBuf, 0, packed.materialValues);
1108
1144
  }
1109
1145
  setModels(meshes) {
1146
+ this.inspectionGeometry?.destroy();
1147
+ if (this.inspection)
1148
+ this.inspectionGeometry = new PropInspectionGeometry(this.device, meshes);
1110
1149
  const dev = this.device, U = GPUBufferUsage;
1111
1150
  for (const p of this.models) {
1112
1151
  // Geometry is no longer per model — the shared arena is freed once, below, after the new one
@@ -1192,7 +1231,7 @@ export class GpuPropsRenderer {
1192
1231
  dev.queue.writeBuffer(buffer, offset, data);
1193
1232
  };
1194
1233
  let vCursor = 0, iCursor = 0;
1195
- this.models = meshes.map((m) => {
1234
+ this.models = meshes.map((m, modelIndex) => {
1196
1235
  const verts = m.positions.length / 3;
1197
1236
  const baseVertex = vCursor, firstIndex = iCursor;
1198
1237
  upload(this.geo.pos, baseVertex * 12, m.positions);
@@ -1238,11 +1277,11 @@ export class GpuPropsRenderer {
1238
1277
  Math.max(0, Math.min(1, m.metalFlakes ?? 0)),
1239
1278
  Math.max(1, m.flakeScale ?? 320),
1240
1279
  Math.max(0, Math.min(1, m.pearlStrength ?? 0)),
1241
- 0,
1280
+ modelIndex,
1242
1281
  Math.max(0, Math.min(1, m.transmission ?? 0)),
1243
1282
  m.unlit ? 1 : m.glow ? 2 : 0,
1244
1283
  Math.max(0, Math.min(1, m.diffuseTransmission ?? 0)),
1245
- 0,
1284
+ Math.max(0, Math.min(1, m.directClearcoat ?? 0)),
1246
1285
  ]);
1247
1286
  dev.queue.writeBuffer(matBuf, 0, materialValues);
1248
1287
  return {
@@ -1559,7 +1598,10 @@ export class GpuPropsRenderer {
1559
1598
  frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, up,
1560
1599
  // Optional sun shadow-receive: pass the terrain's lightVP + shadow-map view + comparison sampler and props sit
1561
1600
  // in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
1562
- lightVP, shadowView, shadowSamp) {
1601
+ lightVP, shadowView, shadowSamp, channel = 'lit') {
1602
+ if (channel !== 'lit' && !this.inspection)
1603
+ throw new Error('Prop inspection must be enabled at construction.');
1604
+ const topology = channel === 'wireframe' || channel === 'density';
1563
1605
  const receive = !!(lightVP && shadowView && shadowSamp);
1564
1606
  const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
1565
1607
  const u = new Float32Array(PROP_UNIFORM_WORDS);
@@ -1577,7 +1619,12 @@ export class GpuPropsRenderer {
1577
1619
  const w = this.world;
1578
1620
  u.set([w.windDir[0], w.windDir[1], w.windAmp, this.phase], 56);
1579
1621
  u.set([w.grainScale, w.grainDepth, w.roughness, w.emissive], 60);
1580
- u.set([w.hueShift, w.saturation, w.valueLift, 0], 64);
1622
+ u.set([
1623
+ w.hueShift,
1624
+ w.saturation,
1625
+ w.valueLift,
1626
+ this.inspection ? propInspectionChannels.indexOf(channel) : 0,
1627
+ ], 64);
1581
1628
  // csm.x = how many cascades are live; the matrices follow. Zero when not receiving, so the
1582
1629
  // selection loop exits at once and returns fully lit — the old no-shadow path exactly.
1583
1630
  u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
@@ -1600,6 +1647,8 @@ export class GpuPropsRenderer {
1600
1647
  Math.max(0, Math.min(envMaxLod, look.environmentDiffuseLod ?? envMaxLod - 3)),
1601
1648
  ], lookAt);
1602
1649
  u.set(this.pointLightWords, lookAt + 8);
1650
+ if (this.inspection)
1651
+ u[lookAt + 9] = Number(this.srgbTarget);
1603
1652
  this.device.queue.writeBuffer(this.uBuf, 0, u);
1604
1653
  // group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
1605
1654
  const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
@@ -1629,7 +1678,9 @@ export class GpuPropsRenderer {
1629
1678
  });
1630
1679
  rp.setBindGroup(1, this.shadowBG);
1631
1680
  // The arena, bound once for every model in both loops below.
1632
- if (this.geo) {
1681
+ if (topology)
1682
+ this.inspectionGeometry?.bind(rp);
1683
+ else if (this.geo) {
1633
1684
  rp.setVertexBuffer(0, this.geo.pos);
1634
1685
  rp.setVertexBuffer(1, this.geo.nrm);
1635
1686
  rp.setVertexBuffer(2, this.geo.col);
@@ -1643,7 +1694,10 @@ export class GpuPropsRenderer {
1643
1694
  rp.setBindGroup(0, bg);
1644
1695
  rp.setBindGroup(2, mdl.matBG);
1645
1696
  // Geometry is bound ONCE for the whole pass; a model is a range inside it.
1646
- rp.drawIndexed(mdl.idxCount, this.counts[m], mdl.firstIndex, mdl.baseVertex);
1697
+ if (topology)
1698
+ rp.draw(mdl.idxCount, this.counts[m], mdl.firstIndex);
1699
+ else
1700
+ rp.drawIndexed(mdl.idxCount, this.counts[m], mdl.firstIndex, mdl.baseVertex);
1647
1701
  };
1648
1702
  // OPAQUE FIRST, then the blended models — the whole point of the second pass. A translucent
1649
1703
  // crystal drawn before an opaque rock composites against whatever the sky left in the target and
@@ -1654,9 +1708,10 @@ export class GpuPropsRenderer {
1654
1708
  for (const blended of [false, true]) {
1655
1709
  for (let m = 0; m < this.models.length; m++) {
1656
1710
  const model = this.models[m];
1657
- if (model.alpha < 1 !== blended || this.counts[m] === 0)
1711
+ const clay = channel === 'clay';
1712
+ if ((model.alpha < 1 && !clay) !== blended || this.counts[m] === 0)
1658
1713
  continue;
1659
- const pipeline = this.drawPipeline(blended, model.cullMode, model.depthWrite);
1714
+ const pipeline = this.drawPipeline(blended, model.cullMode, clay || model.depthWrite);
1660
1715
  if (pipeline !== activePipeline) {
1661
1716
  rp.setPipeline(pipeline);
1662
1717
  activePipeline = pipeline;
@@ -0,0 +1,15 @@
1
+ import type { PropMesh } from './gpuProps.js';
2
+ export declare const propInspectionChannels: readonly ["lit", "albedo", "normal", "roughness", "emissive", "ao", "clay", "wireframe", "density", "uv", "batch"];
3
+ export type PropInspectionChannel = (typeof propInspectionChannels)[number];
4
+ export declare const inspectionFragment = "\n if (u.grade.w > 0.5) {\n let channel = u32(u.grade.w);\n let opacity = surfaceOpacity(N,i.world);\n let albedo = select(base, srgbToLinear(i.c) * tex.rgb, mtl.mat.z >= 0.0);\n var display = vec3(0.0);\n switch channel {\n case 1u: { return vec4(select(albedo, tex.rgb, mtl.optics.y == 1.0),opacity); }\n case 2u: {\n let backTissue = mtl.optics.z>0.0 && dot(i.n,u.cam.xyz-i.world)<0.0;\n display=select(N,-N,backTissue)*0.5+0.5;\n }\n case 3u: { display=vec3(select(1.0,effectiveRoughness(orm),mtl.mat.z>=0.0)); }\n case 4u: {\n var emission = select(vec3(0.0), base*u.surf.w, mtl.mat.z<0.0);\n if (mtl.optics.y > 1.5) { emission = tex.rgb * i.c; }\n return vec4(emission,opacity);\n }\n case 5u: { display=vec3(select(1.0,effectiveAO(orm),mtl.mat.z>=0.0)); }\n case 6u: {\n // White diffuse clay shows the smooth geometry normals alone.\n let geoN = normalize(i.n);\n let nl = max(0.0,dot(geoN,-normalize(u.sun.xyz)));\n var ambient = u.amb.xyz * (0.6+0.4*max(geoN.y,0.0));\n if (u.look.z>0.0) { ambient=textureSampleLevel(sceneEnvTex,albedoSamp,equirectUV(geoN),u.look2.w).rgb*u.look.z; }\n return vec4(toneMap(vec3(0.76)*(ambient+u.sunCol.xyz*nl*sh*2.5/3.14159265)),1.0);\n }\n case 7u: {\n let dx=dpdx(i.bary); let dy=dpdy(i.bary);\n let dist=i.bary / max(sqrt(dx*dx+dy*dy),vec3(1e-6));\n let edge=1.0-smoothstep(0.0,1.15,min(dist.x,min(dist.y,dist.z)));\n display=mix(vec3(0.12,0.15,0.18),vec3(0.87,0.96,1.0),edge);\n }\n case 8u: {\n // The determinant maps one screen pixel to a fraction of the unit barycentric triangle (area 0.5).\n // A <=1 px\u00B2 triangle is hot, >=256 px\u00B2 cool. Area follows the actual posed projection.\n let dx=dpdx(i.bary.xy); let dy=dpdy(i.bary.xy);\n let area=0.5/max(abs(dx.x*dy.y-dx.y*dy.x),1e-10);\n let t=clamp(log2(max(area,1.0))/8.0,0.0,1.0);\n display=mix(mix(vec3(1.0,0.22,0.12),vec3(1.0,0.88,0.23),clamp(t*2.0,0.0,1.0)),\n vec3(0.12,0.36,0.87),clamp(t*2.0-1.0,0.0,1.0));\n }\n case 9u: {\n let uv=i.uv * mtl.surface.w;\n let q=uv*8.0;\n let w=clamp(fwidth(q),vec2(0.001),vec2(1.0));\n let band=clamp((abs(fract(q*0.5+0.25)-0.5)*4.0-1.0)/w,vec2(-1.0),vec2(1.0));\n let checker = 0.5+0.5*band.x*band.y;\n display=mix(vec3(0.14,0.18,0.24),vec3(0.77,0.85,0.92),checker);\n }\n default: {\n display = vec3(0.55)+0.37*cos(6.2831853*(mtl.paint.w*0.618034+vec3(0.0,0.34,0.66)));\n }\n }\n return vec4(select(display,srgbToLinear(display),u.pointMeta.y>0.5),opacity);\n }\n";
5
+ export declare class PropInspectionGeometry {
6
+ private readonly device;
7
+ private readonly meshes;
8
+ private streams;
9
+ constructor(device: GPUDevice, meshes: readonly PropMesh[]);
10
+ /** Only topology views need unshared triangle corners. Other channels keep
11
+ * using the indexed production geometry. vertex_index % 3 gives a corner
12
+ * once indexed vertex streams have been expanded in original draw order. */
13
+ bind(pass: GPURenderPassEncoder): void;
14
+ destroy(): void;
15
+ }
@@ -0,0 +1,119 @@
1
+ export const propInspectionChannels = [
2
+ 'lit',
3
+ 'albedo',
4
+ 'normal',
5
+ 'roughness',
6
+ 'emissive',
7
+ 'ao',
8
+ 'clay',
9
+ 'wireframe',
10
+ 'density',
11
+ 'uv',
12
+ 'batch',
13
+ ];
14
+ // Shared by the diagnostic-only version of the production prop shader. The
15
+ // regular renderer compiles no additional varyings, derivatives or branches.
16
+ export const inspectionFragment = /* wgsl */ `
17
+ if (u.grade.w > 0.5) {
18
+ let channel = u32(u.grade.w);
19
+ let opacity = surfaceOpacity(N,i.world);
20
+ let albedo = select(base, srgbToLinear(i.c) * tex.rgb, mtl.mat.z >= 0.0);
21
+ var display = vec3(0.0);
22
+ switch channel {
23
+ case 1u: { return vec4(select(albedo, tex.rgb, mtl.optics.y == 1.0),opacity); }
24
+ case 2u: {
25
+ let backTissue = mtl.optics.z>0.0 && dot(i.n,u.cam.xyz-i.world)<0.0;
26
+ display=select(N,-N,backTissue)*0.5+0.5;
27
+ }
28
+ case 3u: { display=vec3(select(1.0,effectiveRoughness(orm),mtl.mat.z>=0.0)); }
29
+ case 4u: {
30
+ var emission = select(vec3(0.0), base*u.surf.w, mtl.mat.z<0.0);
31
+ if (mtl.optics.y > 1.5) { emission = tex.rgb * i.c; }
32
+ return vec4(emission,opacity);
33
+ }
34
+ case 5u: { display=vec3(select(1.0,effectiveAO(orm),mtl.mat.z>=0.0)); }
35
+ case 6u: {
36
+ // White diffuse clay shows the smooth geometry normals alone.
37
+ let geoN = normalize(i.n);
38
+ let nl = max(0.0,dot(geoN,-normalize(u.sun.xyz)));
39
+ var ambient = u.amb.xyz * (0.6+0.4*max(geoN.y,0.0));
40
+ if (u.look.z>0.0) { ambient=textureSampleLevel(sceneEnvTex,albedoSamp,equirectUV(geoN),u.look2.w).rgb*u.look.z; }
41
+ return vec4(toneMap(vec3(0.76)*(ambient+u.sunCol.xyz*nl*sh*2.5/3.14159265)),1.0);
42
+ }
43
+ case 7u: {
44
+ let dx=dpdx(i.bary); let dy=dpdy(i.bary);
45
+ let dist=i.bary / max(sqrt(dx*dx+dy*dy),vec3(1e-6));
46
+ let edge=1.0-smoothstep(0.0,1.15,min(dist.x,min(dist.y,dist.z)));
47
+ display=mix(vec3(0.12,0.15,0.18),vec3(0.87,0.96,1.0),edge);
48
+ }
49
+ case 8u: {
50
+ // The determinant maps one screen pixel to a fraction of the unit barycentric triangle (area 0.5).
51
+ // A <=1 px² triangle is hot, >=256 px² cool. Area follows the actual posed projection.
52
+ let dx=dpdx(i.bary.xy); let dy=dpdy(i.bary.xy);
53
+ let area=0.5/max(abs(dx.x*dy.y-dx.y*dy.x),1e-10);
54
+ let t=clamp(log2(max(area,1.0))/8.0,0.0,1.0);
55
+ display=mix(mix(vec3(1.0,0.22,0.12),vec3(1.0,0.88,0.23),clamp(t*2.0,0.0,1.0)),
56
+ vec3(0.12,0.36,0.87),clamp(t*2.0-1.0,0.0,1.0));
57
+ }
58
+ case 9u: {
59
+ let uv=i.uv * mtl.surface.w;
60
+ let q=uv*8.0;
61
+ let w=clamp(fwidth(q),vec2(0.001),vec2(1.0));
62
+ let band=clamp((abs(fract(q*0.5+0.25)-0.5)*4.0-1.0)/w,vec2(-1.0),vec2(1.0));
63
+ let checker = 0.5+0.5*band.x*band.y;
64
+ display=mix(vec3(0.14,0.18,0.24),vec3(0.77,0.85,0.92),checker);
65
+ }
66
+ default: {
67
+ display = vec3(0.55)+0.37*cos(6.2831853*(mtl.paint.w*0.618034+vec3(0.0,0.34,0.66)));
68
+ }
69
+ }
70
+ return vec4(select(display,srgbToLinear(display),u.pointMeta.y>0.5),opacity);
71
+ }
72
+ `;
73
+ export class PropInspectionGeometry {
74
+ device;
75
+ meshes;
76
+ streams = [];
77
+ constructor(device, meshes) {
78
+ this.device = device;
79
+ this.meshes = meshes;
80
+ }
81
+ /** Only topology views need unshared triangle corners. Other channels keep
82
+ * using the indexed production geometry. vertex_index % 3 gives a corner
83
+ * once indexed vertex streams have been expanded in original draw order. */
84
+ bind(pass) {
85
+ if (!this.streams.length) {
86
+ const count = this.meshes.reduce((n, mesh) => n + mesh.indices.length, 0);
87
+ for (const [key, size] of [
88
+ ['positions', 3],
89
+ ['normals', 3],
90
+ ['colors', 3],
91
+ ['uvs', 2],
92
+ ]) {
93
+ const values = new Float32Array(count * size);
94
+ let at = 0;
95
+ for (const mesh of this.meshes) {
96
+ const source = mesh[key];
97
+ for (const index of mesh.indices) {
98
+ for (let c = 0; c < size; c++)
99
+ values[at++] = source?.[index * size + c] ?? 0;
100
+ }
101
+ }
102
+ const buffer = this.device.createBuffer({
103
+ size: Math.max(4, values.byteLength),
104
+ usage: GPUBufferUsage.VERTEX | GPUBufferUsage.COPY_DST,
105
+ });
106
+ if (values.length)
107
+ this.device.queue.writeBuffer(buffer, 0, values);
108
+ this.streams.push(buffer);
109
+ }
110
+ }
111
+ for (const [slot, buffer] of this.streams.entries())
112
+ pass.setVertexBuffer(slot, buffer);
113
+ }
114
+ destroy() {
115
+ for (const buffer of this.streams)
116
+ buffer.destroy();
117
+ this.streams = [];
118
+ }
119
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ugly-game",
3
- "version": "0.15.72",
3
+ "version": "0.15.74",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {