ugly-game 0.15.75 → 0.15.77
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.
- package/dist/assets/gpuProps.d.ts +38 -1
- package/dist/assets/gpuProps.js +225 -24
- package/dist/assets/propGlass.d.ts +23 -0
- package/dist/assets/propGlass.js +57 -0
- package/package.json +1 -1
|
@@ -96,6 +96,13 @@ export interface PropLook {
|
|
|
96
96
|
}
|
|
97
97
|
/** Constructor options. All optional; omitting them is the historical renderer. */
|
|
98
98
|
export interface PropRendererOptions extends Partial<PropLook> {
|
|
99
|
+
/** Optional second colour attachment for translucent props layered after a
|
|
100
|
+
* compositor. The caller supplies WGSL defining encodePropPresentation(rgb:
|
|
101
|
+
* vec3<f32>) -> vec3<f32>. Use linear tone mapping for a scene-linear input. */
|
|
102
|
+
presentation?: {
|
|
103
|
+
format: GPUTextureFormat;
|
|
104
|
+
transferWGSL: string;
|
|
105
|
+
};
|
|
99
106
|
/** Compile material/topology inspection channels. Off by default. On the
|
|
100
107
|
* first topology view, an expanded corner stream is built for wire/density. */
|
|
101
108
|
inspection?: boolean;
|
|
@@ -134,6 +141,12 @@ export interface PropMesh {
|
|
|
134
141
|
normalMap?: GPUTexture;
|
|
135
142
|
/** Thin-glass transmission [0,1]. Use alpha < 1 and depthWrite false. */
|
|
136
143
|
transmission?: number;
|
|
144
|
+
/** Optical path in unscaled model units. With a frame's copyable scene
|
|
145
|
+
* texture, bend its real pixels through this thickness. Requires a blended
|
|
146
|
+
* transmissive surface. Without a snapshot, thin-glass blending is used. */
|
|
147
|
+
glassThickness?: number;
|
|
148
|
+
/** Dielectric index used for bending and Fresnel coverage. Defaults to 1.5. */
|
|
149
|
+
glassIOR?: number;
|
|
137
150
|
/** Display-referred albedo render texture, bypassing lighting/tone mapping. */
|
|
138
151
|
unlit?: boolean;
|
|
139
152
|
/**
|
|
@@ -291,6 +304,10 @@ export interface PropDeform {
|
|
|
291
304
|
tint?: readonly [number, number, number];
|
|
292
305
|
}
|
|
293
306
|
export interface PropInstance {
|
|
307
|
+
/** Draw after the compositor, or split at a plane in the same rebased world
|
|
308
|
+
* coordinates as mat. Positive distances go to presentation, negative to
|
|
309
|
+
* the original scene. The default remains wholly in the original scene. */
|
|
310
|
+
presentation?: true | readonly [number, number, number, number];
|
|
294
311
|
model: number;
|
|
295
312
|
mat: Float32Array;
|
|
296
313
|
/** Optional per-instance deform. Omitted ⇒ the mesh is drawn exactly as authored. */
|
|
@@ -298,8 +315,21 @@ export interface PropInstance {
|
|
|
298
315
|
}
|
|
299
316
|
export declare class GpuPropsRenderer {
|
|
300
317
|
private device;
|
|
318
|
+
private format;
|
|
301
319
|
private inspection;
|
|
320
|
+
private glassInstances;
|
|
321
|
+
private glassVP;
|
|
322
|
+
private snapshots;
|
|
323
|
+
/** Number of source pixels copied by the most recent scene and late passes. */
|
|
324
|
+
readonly glassCopyPixels: {
|
|
325
|
+
scene: number;
|
|
326
|
+
presentation: number;
|
|
327
|
+
};
|
|
302
328
|
private srgbTarget;
|
|
329
|
+
private presentation?;
|
|
330
|
+
private sceneCounts;
|
|
331
|
+
private presentationFirst;
|
|
332
|
+
private hasPresentation;
|
|
303
333
|
private inspectionGeometry?;
|
|
304
334
|
private pipe;
|
|
305
335
|
/**
|
|
@@ -472,5 +502,12 @@ export declare class GpuPropsRenderer {
|
|
|
472
502
|
* a single shared uniform written once per cascade during encoding does not work — see `lightBufs`.
|
|
473
503
|
*/
|
|
474
504
|
shadowPass(pass: GPURenderPassEncoder, lightVP: Float32Array, cascade?: number): void;
|
|
475
|
-
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;
|
|
505
|
+
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, sceneTexture?: GPUTexture): void;
|
|
506
|
+
/** Draw over the presented compositor result, testing its final depth.
|
|
507
|
+
* Reuses the same uploaded instances, material bindings and frame uniforms. */
|
|
508
|
+
framePresentation(enc: GPUCommandEncoder, colorView: GPUTextureView, depthView: GPUTextureView, sceneTexture?: GPUTexture): void;
|
|
509
|
+
private encodeColor;
|
|
510
|
+
/** Release renderer-owned backdrop storage on teardown or after a resize. */
|
|
511
|
+
releaseGlassSnapshots(): void;
|
|
512
|
+
private colorPass;
|
|
476
513
|
}
|
package/dist/assets/gpuProps.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// ─────────────────────────────────────────────────────────────────────────────
|
|
7
7
|
// The cull reuses the SAME plane extraction the rest of the engine uses. A second, local copy of
|
|
8
8
|
// Gribb-Hartmann is exactly how a cull ends up disagreeing with the projection it is culling for.
|
|
9
|
+
import { glassBounds, glassCopyRegion, } from './propGlass.js';
|
|
9
10
|
import { WET_RIND_WGSL } from './wetRind.js';
|
|
10
11
|
export { WET_RIND_WGSL } from './wetRind.js';
|
|
11
12
|
import { INTERIOR_LIGHT_WGSL } from './interiorLight.js';
|
|
@@ -19,7 +20,7 @@ export const MAX_CASCADES = 4; // must match gpuShadow's
|
|
|
19
20
|
export const MAX_PROP_POINT_LIGHTS = 8;
|
|
20
21
|
const POINT_LIGHT_WORDS = 4 + MAX_PROP_POINT_LIGHTS * 16;
|
|
21
22
|
const PROP_UNIFORM_WORDS = 72 + 16 * MAX_CASCADES + 8 + POINT_LIGHT_WORDS;
|
|
22
|
-
const drawShader = (inspect) => /* wgsl */ `
|
|
23
|
+
const drawShader = (inspect, split, transfer = '') => /* wgsl */ `
|
|
23
24
|
// wind = (dirX, dirZ, amplitude, phase) — sway, amplitude scaled by the world's air density
|
|
24
25
|
// surf = (grainScale, grainDepth, roughness, emissive)
|
|
25
26
|
// grade = (hueShift, saturation, valueLift, unused)
|
|
@@ -62,7 +63,8 @@ struct Mat { mat: vec4<f32>, coat: vec4<f32>, maps: vec4<f32>, surface: vec4<f32
|
|
|
62
63
|
// Renderer-wide linear HDR equirect (PropLook.environment). A 1×1 dummy when unset, and then never
|
|
63
64
|
// sampled: look.z and look.w are forced to 0 on the CPU without it.
|
|
64
65
|
@group(1) @binding(2) var sceneEnvTex: texture_2d<f32>;
|
|
65
|
-
|
|
66
|
+
@group(1) @binding(3) var transmittedScene: texture_2d<f32>;
|
|
67
|
+
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>, @location(7) glassScale:f32 ${split ? ', @location(6) clipDistance: f32' : ''} ${inspect ? ', @location(5) @interpolate(linear) bary:vec3<f32>' : ''} };
|
|
66
68
|
// WIND SWAY. Displacement is weighted by height above the instance's own anchor, so a trunk stays
|
|
67
69
|
// planted while the canopy moves, and the phase is offset by world position so a grove never sways
|
|
68
70
|
// in unison. Amplitude comes from the world's air density: an airless world is perfectly, eerily
|
|
@@ -134,7 +136,8 @@ fn deformNormal(n: vec3<f32>, py: f32, d0: vec4<f32>, d1: vec4<f32>, d2: vec4<f3
|
|
|
134
136
|
let sc = length(m[0].xyz);
|
|
135
137
|
world = sway(lp, world, upv, sc * inst.d2.w);
|
|
136
138
|
var o: VO; o.pos = u.mvp * vec4(world, 1.0); o.world = world; o.n = (m * vec4(ln, 0.0)).xyz;
|
|
137
|
-
o.
|
|
139
|
+
${split ? 'o.clipDistance = dot(world,vec3(m[0].w,m[1].w,m[2].w))+inst.d3.w;' : ''}
|
|
140
|
+
o.glassScale = sc; o.c = col * inst.d2.y * inst.d3.rgb; o.l = lp; o.uv = uv;
|
|
138
141
|
${inspect ? 'o.bary = vec3<f32>(f32(vi%3u==0u),f32(vi%3u==1u),f32(vi%3u==2u));' : ''}
|
|
139
142
|
return o;
|
|
140
143
|
}
|
|
@@ -373,11 +376,16 @@ fn effectiveAO(orm:vec3<f32>) -> f32 { return select(1.0,orm.r,mtl.maps.y>0.5);
|
|
|
373
376
|
fn surfaceOpacity(n:vec3<f32>, world:vec3<f32>) -> f32 {
|
|
374
377
|
if (mtl.optics.x <= 0.0) { return mtl.mat.x; }
|
|
375
378
|
let nv = clamp(abs(dot(n,normalize(u.cam.xyz-world))),0.0,1.0);
|
|
376
|
-
let
|
|
379
|
+
let eta = max(1.0,mtl.medium.z);
|
|
380
|
+
let r = (eta-1.0)/(eta+1.0);
|
|
381
|
+
let f0 = select(0.04,r*r,mtl.medium.w>0.0);
|
|
382
|
+
let F = f0 + (1.0-f0)*pow(1.0-nv,5.0);
|
|
377
383
|
return clamp(1.0-mtl.optics.x*(1.0-F),0.0,1.0);
|
|
378
384
|
}
|
|
379
385
|
${THIN_TISSUE_WGSL}
|
|
380
|
-
|
|
386
|
+
${transfer}
|
|
387
|
+
fn shade(i: VO) -> vec4<f32> {
|
|
388
|
+
${split ? (transfer ? 'if (i.clipDistance < 0.0) { discard; }' : 'if (i.clipDistance >= 0.0) { discard; }') : ''}
|
|
381
389
|
var N = normalize(i.n);
|
|
382
390
|
let materialUV=i.uv*mtl.maps.w;
|
|
383
391
|
let px=dpdx(i.world); let py=dpdy(i.world);
|
|
@@ -546,6 +554,26 @@ ${THIN_TISSUE_WGSL}
|
|
|
546
554
|
// A display-referred render texture (rear-view camera) is already lit.
|
|
547
555
|
if (mtl.optics.y > 0.5) { return vec4(tex.rgb, mtl.mat.x); }
|
|
548
556
|
return vec4(mix(lit, u.fog.xyz, f), opacity);
|
|
557
|
+
}
|
|
558
|
+
@fragment fn fs(i: VO) -> @location(0) vec4<f32> {
|
|
559
|
+
let color = shade(i);
|
|
560
|
+
let output = ${transfer ? 'vec4(encodePropPresentation(color.rgb),color.a)' : 'color'};
|
|
561
|
+
if (mtl.medium.w > 0.0 && textureDimensions(transmittedScene).x > 1u) {
|
|
562
|
+
let size = vec2<f32>(textureDimensions(transmittedScene));
|
|
563
|
+
let V = normalize(u.cam.xyz-i.world);
|
|
564
|
+
let N = faceForward(normalize(i.n),-V,normalize(i.n));
|
|
565
|
+
let ray = refract(-V,N,1.0/mtl.medium.z);
|
|
566
|
+
let path = mtl.medium.w*i.glassScale;
|
|
567
|
+
let clear = u.mvp*vec4(i.world-V*path,1.0);
|
|
568
|
+
let bent = u.mvp*vec4(i.world+ray*path,1.0);
|
|
569
|
+
let shift = (bent.xy/max(0.001,bent.w)-clear.xy/max(0.001,clear.w))*vec2(0.5,-0.5);
|
|
570
|
+
let coord = clamp(i.pos.xy/size+shift,0.5/size,1.0-0.5/size);
|
|
571
|
+
let through = textureSampleLevel(transmittedScene,albedoSamp,coord,0.0).rgb;
|
|
572
|
+
let density = vec3(1.0)-clamp(srgbToLinear(i.c),vec3(0.0),vec3(1.0));
|
|
573
|
+
let absorb = exp(-density*mtl.medium.w/max(0.35,-dot(ray,N)));
|
|
574
|
+
return vec4(mix(through*absorb,output.rgb,output.a),1.0);
|
|
575
|
+
}
|
|
576
|
+
return output;
|
|
549
577
|
}`;
|
|
550
578
|
// Depth-only occluder pass: instanced prop depth from the sun's ortho view (so trees/rocks cast shadows).
|
|
551
579
|
// Depth-only occluder. It MUST apply the identical sway, or the shadows stay put while the leaves
|
|
@@ -697,8 +725,18 @@ fn cull(@builtin(global_invocation_id) gid: vec3<u32>) {
|
|
|
697
725
|
`;
|
|
698
726
|
export class GpuPropsRenderer {
|
|
699
727
|
device;
|
|
728
|
+
format;
|
|
700
729
|
inspection;
|
|
730
|
+
glassInstances = [];
|
|
731
|
+
glassVP = new Float32Array(16);
|
|
732
|
+
snapshots = [];
|
|
733
|
+
/** Number of source pixels copied by the most recent scene and late passes. */
|
|
734
|
+
glassCopyPixels = { scene: 0, presentation: 0 };
|
|
701
735
|
srgbTarget;
|
|
736
|
+
presentation;
|
|
737
|
+
sceneCounts = new Uint32Array(0);
|
|
738
|
+
presentationFirst = new Uint32Array(0);
|
|
739
|
+
hasPresentation = false;
|
|
702
740
|
inspectionGeometry;
|
|
703
741
|
pipe;
|
|
704
742
|
/**
|
|
@@ -857,7 +895,8 @@ export class GpuPropsRenderer {
|
|
|
857
895
|
}
|
|
858
896
|
constructor(device, format, options = {}) {
|
|
859
897
|
this.device = device;
|
|
860
|
-
|
|
898
|
+
this.format = format;
|
|
899
|
+
const { inspection = false, sampleCount = 1, depth = FORWARD_DEPTH, shadowDepthFormat = 'depth32float', presentation, ...look } = options;
|
|
861
900
|
// Runtime guard for untyped callers: WebGPU only guarantees 1 and 4.
|
|
862
901
|
if (![1, 4].includes(sampleCount))
|
|
863
902
|
throw new RangeError(`Unsupported prop sampleCount ${String(sampleCount)}`);
|
|
@@ -865,10 +904,13 @@ export class GpuPropsRenderer {
|
|
|
865
904
|
throw new RangeError(`Unsupported prop shadowDepthFormat ${shadowDepthFormat}`);
|
|
866
905
|
this.sampleCount = sampleCount;
|
|
867
906
|
this.depth = depth;
|
|
907
|
+
this.presentation = presentation;
|
|
868
908
|
this.setLook(look);
|
|
869
909
|
this.inspection = inspection;
|
|
870
910
|
this.srgbTarget = format.endsWith('-srgb');
|
|
871
|
-
const sm = device.createShaderModule({
|
|
911
|
+
const sm = device.createShaderModule({
|
|
912
|
+
code: drawShader(inspection, !!presentation),
|
|
913
|
+
});
|
|
872
914
|
// The three groups the SHADER declares, written out so BOTH render pipelines share them. Under
|
|
873
915
|
// `layout: 'auto'` each pipeline would mint its own and the bind groups built elsewhere in this
|
|
874
916
|
// class could only ever be used by one of them.
|
|
@@ -903,6 +945,11 @@ export class GpuPropsRenderer {
|
|
|
903
945
|
visibility: GPUShaderStage.FRAGMENT,
|
|
904
946
|
texture: { sampleType: 'float' },
|
|
905
947
|
},
|
|
948
|
+
{
|
|
949
|
+
binding: 3,
|
|
950
|
+
visibility: GPUShaderStage.FRAGMENT,
|
|
951
|
+
texture: { sampleType: 'float' },
|
|
952
|
+
},
|
|
906
953
|
],
|
|
907
954
|
});
|
|
908
955
|
const bgl2 = device.createBindGroupLayout({
|
|
@@ -1009,13 +1056,39 @@ export class GpuPropsRenderer {
|
|
|
1009
1056
|
['false:none:true', this.pipe],
|
|
1010
1057
|
['true:none:true', this.blendPipe],
|
|
1011
1058
|
]);
|
|
1012
|
-
|
|
1013
|
-
|
|
1059
|
+
let presentationModule;
|
|
1060
|
+
this.drawPipeline = (blend, cull, depthWrite, late = false) => {
|
|
1061
|
+
const key = `${blend}:${cull}:${depthWrite}${late ? ':presentation' : ''}`;
|
|
1014
1062
|
let pipeline = variants.get(key);
|
|
1015
1063
|
if (!pipeline) {
|
|
1016
1064
|
const base = blend ? blendDescriptor : opaqueDescriptor;
|
|
1017
1065
|
pipeline = device.createRenderPipeline({
|
|
1018
1066
|
...base,
|
|
1067
|
+
...(late && presentation
|
|
1068
|
+
? {
|
|
1069
|
+
fragment: {
|
|
1070
|
+
...base.fragment,
|
|
1071
|
+
module: (presentationModule ??= device.createShaderModule({
|
|
1072
|
+
code: drawShader(inspection, true, presentation.transferWGSL),
|
|
1073
|
+
})),
|
|
1074
|
+
targets: [
|
|
1075
|
+
{
|
|
1076
|
+
format: presentation.format,
|
|
1077
|
+
blend: {
|
|
1078
|
+
color: {
|
|
1079
|
+
srcFactor: 'src-alpha',
|
|
1080
|
+
dstFactor: 'one-minus-src-alpha',
|
|
1081
|
+
},
|
|
1082
|
+
alpha: {
|
|
1083
|
+
srcFactor: 'one',
|
|
1084
|
+
dstFactor: 'one-minus-src-alpha',
|
|
1085
|
+
},
|
|
1086
|
+
},
|
|
1087
|
+
},
|
|
1088
|
+
],
|
|
1089
|
+
},
|
|
1090
|
+
}
|
|
1091
|
+
: {}),
|
|
1019
1092
|
primitive: { ...base.primitive, cullMode: cull },
|
|
1020
1093
|
depthStencil: {
|
|
1021
1094
|
...base.depthStencil,
|
|
@@ -1181,6 +1254,9 @@ export class GpuPropsRenderer {
|
|
|
1181
1254
|
this.device.queue.writeBuffer(packed.matBuf, 0, packed.materialValues);
|
|
1182
1255
|
}
|
|
1183
1256
|
setModels(meshes) {
|
|
1257
|
+
this.glassInstances.length = 0;
|
|
1258
|
+
if (!meshes.some((m) => m.glassThickness))
|
|
1259
|
+
this.releaseGlassSnapshots();
|
|
1184
1260
|
this.inspectionGeometry?.destroy();
|
|
1185
1261
|
if (this.inspection)
|
|
1186
1262
|
this.inspectionGeometry = new PropInspectionGeometry(this.device, meshes);
|
|
@@ -1322,12 +1398,18 @@ export class GpuPropsRenderer {
|
|
|
1322
1398
|
Math.max(0, Math.min(1, m.directClearcoat ?? 0)),
|
|
1323
1399
|
Math.max(0, Math.min(8, m.interiorOpticalDepth ?? 0)),
|
|
1324
1400
|
Math.max(0, m.wetRindHeight ?? 0),
|
|
1325
|
-
|
|
1326
|
-
0,
|
|
1401
|
+
Math.min(2.8, Math.max(1, m.glassIOR ?? 1.5)),
|
|
1402
|
+
Math.max(0, m.glassThickness ?? 0),
|
|
1327
1403
|
]);
|
|
1328
1404
|
dev.queue.writeBuffer(matBuf, 0, materialValues);
|
|
1329
1405
|
return {
|
|
1330
1406
|
alpha,
|
|
1407
|
+
...(m.glassThickness &&
|
|
1408
|
+
m.glassThickness > 0 &&
|
|
1409
|
+
m.transmission &&
|
|
1410
|
+
alpha < 1
|
|
1411
|
+
? { glassBounds: glassBounds(m.positions, m.glassThickness) }
|
|
1412
|
+
: {}),
|
|
1331
1413
|
cullMode: m.cullMode ?? 'none',
|
|
1332
1414
|
depthWrite: m.depthWrite ?? true,
|
|
1333
1415
|
cutout,
|
|
@@ -1360,6 +1442,7 @@ export class GpuPropsRenderer {
|
|
|
1360
1442
|
this.bgs = meshes.map(() => null);
|
|
1361
1443
|
this.occBGs = meshes.map(() => []);
|
|
1362
1444
|
this.counts = meshes.map(() => 0);
|
|
1445
|
+
this.hasPresentation = false;
|
|
1363
1446
|
this.cullU = meshes.map(() => []);
|
|
1364
1447
|
this.cullArgs = meshes.map(() => []);
|
|
1365
1448
|
this.cullBGs = meshes.map(() => []);
|
|
@@ -1376,23 +1459,68 @@ export class GpuPropsRenderer {
|
|
|
1376
1459
|
* re-allocating a megabyte of storage buffers per frame would cost more than the LOD saves.
|
|
1377
1460
|
*/
|
|
1378
1461
|
setInstances(instances) {
|
|
1462
|
+
this.glassInstances.length = 0;
|
|
1379
1463
|
const dev = this.device;
|
|
1380
1464
|
const n = this.models.length;
|
|
1381
1465
|
const counts = new Uint32Array(n);
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1466
|
+
this.hasPresentation = false;
|
|
1467
|
+
const split = !!this.presentation;
|
|
1468
|
+
if (this.presentation) {
|
|
1469
|
+
this.sceneCounts = new Uint32Array(n);
|
|
1470
|
+
this.presentationFirst = new Uint32Array(n);
|
|
1471
|
+
}
|
|
1472
|
+
for (const it of instances) {
|
|
1473
|
+
if (it.model < 0 || it.model >= n)
|
|
1474
|
+
continue;
|
|
1475
|
+
counts[it.model]++;
|
|
1476
|
+
if (!split)
|
|
1477
|
+
continue;
|
|
1478
|
+
if (!it.presentation)
|
|
1479
|
+
this.presentationFirst[it.model]++;
|
|
1480
|
+
else {
|
|
1481
|
+
this.hasPresentation = true;
|
|
1482
|
+
}
|
|
1483
|
+
if (it.presentation !== true)
|
|
1484
|
+
this.sceneCounts[it.model]++;
|
|
1485
|
+
}
|
|
1486
|
+
// Scene-only, split, presentation-only: each stage has one contiguous draw
|
|
1487
|
+
// per model. A crossing instance appears in both ranges at its single GPU address.
|
|
1385
1488
|
const cursors = new Uint32Array(n);
|
|
1489
|
+
const splitCursors = split ? this.presentationFirst.slice() : cursors;
|
|
1490
|
+
const lateCursors = split ? this.sceneCounts.slice() : cursors;
|
|
1386
1491
|
const arrays = new Array(n);
|
|
1387
1492
|
for (let m = 0; m < n; m++)
|
|
1388
1493
|
arrays[m] = counts[m] ? new Float32Array(counts[m] * INST_FLOATS) : null;
|
|
1389
1494
|
for (const it of instances) {
|
|
1390
1495
|
if (it.model < 0 || it.model >= n)
|
|
1391
1496
|
continue;
|
|
1497
|
+
const bounds = this.models[it.model].glassBounds;
|
|
1498
|
+
if (bounds)
|
|
1499
|
+
this.glassInstances.push({
|
|
1500
|
+
bounds,
|
|
1501
|
+
mat: it.mat,
|
|
1502
|
+
early: !split || it.presentation !== true,
|
|
1503
|
+
late: split && !!it.presentation,
|
|
1504
|
+
});
|
|
1392
1505
|
const arr = arrays[it.model];
|
|
1393
|
-
const
|
|
1506
|
+
const cursor = !split || !it.presentation
|
|
1507
|
+
? cursors
|
|
1508
|
+
: it.presentation === true
|
|
1509
|
+
? lateCursors
|
|
1510
|
+
: splitCursors;
|
|
1511
|
+
const o = cursor[it.model]++ * INST_FLOATS;
|
|
1394
1512
|
arr.set(it.mat.subarray(0, 16), o);
|
|
1395
1513
|
writeDeform(arr, o + 16, it.deform, this.models[it.model].extent);
|
|
1514
|
+
if (this.presentation) {
|
|
1515
|
+
const plane = it.presentation && it.presentation !== true ? it.presentation : null;
|
|
1516
|
+
// Placement is affine. Its unused fourth row and deformation padding
|
|
1517
|
+
// hold the clip plane with no added per-instance bytes; all passes
|
|
1518
|
+
// read world position from the first three rows.
|
|
1519
|
+
arr[o + 3] = plane?.[0] ?? 0;
|
|
1520
|
+
arr[o + 7] = plane?.[1] ?? 0;
|
|
1521
|
+
arr[o + 11] = plane?.[2] ?? 0;
|
|
1522
|
+
arr[o + 31] = plane?.[3] ?? (it.presentation ? 1 : -1);
|
|
1523
|
+
}
|
|
1396
1524
|
}
|
|
1397
1525
|
for (let m = 0; m < n; m++) {
|
|
1398
1526
|
const arr = arrays[m];
|
|
@@ -1640,10 +1768,12 @@ export class GpuPropsRenderer {
|
|
|
1640
1768
|
frame(enc, colorView, depthView, camVP, sunDir, sunCol, amb, camPos, fog, fogDensity, up,
|
|
1641
1769
|
// Optional sun shadow-receive: pass the terrain's lightVP + shadow-map view + comparison sampler and props sit
|
|
1642
1770
|
// in the same shadow. Omit all three (the demo path) → no shadowing, byte-identical to before.
|
|
1643
|
-
lightVP, shadowView, shadowSamp, channel = 'lit') {
|
|
1771
|
+
lightVP, shadowView, shadowSamp, channel = 'lit', sceneTexture) {
|
|
1772
|
+
if (this.glassInstances.length)
|
|
1773
|
+
this.glassVP.set(camVP);
|
|
1774
|
+
this.glassCopyPixels.scene = this.glassCopyPixels.presentation = 0;
|
|
1644
1775
|
if (channel !== 'lit' && !this.inspection)
|
|
1645
1776
|
throw new Error('Prop inspection must be enabled at construction.');
|
|
1646
|
-
const topology = channel === 'wireframe' || channel === 'density';
|
|
1647
1777
|
const receive = !!(lightVP && shadowView && shadowSamp);
|
|
1648
1778
|
const mats = lightVP ? (Array.isArray(lightVP) ? lightVP : [lightVP]) : [];
|
|
1649
1779
|
const u = new Float32Array(PROP_UNIFORM_WORDS);
|
|
@@ -1704,12 +1834,74 @@ export class GpuPropsRenderer {
|
|
|
1704
1834
|
{ binding: 0, resource: sv },
|
|
1705
1835
|
{ binding: 1, resource: ss },
|
|
1706
1836
|
{ binding: 2, resource: (env ?? this.dummyEnv).createView() },
|
|
1837
|
+
{ binding: 3, resource: this.dummyEnv.createView() },
|
|
1707
1838
|
],
|
|
1708
1839
|
});
|
|
1709
1840
|
this.lastSV = sv;
|
|
1710
1841
|
this.lastSS = ss;
|
|
1711
1842
|
this.lastEnv = env;
|
|
1712
1843
|
}
|
|
1844
|
+
this.encodeColor(enc, colorView, depthView, channel, false, sceneTexture);
|
|
1845
|
+
}
|
|
1846
|
+
/** Draw over the presented compositor result, testing its final depth.
|
|
1847
|
+
* Reuses the same uploaded instances, material bindings and frame uniforms. */
|
|
1848
|
+
framePresentation(enc, colorView, depthView, sceneTexture) {
|
|
1849
|
+
if (this.hasPresentation && this.presentation)
|
|
1850
|
+
this.encodeColor(enc, colorView, depthView, 'lit', true, sceneTexture);
|
|
1851
|
+
}
|
|
1852
|
+
encodeColor(enc, colorView, depthView, channel, late, source) {
|
|
1853
|
+
const crop = source &&
|
|
1854
|
+
channel === 'lit' &&
|
|
1855
|
+
source.sampleCount === 1 &&
|
|
1856
|
+
this.glassInstances.length
|
|
1857
|
+
? glassCopyRegion(this.glassInstances, this.glassVP, source.width, source.height, late)
|
|
1858
|
+
: undefined;
|
|
1859
|
+
this.colorPass(enc, colorView, depthView, channel, late, this.shadowBG, crop ? 'background' : 'all');
|
|
1860
|
+
if (!crop || !source)
|
|
1861
|
+
return;
|
|
1862
|
+
const format = late ? this.presentation.format : this.format;
|
|
1863
|
+
const slot = Number(late);
|
|
1864
|
+
let entry = this.snapshots[slot];
|
|
1865
|
+
if (entry?.texture.width !== source.width ||
|
|
1866
|
+
entry.texture.height !== source.height ||
|
|
1867
|
+
entry.texture.format !== format) {
|
|
1868
|
+
entry?.texture.destroy();
|
|
1869
|
+
const texture = this.device.createTexture({
|
|
1870
|
+
label: 'prop-glass-backdrop',
|
|
1871
|
+
format,
|
|
1872
|
+
size: [source.width, source.height],
|
|
1873
|
+
usage: GPUTextureUsage.COPY_DST | GPUTextureUsage.TEXTURE_BINDING,
|
|
1874
|
+
});
|
|
1875
|
+
this.snapshots[slot] = entry = { texture, view: texture.createView() };
|
|
1876
|
+
}
|
|
1877
|
+
enc.copyTextureToTexture({ texture: source, origin: crop.origin }, { texture: entry.texture, origin: crop.origin }, crop.size);
|
|
1878
|
+
this.glassCopyPixels[late ? 'presentation' : 'scene'] =
|
|
1879
|
+
crop.size.width * crop.size.height;
|
|
1880
|
+
if (!entry.group || entry.shadow !== this.shadowBG) {
|
|
1881
|
+
entry.shadow = this.shadowBG;
|
|
1882
|
+
entry.group = this.device.createBindGroup({
|
|
1883
|
+
layout: this.drawBGL[1],
|
|
1884
|
+
entries: [
|
|
1885
|
+
{ binding: 0, resource: this.lastSV },
|
|
1886
|
+
{ binding: 1, resource: this.lastSS },
|
|
1887
|
+
{
|
|
1888
|
+
binding: 2,
|
|
1889
|
+
resource: (this.lastEnv ?? this.dummyEnv).createView(),
|
|
1890
|
+
},
|
|
1891
|
+
{ binding: 3, resource: entry.view },
|
|
1892
|
+
],
|
|
1893
|
+
});
|
|
1894
|
+
}
|
|
1895
|
+
this.colorPass(enc, colorView, depthView, channel, late, entry.group, 'glass');
|
|
1896
|
+
}
|
|
1897
|
+
/** Release renderer-owned backdrop storage on teardown or after a resize. */
|
|
1898
|
+
releaseGlassSnapshots() {
|
|
1899
|
+
for (const snapshot of this.snapshots)
|
|
1900
|
+
snapshot?.texture.destroy();
|
|
1901
|
+
this.snapshots = [];
|
|
1902
|
+
}
|
|
1903
|
+
colorPass(enc, colorView, depthView, channel, late, lighting, selection) {
|
|
1904
|
+
const topology = channel === 'wireframe' || channel === 'density';
|
|
1713
1905
|
const rp = enc.beginRenderPass({
|
|
1714
1906
|
colorAttachments: [{ view: colorView, loadOp: 'load', storeOp: 'store' }],
|
|
1715
1907
|
depthStencilAttachment: {
|
|
@@ -1718,7 +1910,7 @@ export class GpuPropsRenderer {
|
|
|
1718
1910
|
depthStoreOp: 'store',
|
|
1719
1911
|
},
|
|
1720
1912
|
});
|
|
1721
|
-
rp.setBindGroup(1,
|
|
1913
|
+
rp.setBindGroup(1, lighting);
|
|
1722
1914
|
// The arena, bound once for every model in both loops below.
|
|
1723
1915
|
if (topology)
|
|
1724
1916
|
this.inspectionGeometry?.bind(rp);
|
|
@@ -1729,7 +1921,7 @@ export class GpuPropsRenderer {
|
|
|
1729
1921
|
rp.setVertexBuffer(3, this.geo.uv);
|
|
1730
1922
|
rp.setIndexBuffer(this.geo.idx, 'uint32');
|
|
1731
1923
|
}
|
|
1732
|
-
const drawModel = (m) => {
|
|
1924
|
+
const drawModel = (m, count, first) => {
|
|
1733
1925
|
const bg = this.bgs[m], mdl = this.models[m];
|
|
1734
1926
|
if (!bg || this.counts[m] === 0)
|
|
1735
1927
|
return;
|
|
@@ -1737,9 +1929,9 @@ export class GpuPropsRenderer {
|
|
|
1737
1929
|
rp.setBindGroup(2, mdl.matBG);
|
|
1738
1930
|
// Geometry is bound ONCE for the whole pass; a model is a range inside it.
|
|
1739
1931
|
if (topology)
|
|
1740
|
-
rp.draw(mdl.idxCount,
|
|
1932
|
+
rp.draw(mdl.idxCount, count, mdl.firstIndex, first);
|
|
1741
1933
|
else
|
|
1742
|
-
rp.drawIndexed(mdl.idxCount,
|
|
1934
|
+
rp.drawIndexed(mdl.idxCount, count, mdl.firstIndex, mdl.baseVertex, first);
|
|
1743
1935
|
};
|
|
1744
1936
|
// OPAQUE FIRST, then the blended models — the whole point of the second pass. A translucent
|
|
1745
1937
|
// crystal drawn before an opaque rock composites against whatever the sky left in the target and
|
|
@@ -1750,15 +1942,24 @@ export class GpuPropsRenderer {
|
|
|
1750
1942
|
for (const blended of [false, true]) {
|
|
1751
1943
|
for (let m = 0; m < this.models.length; m++) {
|
|
1752
1944
|
const model = this.models[m];
|
|
1945
|
+
if (selection !== 'all' &&
|
|
1946
|
+
!!model.glassBounds !== (selection === 'glass'))
|
|
1947
|
+
continue;
|
|
1753
1948
|
const clay = channel === 'clay';
|
|
1754
|
-
|
|
1949
|
+
const first = late ? this.presentationFirst[m] : 0;
|
|
1950
|
+
const count = this.presentation
|
|
1951
|
+
? late
|
|
1952
|
+
? this.counts[m] - first
|
|
1953
|
+
: this.sceneCounts[m]
|
|
1954
|
+
: this.counts[m];
|
|
1955
|
+
if ((model.alpha < 1 && !clay) !== blended || !count)
|
|
1755
1956
|
continue;
|
|
1756
|
-
const pipeline = this.drawPipeline(blended, model.cullMode, clay || model.depthWrite);
|
|
1957
|
+
const pipeline = this.drawPipeline(blended, model.cullMode, clay || model.depthWrite, late);
|
|
1757
1958
|
if (pipeline !== activePipeline) {
|
|
1758
1959
|
rp.setPipeline(pipeline);
|
|
1759
1960
|
activePipeline = pipeline;
|
|
1760
1961
|
}
|
|
1761
|
-
drawModel(m);
|
|
1962
|
+
drawModel(m, count, first);
|
|
1762
1963
|
}
|
|
1763
1964
|
}
|
|
1764
1965
|
rp.end();
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Local bounds expanded to include the exit point of a refracted ray. */
|
|
2
|
+
export declare function glassBounds(positions: Float32Array, thickness: number): {
|
|
3
|
+
low: number[];
|
|
4
|
+
high: number[];
|
|
5
|
+
};
|
|
6
|
+
export interface GlassBoundsInstance {
|
|
7
|
+
bounds: ReturnType<typeof glassBounds>;
|
|
8
|
+
mat: Float32Array;
|
|
9
|
+
early: boolean;
|
|
10
|
+
late: boolean;
|
|
11
|
+
}
|
|
12
|
+
/** The copy keeps original pixel coordinates inside a retained scratch texture.
|
|
13
|
+
* Sampling is clamped to the viewport, including when a lens crosses its edge. */
|
|
14
|
+
export declare function glassCopyRegion(instances: readonly GlassBoundsInstance[], vp: Float32Array, width: number, height: number, late: boolean): {
|
|
15
|
+
origin: {
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
};
|
|
19
|
+
size: {
|
|
20
|
+
width: number;
|
|
21
|
+
height: number;
|
|
22
|
+
};
|
|
23
|
+
} | undefined;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** Local bounds expanded to include the exit point of a refracted ray. */
|
|
2
|
+
export function glassBounds(positions, thickness) {
|
|
3
|
+
const low = [Infinity, Infinity, Infinity];
|
|
4
|
+
const high = [-Infinity, -Infinity, -Infinity];
|
|
5
|
+
for (let i = 0; i < positions.length; i += 3) {
|
|
6
|
+
for (let k = 0; k < 3; k++) {
|
|
7
|
+
low[k] = Math.min(low[k], positions[i + k] - thickness * 2);
|
|
8
|
+
high[k] = Math.max(high[k], positions[i + k] + thickness * 2);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
return { low, high };
|
|
12
|
+
}
|
|
13
|
+
/** The copy keeps original pixel coordinates inside a retained scratch texture.
|
|
14
|
+
* Sampling is clamped to the viewport, including when a lens crosses its edge. */
|
|
15
|
+
export function glassCopyRegion(instances, vp, width, height, late) {
|
|
16
|
+
let left = width, right = 0, top = height, bottom = 0;
|
|
17
|
+
for (const instance of instances) {
|
|
18
|
+
if (!(late ? instance.late : instance.early))
|
|
19
|
+
continue;
|
|
20
|
+
const m = instance.mat, { low, high } = instance.bounds;
|
|
21
|
+
let l = width, r = 0, t = height, b = 0;
|
|
22
|
+
let inFront = false, behind = false;
|
|
23
|
+
for (let corner = 0; corner < 8; corner++) {
|
|
24
|
+
const x = corner & 1 ? high[0] : low[0], y = corner & 2 ? high[1] : low[1], z = corner & 4 ? high[2] : low[2];
|
|
25
|
+
const wx = m[0] * x + m[4] * y + m[8] * z + m[12];
|
|
26
|
+
const wy = m[1] * x + m[5] * y + m[9] * z + m[13];
|
|
27
|
+
const wz = m[2] * x + m[6] * y + m[10] * z + m[14];
|
|
28
|
+
const w = vp[3] * wx + vp[7] * wy + vp[11] * wz + vp[15];
|
|
29
|
+
if (w <= 0.001) {
|
|
30
|
+
behind = true;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
inFront = true;
|
|
34
|
+
const sx = (0.5 + (0.5 * (vp[0] * wx + vp[4] * wy + vp[8] * wz + vp[12])) / w) *
|
|
35
|
+
width;
|
|
36
|
+
const sy = (0.5 - (0.5 * (vp[1] * wx + vp[5] * wy + vp[9] * wz + vp[13])) / w) *
|
|
37
|
+
height;
|
|
38
|
+
l = Math.min(l, sx);
|
|
39
|
+
r = Math.max(r, sx);
|
|
40
|
+
t = Math.min(t, sy);
|
|
41
|
+
b = Math.max(b, sy);
|
|
42
|
+
}
|
|
43
|
+
if (!inFront)
|
|
44
|
+
continue;
|
|
45
|
+
if (behind)
|
|
46
|
+
return { origin: { x: 0, y: 0 }, size: { width, height } };
|
|
47
|
+
left = Math.min(left, l);
|
|
48
|
+
right = Math.max(right, r);
|
|
49
|
+
top = Math.min(top, t);
|
|
50
|
+
bottom = Math.max(bottom, b);
|
|
51
|
+
}
|
|
52
|
+
const x = Math.max(0, Math.floor(left) - 2), y = Math.max(0, Math.floor(top) - 2);
|
|
53
|
+
const w = Math.min(width, Math.ceil(right) + 2) - x, h = Math.min(height, Math.ceil(bottom) + 2) - y;
|
|
54
|
+
return w > 0 && h > 0
|
|
55
|
+
? { origin: { x, y }, size: { width: w, height: h } }
|
|
56
|
+
: undefined;
|
|
57
|
+
}
|