ugly-game 0.15.73 → 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
|
|
@@ -284,6 +289,9 @@ export interface PropInstance {
|
|
|
284
289
|
}
|
|
285
290
|
export declare class GpuPropsRenderer {
|
|
286
291
|
private device;
|
|
292
|
+
private inspection;
|
|
293
|
+
private srgbTarget;
|
|
294
|
+
private inspectionGeometry?;
|
|
287
295
|
private pipe;
|
|
288
296
|
/**
|
|
289
297
|
* The same pipeline with source-alpha blending and depth writes off, for models with `alpha < 1`.
|
|
@@ -455,5 +463,5 @@ export declare class GpuPropsRenderer {
|
|
|
455
463
|
* a single shared uniform written once per cascade during encoding does not work — see `lightBufs`.
|
|
456
464
|
*/
|
|
457
465
|
shadowPass(pass: GPURenderPassEncoder, lightVP: Float32Array, cascade?: number): void;
|
|
458
|
-
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;
|
|
459
467
|
}
|
package/dist/assets/gpuProps.js
CHANGED
|
@@ -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
|
|
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;
|
|
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).
|
|
@@ -348,6 +351,16 @@ fn srgbToLinear(c: vec3<f32>) -> vec3<f32> {
|
|
|
348
351
|
fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
|
|
349
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));
|
|
350
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
|
+
}
|
|
351
364
|
@fragment fn fs(i: VO) -> @location(0) vec4<f32> {
|
|
352
365
|
var N = normalize(i.n);
|
|
353
366
|
let materialUV=i.uv*mtl.maps.w;
|
|
@@ -384,6 +397,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
|
|
|
384
397
|
base = base * (1.0 - u.surf.y * 0.5 + u.surf.y * grain);
|
|
385
398
|
}
|
|
386
399
|
base = hsvShift(base, u.grade.x, max(u.grade.y, 0.0), max(u.grade.z, 0.0));
|
|
400
|
+
${inspect ? inspectionFragment : ''}
|
|
387
401
|
let spec = u.surf.z;
|
|
388
402
|
var lit = base * (u.amb.xyz * ff + d * u.sunCol.xyz * sh * (1.0 + spec * 0.6))
|
|
389
403
|
+ base * u.surf.w; // emissive — bioluminescence on dark worlds
|
|
@@ -398,7 +412,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
|
|
|
398
412
|
let nl = max(dot(N, L), 0.0);
|
|
399
413
|
let nh = max(dot(N, H), 0.0);
|
|
400
414
|
let vh = max(dot(V, H), 0.0);
|
|
401
|
-
let rough =
|
|
415
|
+
let rough = effectiveRoughness(orm);
|
|
402
416
|
let metal = clamp(mtl.mat.w * select(1.0,orm.b,mtl.maps.y>0.5), 0.0, 1.0);
|
|
403
417
|
// Vertex/instance tints are display sRGB, while albedo textures use sRGB GPU
|
|
404
418
|
// formats (and arrive decoded). Legacy props keep their historical path.
|
|
@@ -423,7 +437,7 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
|
|
|
423
437
|
ambient=textureSampleLevel(sceneEnvTex,albedoSamp,equirectUV(N),u.look2.w).rgb*u.look.z;
|
|
424
438
|
skyOcclusion=1.0;
|
|
425
439
|
}
|
|
426
|
-
let ao=
|
|
440
|
+
let ao=effectiveAO(orm);
|
|
427
441
|
let diffuse = albedo * ambient * skyOcclusion * (1.0-metal)*ao;
|
|
428
442
|
let coatF = fresnel(vec3(0.04),nv) * mtl.coat.x;
|
|
429
443
|
var coating = environment(R,mtl.surface.z) * coatF * mtl.coat.y;
|
|
@@ -482,14 +496,11 @@ fn linearToSrgb(c: vec3<f32>) -> vec3<f32> {
|
|
|
482
496
|
let f = clamp(1.0 - exp(-dist * u.fog.w), 0.0, 1.0);
|
|
483
497
|
// mtl.mat.x is the model's opacity, 1 for every model that does not set it — the same constant
|
|
484
498
|
// this returned before, so the opaque pipeline draws exactly what it always did.
|
|
485
|
-
|
|
499
|
+
let opacity = surfaceOpacity(N,i.world);
|
|
486
500
|
// Thin glazing reflects more at grazing angles and transmits the rendered
|
|
487
501
|
// scene at normal incidence. This does not approximate thick-glass refraction.
|
|
488
502
|
if (mtl.optics.x > 0.0) {
|
|
489
503
|
let V = normalize(u.cam.xyz-i.world);
|
|
490
|
-
let nv = clamp(abs(dot(N,V)),0.0,1.0);
|
|
491
|
-
let F = 0.04 + 0.96*pow(1.0-nv,5.0);
|
|
492
|
-
opacity = clamp(1.0-mtl.optics.x*(1.0-F),0.0,1.0);
|
|
493
504
|
let radiance = environment(reflect(-V,N),max(mtl.mat.z,0.045));
|
|
494
505
|
lit = toneMap(radiance);
|
|
495
506
|
}
|
|
@@ -648,6 +659,9 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
|
648
659
|
`;
|
|
649
660
|
export class GpuPropsRenderer {
|
|
650
661
|
device;
|
|
662
|
+
inspection;
|
|
663
|
+
srgbTarget;
|
|
664
|
+
inspectionGeometry;
|
|
651
665
|
pipe;
|
|
652
666
|
/**
|
|
653
667
|
* The same pipeline with source-alpha blending and depth writes off, for models with `alpha < 1`.
|
|
@@ -805,7 +819,7 @@ export class GpuPropsRenderer {
|
|
|
805
819
|
}
|
|
806
820
|
constructor(device, format, options = {}) {
|
|
807
821
|
this.device = device;
|
|
808
|
-
const { sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', ...look } = options;
|
|
822
|
+
const { inspection = false, sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', ...look } = options;
|
|
809
823
|
// Runtime guard for untyped callers: WebGPU only guarantees 1 and 4.
|
|
810
824
|
if (![1, 4].includes(sampleCount))
|
|
811
825
|
throw new RangeError(`Unsupported prop sampleCount ${String(sampleCount)}`);
|
|
@@ -814,7 +828,9 @@ export class GpuPropsRenderer {
|
|
|
814
828
|
this.sampleCount = sampleCount;
|
|
815
829
|
this.depth = depth;
|
|
816
830
|
this.setLook(look);
|
|
817
|
-
|
|
831
|
+
this.inspection = inspection;
|
|
832
|
+
this.srgbTarget = format.endsWith('-srgb');
|
|
833
|
+
const sm = device.createShaderModule({ code: drawShader(inspection) });
|
|
818
834
|
// The three groups the SHADER declares, written out so BOTH render pipelines share them. Under
|
|
819
835
|
// `layout: 'auto'` each pipeline would mint its own and the bind groups built elsewhere in this
|
|
820
836
|
// class could only ever be used by one of them.
|
|
@@ -1127,6 +1143,9 @@ export class GpuPropsRenderer {
|
|
|
1127
1143
|
this.device.queue.writeBuffer(packed.matBuf, 0, packed.materialValues);
|
|
1128
1144
|
}
|
|
1129
1145
|
setModels(meshes) {
|
|
1146
|
+
this.inspectionGeometry?.destroy();
|
|
1147
|
+
if (this.inspection)
|
|
1148
|
+
this.inspectionGeometry = new PropInspectionGeometry(this.device, meshes);
|
|
1130
1149
|
const dev = this.device, U = GPUBufferUsage;
|
|
1131
1150
|
for (const p of this.models) {
|
|
1132
1151
|
// Geometry is no longer per model — the shared arena is freed once, below, after the new one
|
|
@@ -1212,7 +1231,7 @@ export class GpuPropsRenderer {
|
|
|
1212
1231
|
dev.queue.writeBuffer(buffer, offset, data);
|
|
1213
1232
|
};
|
|
1214
1233
|
let vCursor = 0, iCursor = 0;
|
|
1215
|
-
this.models = meshes.map((m) => {
|
|
1234
|
+
this.models = meshes.map((m, modelIndex) => {
|
|
1216
1235
|
const verts = m.positions.length / 3;
|
|
1217
1236
|
const baseVertex = vCursor, firstIndex = iCursor;
|
|
1218
1237
|
upload(this.geo.pos, baseVertex * 12, m.positions);
|
|
@@ -1258,7 +1277,7 @@ export class GpuPropsRenderer {
|
|
|
1258
1277
|
Math.max(0, Math.min(1, m.metalFlakes ?? 0)),
|
|
1259
1278
|
Math.max(1, m.flakeScale ?? 320),
|
|
1260
1279
|
Math.max(0, Math.min(1, m.pearlStrength ?? 0)),
|
|
1261
|
-
|
|
1280
|
+
modelIndex,
|
|
1262
1281
|
Math.max(0, Math.min(1, m.transmission ?? 0)),
|
|
1263
1282
|
m.unlit ? 1 : m.glow ? 2 : 0,
|
|
1264
1283
|
Math.max(0, Math.min(1, m.diffuseTransmission ?? 0)),
|
|
@@ -1579,7 +1598,10 @@ export class GpuPropsRenderer {
|
|
|
1579
1598
|
frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, up,
|
|
1580
1599
|
// Optional sun shadow-receive: pass the terrain's lightVP + shadow-map view + comparison sampler and props sit
|
|
1581
1600
|
// in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
|
|
1582
|
-
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';
|
|
1583
1605
|
const receive = !!(lightVP && shadowView && shadowSamp);
|
|
1584
1606
|
const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
|
|
1585
1607
|
const u = new Float32Array(PROP_UNIFORM_WORDS);
|
|
@@ -1597,7 +1619,12 @@ export class GpuPropsRenderer {
|
|
|
1597
1619
|
const w = this.world;
|
|
1598
1620
|
u.set([w.windDir[0], w.windDir[1], w.windAmp, this.phase], 56);
|
|
1599
1621
|
u.set([w.grainScale, w.grainDepth, w.roughness, w.emissive], 60);
|
|
1600
|
-
u.set([
|
|
1622
|
+
u.set([
|
|
1623
|
+
w.hueShift,
|
|
1624
|
+
w.saturation,
|
|
1625
|
+
w.valueLift,
|
|
1626
|
+
this.inspection ? propInspectionChannels.indexOf(channel) : 0,
|
|
1627
|
+
], 64);
|
|
1601
1628
|
// csm.x = how many cascades are live; the matrices follow. Zero when not receiving, so the
|
|
1602
1629
|
// selection loop exits at once and returns fully lit — the old no-shadow path exactly.
|
|
1603
1630
|
u[68] = receive ? Math.min(MAX_CASCADES, mats.length) : 0;
|
|
@@ -1620,6 +1647,8 @@ export class GpuPropsRenderer {
|
|
|
1620
1647
|
Math.max(0, Math.min(envMaxLod, look.environmentDiffuseLod ?? envMaxLod - 3)),
|
|
1621
1648
|
], lookAt);
|
|
1622
1649
|
u.set(this.pointLightWords, lookAt + 8);
|
|
1650
|
+
if (this.inspection)
|
|
1651
|
+
u[lookAt + 9] = Number(this.srgbTarget);
|
|
1623
1652
|
this.device.queue.writeBuffer(this.uBuf, 0, u);
|
|
1624
1653
|
// group 1: the shadow map + comparison sampler (dummy when not receiving). Rebuild only when the view changes.
|
|
1625
1654
|
const sv = shadowView ?? this.dummyShadowView, ss = shadowSamp ?? this.dummyShadowSamp;
|
|
@@ -1649,7 +1678,9 @@ export class GpuPropsRenderer {
|
|
|
1649
1678
|
});
|
|
1650
1679
|
rp.setBindGroup(1, this.shadowBG);
|
|
1651
1680
|
// The arena, bound once for every model in both loops below.
|
|
1652
|
-
if (
|
|
1681
|
+
if (topology)
|
|
1682
|
+
this.inspectionGeometry?.bind(rp);
|
|
1683
|
+
else if (this.geo) {
|
|
1653
1684
|
rp.setVertexBuffer(0, this.geo.pos);
|
|
1654
1685
|
rp.setVertexBuffer(1, this.geo.nrm);
|
|
1655
1686
|
rp.setVertexBuffer(2, this.geo.col);
|
|
@@ -1663,7 +1694,10 @@ export class GpuPropsRenderer {
|
|
|
1663
1694
|
rp.setBindGroup(0, bg);
|
|
1664
1695
|
rp.setBindGroup(2, mdl.matBG);
|
|
1665
1696
|
// Geometry is bound ONCE for the whole pass; a model is a range inside it.
|
|
1666
|
-
|
|
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);
|
|
1667
1701
|
};
|
|
1668
1702
|
// OPAQUE FIRST, then the blended models — the whole point of the second pass. A translucent
|
|
1669
1703
|
// crystal drawn before an opaque rock composites against whatever the sky left in the target and
|
|
@@ -1674,9 +1708,10 @@ export class GpuPropsRenderer {
|
|
|
1674
1708
|
for (const blended of [false, true]) {
|
|
1675
1709
|
for (let m = 0; m < this.models.length; m++) {
|
|
1676
1710
|
const model = this.models[m];
|
|
1677
|
-
|
|
1711
|
+
const clay = channel === 'clay';
|
|
1712
|
+
if ((model.alpha < 1 && !clay) !== blended || this.counts[m] === 0)
|
|
1678
1713
|
continue;
|
|
1679
|
-
const pipeline = this.drawPipeline(blended, model.cullMode, model.depthWrite);
|
|
1714
|
+
const pipeline = this.drawPipeline(blended, model.cullMode, clay || model.depthWrite);
|
|
1680
1715
|
if (pipeline !== activePipeline) {
|
|
1681
1716
|
rp.setPipeline(pipeline);
|
|
1682
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
|
+
}
|