14 @location(0) pos: vec2f,
15 @location(1) color: vec4f,
19 @builtin(position) pos: vec4f,
20 @location(0) color: vec4f,
24fn vs_main(in: VSIn) -> VSOut {
26 out.pos = vec4f(in.pos, 0.0, 1.0);
35 @location(0) color: vec4f,
39fn fs_main(in: FSIn) -> @location(0) vec4f {
48 @location(0) pos: vec2f,
49 @location(1) color: vec4f,
50 @location(2) uv: vec2f,
54 @builtin(position) pos: vec4f,
55 @location(0) color: vec4f,
56 @location(1) uv: vec2f,
60fn vs_main(in: VSIn) -> VSOut {
62 out.pos = vec4f(in.pos, 0.0, 1.0);
72 @location(0) color: vec4f,
73 @location(1) uv: vec2f,
75@group(0) @binding(0) var mainTex: texture_2d<f32>;
76@group(0) @binding(2) var mainSamp: sampler;
79fn fs_main(in: FSIn) -> @location(0) vec4f {
81 return textureSample(mainTex, mainSamp, in.uv) * in.color;
89 @location(0) color: vec4f,
90 @location(1) uv: vec2f,
96@group(0) @binding(0) var mainTex: texture_2d<f32>;
97@group(0) @binding(2) var mainSamp: sampler;
98@group(0) @binding(4) var<uniform> u: Externals;
101fn fs_main(in: FSIn) -> @location(0) vec4f {
103 return textureSample(mainTex, mainSamp, in.uv) * in.color;
111 @location(0) pos: vec2f,
112 @location(1) color: vec4f,
113 @location(2) uv: vec2f,
117 @builtin(position) pos: vec4f,
118 @location(0) color: vec4f,
119 @location(1) uv: vec2f,
120 @location(2) ndc: vec2f,
124fn vs_main(in: VSIn) -> VSOut {
126 out.pos = vec4f(in.pos, 0.0, 1.0);
127 out.color = in.color;
137 @location(0) color: vec4f,
138 @location(1) uv: vec2f,
139 @location(2) ndc: vec2f,
145 spot: vec4f, // xy = beam dir; z/w = cos(outer/inner); z <= -1.5 => no cone
149 ambient: vec4f, // rgb ambient
150 lightInfo: vec4f, // x = count, y = viewW, z = viewH
151 lights: array<Light2D, 8>,
153@group(0) @binding(0) var albedoTex: texture_2d<f32>;
154@group(0) @binding(1) var normalTex: texture_2d<f32>;
155@group(0) @binding(2) var mainSamp: sampler;
156@group(0) @binding(4) var<uniform> u: Lighting2D;
159fn applyNormalMap2D(mapSample: vec3f, logical: vec2f, uv: vec2f) -> vec3f {
160 var mapN = mapSample * 2.0 - vec3f(1.0);
161 mapN.z = max(mapN.z, 0.05);
162 let N = vec3f(0.0, 0.0, 1.0);
163 let dp1 = vec3f(dpdx(logical), 0.0);
164 let dp2 = vec3f(dpdy(logical), 0.0);
167 let det = duv1.x * duv2.y - duv2.x * duv1.y;
168 // Scale-aware singularity: |det| / (|duv1||duv2|) ≈ |sin θ| of the UV basis.
169 // Absolute 1e-6 rejects ordinary atlas regions (e.g. 32px in a 1024 atlas).
170 let uvScale = length(duv1) * length(duv2);
171 if (uvScale < 1e-20 || abs(det) < uvScale * 1e-3) {
173 return normalize(vec3f(mapN.xy, mapN.z));
175 let invDet = 1.0 / det;
176 var T = (dp1 * duv2.y - dp2 * duv1.y) * invDet;
177 var B = (dp2 * duv1.x - dp1 * duv2.x) * invDet;
178 T = T - N * dot(N, T);
179 let tLen = length(T);
180 let bLen = length(B);
181 if (tLen < 1e-4 || bLen < 1e-4) {
183 return normalize(vec3f(mapN.xy, mapN.z));
186 B = normalize(B - N * dot(N, B) - T * dot(T, B));
187 if (abs(dot(T, B)) > 0.35) {
189 return normalize(vec3f(mapN.xy, mapN.z));
192 return normalize(mat3x3f(T, B, N) * mapN);
197fn fs_main(in: FSIn) -> @location(0) vec4f {
198 let base = textureSample(albedoTex, mainSamp, in.uv) * in.color;
199 // WebGPU upload flips clip-space Y; undo it for the engine's Y-down logical coordinates.
200 let logicalNdc = vec2f(in.ndc.x, -in.ndc.y);
201 let logical = (logicalNdc * 0.5 + 0.5) * u.lightInfo.yz;
202 let normalSample = textureSample(normalTex, mainSamp, in.uv).xyz;
203 let normal = applyNormalMap2D(normalSample, logical, in.uv);
204 var lit = u.ambient.rgb;
205 let count = i32(u.lightInfo.x + 0.5);
206 for (var i = 0; i < 8; i = i + 1) {
207 if (i >= count) { break; }
209 var contribution = 0.0;
210 if (l.posRadius.w <= 0.0) {
211 // Zero-range spots must not fall through as directional (position ≠ beam dir).
212 if (l.spot.z > -1.5) {
215 let lightDirection = normalize(vec3f(l.posRadius.xy, 0.35));
216 contribution = max(dot(normal, lightDirection), 0.0);
219 let toLight = l.posRadius.xy - logical;
220 let distance = length(toLight);
221 var attenuation = clamp(1.0 - distance / max(l.posRadius.w, 1.0), 0.0, 1.0);
222 attenuation *= attenuation;
223 if (l.spot.z > -1.5) {
224 let fromLight = logical - l.posRadius.xy;
225 let fl = length(fromLight);
226 var spotAttenuation = 1.0;
228 let beamLen = length(l.spot.xy);
229 if (beamLen > 1e-6) {
230 let cosTheta = dot(fromLight / fl, l.spot.xy / beamLen);
231 spotAttenuation = smoothstep(l.spot.z, l.spot.w, cosTheta);
233 spotAttenuation = 0.0;
236 attenuation *= spotAttenuation;
238 let lightDirection = normalize(vec3f(toLight, l.posRadius.w * 0.35));
239 contribution = max(dot(normal, lightDirection), 0.0) * attenuation;
241 lit += l.color.rgb * contribution;
244 return vec4f(base.rgb * lit, base.a);
251@group(0) @binding(21) var<storage, read> skinBones: array<mat4x4f>;
255 @location(0) pos: vec3f,
256 @location(1) normal: vec3f,
257 @location(2) uv: vec2f,
258 @location(3) joints: vec4u,
259 @location(4) weights: vec4f,
260 @location(5) color: vec4f,
277 lights: array<Light3D, 8>,
285 virtualTexture: vec4f,
287 envProbeCenter: vec4f,
288 envProbeExtent: vec4f,
290 reflectionProbeCenter: array<vec4f, 2>,
291 reflectionProbeExtent: array<vec4f, 2>,
292 diffuseProbeSh: array<vec4f, 9>,
293 diffuseProbeInfo: vec4f,
294 diffuseVolumePosition: array<vec4f, 8>,
295 diffuseVolumeExtent: array<vec4f, 8>,
296 diffuseVolumeSh: array<vec4f, 72>,
297 diffuseVolumeInfo: vec4f,
303 @builtin(position) pos: vec4f,
304 @location(0) vNormal: vec3f,
305 @location(1) vUV: vec2f,
306 @location(2) vTint: vec4f,
307 @location(3) vWorldPos: vec3f,
308 @location(4) vCameraPos: vec3f,
309 @location(5) vViewPos: vec3f,
311@group(0) @binding(0) var<uniform> ubo: Frame;
312// WGSL has no stdlib inverse(); implement 3x3 inverse for the normal matrix.
314fn inverse3x3(m: mat3x3f) -> mat3x3f {
315 let a = m[0].x; let b = m[1].x; let c = m[2].x;
316 let d = m[0].y; let e = m[1].y; let f = m[2].y;
317 let g = m[0].z; let h = m[1].z; let i = m[2].z;
318 let det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
322 vec3f((e * i - f * h) / det, (f * g - d * i) / det, (d * h - e * g) / det),
324 vec3f((c * h - b * i) / det, (a * i - c * g) / det, (b * g - a * h) / det),
326 vec3f((b * f - c * e) / det, (c * d - a * f) / det, (a * e - b * d) / det),
331fn vs_main(in: VSIn) -> VSOut {
333 var localPos = vec4f(in.pos, 1.0);
334 var localNormal = in.normal;
335 if (ubo.skinInfo.x > 0.5) {
336 var weights = in.weights;
337 if (ubo.skinInfo.y < 3.5) { weights.z = 0.0; weights.w = 0.0; }
338 if (ubo.skinInfo.y < 1.5) { weights.y = 0.0; }
339 let weightSum = dot(weights, vec4f(1.0));
340 if (weightSum > 1e-8) { weights /= weightSum; }
341 let skin = weights.x * skinBones[in.joints.x]
342 + weights.y * skinBones[in.joints.y]
343 + weights.z * skinBones[in.joints.z]
344 + weights.w * skinBones[in.joints.w];
345 localPos = skin * localPos;
346 localNormal = mat3x3f(skin[0].xyz, skin[1].xyz, skin[2].xyz) * localNormal;
348 out.pos = ubo.mvp * localPos;
349 // WebGPU NDC is Y-up; mirror the Vulkan-convention clip Y.
350 out.pos.y = -out.pos.y;
351 let world = ubo.model * localPos;
352 out.vWorldPos = world.xyz;
353 out.vViewPos = (ubo.view * world).xyz;
354 let nrm = transpose(inverse3x3(mat3x3f(ubo.model[0].xyz, ubo.model[1].xyz, ubo.model[2].xyz))) * localNormal;
355 out.vNormal = normalize(nrm);
357 out.vTint = ubo.tint * in.color;
358 out.vCameraPos = ubo.cameraPos.xyz;
379 lights: array<Light3D, 8>,
387 virtualTexture: vec4f,
389 envProbeCenter: vec4f,
390 envProbeExtent: vec4f,
392 reflectionProbeCenter: array<vec4f, 2>,
393 reflectionProbeExtent: array<vec4f, 2>,
394 diffuseProbeSh: array<vec4f, 9>,
395 diffuseProbeInfo: vec4f,
396 diffuseVolumePosition: array<vec4f, 8>,
397 diffuseVolumeExtent: array<vec4f, 8>,
398 diffuseVolumeSh: array<vec4f, 72>,
399 diffuseVolumeInfo: vec4f,
404 lightVP: array<mat4x4f, 3>,
409 cascadeUvScale: vec4f,
410 localVP: array<mat4x4f, 4>,
418 @location(0) vNormal: vec3f,
419 @location(1) vUV: vec2f,
420 @location(2) vTint: vec4f,
421 @location(3) vWorldPos: vec3f,
422 @location(4) vCameraPos: vec3f,
423 @location(5) vViewPos: vec3f,
424 @builtin(position) fragCoord: vec4f,
426@group(0) @binding(0) var<uniform> ubo: Frame;
427@group(0) @binding(1) var albedoSampler: texture_2d<f32>;
428@group(0) @binding(2) var normalSampler: texture_2d<f32>;
429@group(0) @binding(3) var envSampler: texture_cube<f32>;
430@group(0) @binding(4) var<uniform> shadow: ShadowFrame;
431@group(0) @binding(5) var shadowMap: texture_depth_2d_array;
432@group(0) @binding(6) var heightSampler: texture_2d<f32>;
433@group(0) @binding(7) var mainSamp: sampler;
434@group(0) @binding(8) var shadowSamp: sampler_comparison;
435@group(0) @binding(10) var aoTex: texture_2d<f32>;
436@group(0) @binding(11) var aoSamp: sampler;
437@group(0) @binding(12) var decalAlbedoLayer: texture_2d<f32>;
438@group(0) @binding(13) var decalNormalLayer: texture_2d<f32>;
439@group(0) @binding(14) var decalParamsLayer: texture_2d<f32>;
442fn packVirtualTextureFeedback(sourceUv: vec2f, mipLevel: f32) -> u32 {
443 let pageCounts = max(ubo.virtualTexture.yz, vec2f(1.0));
444 let virtualCoord = fract(sourceUv) * pageCounts;
445 let page = clamp(vec2i(floor(virtualCoord)), vec2i(0), vec2i(pageCounts) - vec2i(1));
446 let mip = u32(clamp(mipLevel, 0.0, 255.0));
447 return u32(page.x) | (u32(page.y) << 12u) | (mip << 24u);
451fn sampleVirtualTexture(atlas: texture_2d<f32>, sourceUv: vec2f,
452 uvDx: vec2f, uvDy: vec2f) -> vec4f {
453 let uv = fract(sourceUv);
454 let pageCounts = max(ubo.virtualTexture.yz, vec2f(1.0));
455 let slots = max(ubo.virtualAtlas.xy, vec2f(1.0));
456 let virtualCoord = uv * pageCounts;
457 let pageLimit = vec2i(textureDimensions(heightSampler)) - vec2i(1);
458 let page = clamp(vec2i(floor(virtualCoord)), vec2i(0), pageLimit);
459 let entry = textureLoad(heightSampler, page, 0);
461 var derivativePages = vec2f(1.0);
463 payloadUv = fract(virtualCoord);
464 derivativePages = pageCounts;
466 let slot = floor(entry.rg * slots);
467 let gutter = clamp(ubo.virtualTexture.w, 0.0, 0.499);
468 let physicalUv = (slot + mix(vec2f(gutter), vec2f(1.0 - gutter), payloadUv)) / slots;
469 let derivativeScale = derivativePages * (1.0 - 2.0 * gutter) / slots;
471 return textureSampleGrad(atlas, mainSamp, physicalUv,
472 uvDx * derivativeScale, uvDy * derivativeScale);
475const PI: f32 = 3.14159265359;
478fn distGGX(n: vec3f, h: vec3f, rough: f32) -> f32 {
479 let a = max(rough * rough, 0.002);
481 let ndh = max(dot(n, h), 0.0);
482 let denom = (ndh * ndh * (a2 - 1.0) + 1.0);
483 return a2 / max(PI * denom * denom, 1e-4);
486fn geomSchlick(ndv: f32, rough: f32) -> f32 {
488 let k = (r * r) / 8.0;
489 return ndv / max(ndv * (1.0 - k) + k, 1e-4);
492fn geomSmith(n: vec3f, v: vec3f, l: vec3f, rough: f32) -> f32 {
494 return geomSchlick(max(dot(n, v), 0.0), rough) * geomSchlick(max(dot(n, l), 0.0), rough);
497fn fresnelSchlick(cosT: f32, f0: vec3f) -> vec3f {
498 return f0 + (1.0 - f0) * pow(clamp(1.0 - cosT, 0.0, 1.0), 5.0);
501fn texBombHash22(p: vec2f) -> vec2f {
502 var p3 = fract(vec3f(p.x, p.y, p.x) * vec3f(0.1031, 0.1030, 0.0973));
503 p3 += dot(p3, p3.yzx + 33.33);
505 return fract((p3.xx + p3.yz) * p3.zy);
508fn texBombRotate(v: vec2f, angle: f32) -> vec2f {
512 return vec2f(c * v.x - s * v.y, s * v.x + c * v.y);
515fn textureCellBomb(tex: texture_2d<f32>, uv: vec2f, cellScale: f32, strength: f32,
516 rotAmount: f32, dx: vec2f, dy: vec2f) -> vec4f {
517 if (strength < 1e-4) { return textureSample(tex, mainSamp, uv); }
518 let scale = max(cellScale, 1e-3);
522 let w = f * f * f * (f * (f * 6.0 - 15.0) + 10.0);
523 var accumulated = vec4f(0.0);
524 for (var j = 0; j <= 1; j = j + 1) {
525 for (var i = 0; i <= 1; i = i + 1) {
526 let ij = vec2f(f32(i), f32(j));
527 let cellIJ = cell + ij;
528 let random = texBombHash22(cellIJ);
529 let randomB = texBombHash22(cellIJ + vec2f(19.0, 47.0));
530 let offset = (random * 2.0 - 1.0) * (strength / scale);
531 let angle = (randomB.x * 2.0 - 1.0) * PI * clamp(rotAmount, 0.0, 1.0) * strength;
532 let center = (cellIJ + vec2f(0.5)) / scale;
533 let sampleUV = center + texBombRotate(uv - center, angle) + offset;
534 let weight = mix(1.0 - w.x, w.x, f32(i)) * mix(1.0 - w.y, w.y, f32(j));
535 accumulated += textureSampleGrad(tex, mainSamp, sampleUV, dx, dy) * weight;
541fn surfaceTBN(nInput: vec3f, dp1: vec3f, dp2: vec3f, duv1: vec2f,
542 duv2: vec2f) -> mat3x3f {
543 let n = normalize(nInput);
544 let det = duv1.x * duv2.y - duv2.x * duv1.y;
545 if (abs(det) < 1e-8) { return mat3x3f(vec3f(0.0), vec3f(0.0), n); }
546 let tangent = normalize(cross(dp2, n) * duv1.x + cross(n, dp1) * duv2.x);
547 let bitangent = normalize(cross(dp2, n) * duv1.y + cross(n, dp1) * duv2.y);
548 if (length(tangent) < 1e-4 || length(bitangent) < 1e-4) {
550 return mat3x3f(vec3f(0.0), vec3f(0.0), n);
553 return mat3x3f(tangent, bitangent, n);
556fn applyNormalMap(nInput: vec3f, mapSample: vec3f, dp1: vec3f, dp2: vec3f,
557 duv1: vec2f, duv2: vec2f) -> vec3f {
558 let mapN = mapSample * 2.0 - 1.0;
559 if (length(mapN.xy) < 0.04 && mapN.z > 0.85) { return normalize(nInput); }
560 let n = normalize(nInput);
561 let det = duv1.x * duv2.y - duv2.x * duv1.y;
562 if (abs(det) < 1e-6) { return n; }
563 let invDet = 1.0 / det;
564 var tangent = (dp1 * duv2.y - dp2 * duv1.y) * invDet;
565 var bitangent = (dp2 * duv1.x - dp1 * duv2.x) * invDet;
566 tangent -= n * dot(n, tangent);
567 let tangentLength = length(tangent);
568 let bitangentLength = length(bitangent);
569 if (tangentLength < 1e-4 || bitangentLength < 1e-4) { return n; }
570 tangent /= tangentLength;
571 bitangent = normalize(bitangent - n * dot(n, bitangent) - tangent * dot(tangent, bitangent));
572 if (abs(dot(tangent, bitangent)) > 0.35) { return n; }
574 return normalize(mat3x3f(tangent, bitangent, n) * mapN);
577fn parallaxMappedUV(uv: vec2f, n: vec3f, v: vec3f, scale: f32,
578 minLayers: f32, maxLayers: f32, worldDx: vec3f, worldDy: vec3f,
579 uvDx: vec2f, uvDy: vec2f) -> vec2f {
580 if (scale < 1e-5) { return uv; }
581 let tbn = surfaceTBN(n, worldDx, worldDy, uvDx, uvDy);
582 if (length(tbn[0]) < 1e-4) { return uv; }
583 let viewTS = normalize(transpose(tbn) * v);
584 let layers = clamp(mix(max(maxLayers, 1.0), max(minLayers, 1.0),
586 clamp(abs(viewTS.z), 0.0, 1.0)), 1.0, 64.0);
587 let layerDepth = 1.0 / layers;
588 let deltaUV = ((viewTS.xy / max(abs(viewTS.z), 0.08)) * scale) / layers;
590 var currentDepth = 0.0;
591 var mapDepth = 1.0 - textureSampleGrad(heightSampler, mainSamp, currentUV, uvDx, uvDy).r;
592 for (var i = 0; i < 64; i = i + 1) {
593 if (currentDepth >= mapDepth || f32(i) >= layers) { break; }
594 currentUV -= deltaUV;
595 mapDepth = 1.0 - textureSampleGrad(heightSampler, mainSamp, currentUV, uvDx, uvDy).r;
596 currentDepth += layerDepth;
598 let previousUV = currentUV + deltaUV;
599 let after = mapDepth - currentDepth;
600 let before = (1.0 - textureSampleGrad(heightSampler, mainSamp, previousUV, uvDx, uvDy).r) -
601 (currentDepth - layerDepth);
602 let denominator = after - before;
604 if (abs(denominator) >= 1e-5) { weight = clamp(after / denominator, 0.0, 1.0); }
606 return mix(currentUV, previousUV, weight);
609fn shadeLight(n: vec3f, v: vec3f, albedo: vec3f, metallic: f32, rough: f32, l: vec3f, rad: vec3f) -> vec3f {
610 let ndl = max(dot(n, l), 0.0);
611 let diffuse = mix(ndl, ndl * 0.5 + 0.5, 0.25);
612 let h = normalize(v + l);
613 let f0 = mix(vec3f(0.04), albedo, metallic);
614 let ndf = distGGX(n, h, rough);
615 let g = geomSmith(n, v, l, rough);
616 let f = fresnelSchlick(max(dot(h, v), 0.0), f0);
617 let spec = (ndf * g * f) / max(4.0 * max(dot(n, v), 0.0) * max(ndl, 0.001), 1e-3);
618 let kd = (vec3f(1.0) - f) * (1.0 - metallic);
619 return (kd * albedo * diffuse + spec * ndl) * rad;
622fn cloudHash(p: vec2f) -> f32 {
623 let ip = vec2i(floor(p % 64.0));
624 var h = (u32(ip.x) * 374761393u) ^ (u32(ip.y) * 668265263u);
625 h = (h ^ (h >> 13)) * 1274126177u;
628 return f32(h & 0x00FFFFFFu) / f32(0x00FFFFFFu);
631fn cloudNoise(p: vec2f) -> f32 {
634 f = f * f * (3.0 - 2.0 * f);
635 let a = cloudHash(i);
636 let b = cloudHash(i + vec2f(1.0, 0.0));
637 let c = cloudHash(i + vec2f(0.0, 1.0));
638 let d = cloudHash(i + vec2f(1.0, 1.0));
640 return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
643fn cloudFbm(p: vec2f) -> f32 {
647 for (var i = 0; i < 4; i = i + 1) {
648 sum += amp * cloudNoise(p * freq);
655fn cloudShadowFactor(worldPos: vec3f) -> f32 {
656 if (ubo.cloud.x < 1e-4) { return 1.0; }
657 let cell = 1.0 / max(ubo.cloud.y, 1e-4);
658 let drift = (ubo.cloudWind.xy * cell) * ubo.cloud.z;
659 let p = worldPos.xz * cell - drift;
661 let c = smoothstep(ubo.cloudWind.z - 0.1, ubo.cloudWind.z + 0.1, f);
662 let d = cloudNoise(p * 3.0 + vec2f(11.7, 5.3));
663 let covered = mix(c, c * d, ubo.cloudWind.w);
664 return 1.0 - clamp(covered, 0.0, 1.0) * clamp(ubo.cloud.x, 0.0, 1.0);
667fn sampleShadowCascade(worldPos: vec3f, cascade: i32, bias: f32) -> f32 {
668 let lightClip = shadow.lightVP[cascade] * vec4f(worldPos, 1.0);
669 let ndc = lightClip.xyz / max(lightClip.w, 1e-6);
670 // Shadow-map vertices mirror clip Y for WebGPU. Undo that mirror when
671 // projecting the shared Vulkan-convention light matrix for sampling.
672 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
674 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || depth < 0.0 || depth > 1.0) {
677 var uvScale = shadow.cascadeUvScale.z;
678 if (cascade == 0) { uvScale = shadow.cascadeUvScale.x; }
679 else if (cascade == 1) { uvScale = shadow.cascadeUvScale.y; }
680 uvScale = max(uvScale, 1e-4);
681 let mapSize = max(shadow.cascadeUvScale.w, 1.0) * uvScale;
682 let texel = vec2f(1.0 / mapSize);
684 let zref = depth - bias;
685 for (var i = 0; i < 9; i = i + 1) {
687 if (i == 0) { s = uv + vec2f(-0.5, -0.5) * texel; }
688 else if (i == 1) { s = uv + vec2f(0.0, -0.6) * texel; }
689 else if (i == 2) { s = uv + vec2f(0.5, -0.5) * texel; }
690 else if (i == 3) { s = uv + vec2f(-0.6, 0.0) * texel; }
691 else if (i == 4) { s = uv; }
692 else if (i == 5) { s = uv + vec2f(0.6, 0.0) * texel; }
693 else if (i == 6) { s = uv + vec2f(-0.5, 0.5) * texel; }
694 else if (i == 7) { s = uv + vec2f(0.0, 0.6) * texel; }
695 else { s = uv + vec2f(0.5, 0.5) * texel; }
696 if (s.x >= 0.0 && s.x <= 1.0 && s.y >= 0.0 && s.y <= 1.0) {
697 sum += textureSampleCompareLevel(shadowMap, shadowSamp, s * uvScale, cascade, zref);
703fn cascadeNdcBias(cascade: i32) -> f32 {
705 if (cascade == 0) { bias = shadow.cascadeBias.x; }
706 else if (cascade == 1) { bias = shadow.cascadeBias.y; }
707 else { bias = shadow.cascadeBias.z; }
709 return select(bias, shadow.bias.x, bias < 1e-8);
712fn slopeScaledBias(cascade: i32, ndl: f32) -> f32 {
714 return cascadeNdcBias(cascade) * mix(0.75, 1.0, clamp(ndl, 0.0, 1.0));
717fn sampleShadowPCF(worldPos: vec3f, n: vec3f, viewDepth: f32, ndl: f32) -> f32 {
718 if (shadow.bias.y < 0.5 || shadow.bias.z < 0.5 || shadow.splits.w < 1e-4) {
721 var cascade: i32 = 2;
722 if (viewDepth < shadow.splits.x) { cascade = 0; }
723 else if (viewDepth < shadow.splits.y) { cascade = 1; }
725 if (cascade == 0) { tw = shadow.cascadeTexel.x; }
726 else if (cascade == 1) { tw = shadow.cascadeTexel.y; }
727 else { tw = shadow.cascadeTexel.z; }
728 let p = worldPos + n * ((2.0 * max(tw, 1e-6)) / max(ndl, 0.2));
729 var vis = sampleShadowCascade(p, cascade, slopeScaledBias(cascade, ndl));
734 hi = shadow.splits.x;
736 } else if (cascade == 1) {
737 hi = shadow.splits.y;
738 lo = shadow.splits.x;
740 hi = shadow.splits.z;
741 lo = shadow.splits.y;
743 let band = max(0.5, (hi - lo) * 0.1);
744 let toPrev = 1.0 - clamp((viewDepth - lo) / band, 0.0, 1.0);
745 let toNext = 1.0 - clamp((hi - viewDepth) / band, 0.0, 1.0);
746 if (toPrev > 0.0 && cascade > 0) {
747 let previous = sampleShadowCascade(p, cascade - 1, slopeScaledBias(cascade - 1, ndl));
748 vis = mix(vis, previous, toPrev);
750 if (toNext > 0.0 && cascade < 2) {
751 let next = sampleShadowCascade(p, cascade + 1, slopeScaledBias(cascade + 1, ndl));
752 vis = mix(vis, next, toNext);
754 vis = mix(1.0, vis, clamp(shadow.splits.w, 0.0, 1.0));
756 return mix(0.04, 1.0, vis);
760fn fs_main(in: FSIn) -> @location(0) vec4f {
761 // Evaluate derivatives before any per-fragment branch; WGSL requires uniform control flow.
762 let uvDx = dpdx(in.vUV);
763 let uvDy = dpdy(in.vUV);
764 let worldDx = dpdx(in.vWorldPos);
765 let worldDy = dpdy(in.vWorldPos);
766 if (ubo.lodFade.z > 0.5) {
767 let lodHash = fract(dot(floor(in.fragCoord.xy), vec2f(0.06711056, 0.00583715)));
768 let rejected = select((lodHash < 1.0 - ubo.lodFade.x),
769 (lodHash >= ubo.lodFade.x), ubo.lodFade.y < 0.5);
770 if (rejected) { discard; }
772 var nGeom = normalize(in.vNormal);
773 let v = normalize(in.vCameraPos - in.vWorldPos);
774 if (dot(nGeom, v) < 0.0) { nGeom = -nGeom; }
776 if (ubo.virtualTexture.x < 0.5) {
777 uv = parallaxMappedUV(in.vUV, nGeom, v, ubo.parallax.x,
778 ubo.parallax.y, ubo.parallax.z, worldDx, worldDy, uvDx, uvDy);
781 if (ubo.virtualTexture.x > 0.5) {
782 base = sampleVirtualTexture(albedoSampler, uv, uvDx, uvDy) * in.vTint;
784 base = textureCellBomb(albedoSampler, uv, ubo.texBomb.x, ubo.texBomb.y,
785 ubo.texBomb.z, uvDx, uvDy) * in.vTint;
787 if (ubo.surface.x > 0.5 && ubo.surface.x < 1.5 && base.a < ubo.surface.y) {
790 // Screen-space primary; UV secondary so coverage still varies if fragment
791 // position is degenerate (Dawn Metal CI once kept every pixel with fragCoord-only).
792 let alphaHash = fract(dot(floor(in.fragCoord.xy), vec2f(0.06711056, 0.00583715)) +
794 dot(uv * 64.0, vec2f(0.7548777, 0.5698403)));
795 if (ubo.surface.x > 2.5 && base.a < alphaHash) { discard; }
796 var albedo = base.rgb;
797 var metallic = clamp(ubo.ambient.w, 0.0, 1.0);
798 var rough = clamp(ubo.cameraPos.w, 0.04, 1.0);
799 let count = i32(ubo.lightDir.w + 0.5);
800 var packedNormal: vec4f;
801 if (ubo.virtualTexture.x > 0.5) {
802 packedNormal = sampleVirtualTexture(normalSampler, uv, uvDx, uvDy);
804 packedNormal = textureCellBomb(normalSampler, uv, ubo.texBomb.x, ubo.texBomb.y,
805 ubo.texBomb.z, uvDx, uvDy);
807 var nSmp = packedNormal.xyz;
809 if (ubo.virtualAtlas.z > 0.5) {
810 let normalXY = packedNormal.rg * 2.0 - 1.0;
811 nSmp = vec3f(packedNormal.rg, sqrt(1.0 - clamp(dot(normalXY, normalXY), 0.0, 1.0)) * 0.5 + 0.5);
812 terrainAo = clamp(packedNormal.b, 0.0, 1.0);
813 rough = clamp(1.0 - packedNormal.a, 0.04, 1.0);
816 if (length(nSmp - vec3f(0.5, 0.5, 1.0)) > 0.04) {
817 n = applyNormalMap(n, nSmp, worldDx, worldDy, uvDx, uvDy);
819 var emissive = vec3f(0.0);
820 let decalPos = clamp(vec2<i32>(in.fragCoord.xy), vec2<i32>(0),
821 vec2<i32>(textureDimensions(decalAlbedoLayer)) - vec2<i32>(1));
822 let decalA = textureLoad(decalAlbedoLayer, decalPos, 0);
823 let decalCoverage = clamp(decalA.a, 0.0, 1.0);
824 if (decalCoverage > 0.001) {
825 albedo = mix(albedo, decalA.rgb, decalCoverage);
826 let decalN = textureLoad(decalNormalLayer, decalPos, 0);
827 let normalWeight = clamp(decalN.a, 0.0, 1.0);
828 if (normalWeight > 0.001) {
829 n = normalize(mix(n, normalize(decalN.rgb * 2.0 - 1.0), normalWeight));
831 let decalP = textureLoad(decalParamsLayer, decalPos, 0);
832 let paramsWeight = clamp(decalP.a, 0.0, 1.0);
833 if (paramsWeight > 0.001) {
834 rough = mix(rough, decalP.r, paramsWeight);
835 metallic = mix(metallic, decalP.g, paramsWeight);
836 emissive += decalA.rgb * decalP.b;
840 let viewDepth = max(-in.vViewPos.z, 0.0);
841 let primaryL = normalize(ubo.lightDir.xyz);
842 let shadowVis = sampleShadowPCF(in.vWorldPos, n, viewDepth, max(dot(n, primaryL), 0.0));
843 if (length(ubo.lightColor.rgb) > 1e-6) {
844 lo += shadeLight(n, v, albedo, metallic, rough, primaryL, ubo.lightColor.rgb) * shadowVis *
846 cloudShadowFactor(in.vWorldPos);
848 for (var i = 0; i < 8; i = i + 1) {
849 if (i >= count) { break; }
850 let lgt = ubo.lights[i];
851 var rad = lgt.color.rgb;
853 if (lgt.posRadius.w <= 0.0) {
854 l = normalize(lgt.posRadius.xyz);
855 if (length(l - primaryL) < 1e-3) { continue; }
857 let toL = lgt.posRadius.xyz - in.vWorldPos;
858 let dist = length(toL);
859 l = toL / max(dist, 1e-4);
860 var atten = clamp(1.0 - dist / max(lgt.posRadius.w, 1e-3), 0.0, 1.0);
861 atten = atten * atten;
862 if (lgt.spot.w > 0.0) {
863 let beamLen = length(lgt.spot.xyz);
864 if (beamLen > 1e-6) {
865 let cosTheta = dot(normalize(-l), lgt.spot.xyz / beamLen);
866 atten = atten * clamp(cosTheta * lgt.spot.w + lgt.color.a, 0.0, 1.0);
867 // Local spot shadow slot encoded as |beam| = 1 + (slot+1)/100.
868 if (beamLen > 1.005 && shadow.bias.z > 0.5) {
869 let si = i32(round((beamLen - 1.0) * 100.0)) - 1;
870 if (si >= 0 && si <= 3) {
871 let lc = shadow.localVP[si] * vec4f(in.vWorldPos, 1.0);
872 let ndc = lc.xyz / max(lc.w, 1e-4);
873 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
874 let zref = ndc.z - shadow.localBias[si];
876 if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && lc.w > 0.0) {
877 vis = textureSampleCompareLevel(shadowMap, shadowSamp, uv, 3 + si, zref);
879 atten = atten * mix(1.0 - shadow.splits.w, 1.0, vis);
888 lo += shadeLight(n, v, albedo, metallic, rough, l, rad);
890 let hemi = clamp(n.y * 0.5 + 0.5, 0.0, 1.0);
891 let skyIrr = ubo.ambient.rgb * 1.1 + ubo.lightColor.rgb * 0.12;
892 let gndIrr = ubo.ambient.rgb * vec3f(0.72, 0.62, 0.52);
893 var irr = mix(gndIrr, skyIrr, hemi);
894 let shBasis = array<f32, 9>(0.2820947918, 0.4886025119 * n.y, 0.4886025119 * n.z,
895 0.4886025119 * n.x, 1.0925484306 * n.x * n.y, 1.0925484306 * n.y * n.z,
896 0.3153915653 * (3.0 * n.z * n.z - 1.0), 1.0925484306 * n.x * n.z,
897 0.5462742153 * (n.x * n.x - n.y * n.y));
898 if (ubo.diffuseVolumeInfo.x > 0.5) {
899 var volumeIrradiance = vec3f(0.0);
900 var volumeWeight = 0.0;
901 var cellMin = ubo.diffuseVolumePosition[0].xyz;
902 var cellMax = cellMin;
903 if (ubo.diffuseVolumeInfo.y > 0.5) {
904 for (var probe = 1; probe < 8; probe += 1) {
905 if (probe >= i32(ubo.diffuseVolumeInfo.x + 0.5)) { break; }
906 cellMin = min(cellMin, ubo.diffuseVolumePosition[probe].xyz);
907 cellMax = max(cellMax, ubo.diffuseVolumePosition[probe].xyz);
910 let cellT = clamp((in.vWorldPos - cellMin) / max(cellMax - cellMin, vec3f(0.0001)),
912 vec3f(0.0), vec3f(1.0));
913 for (var probe = 0; probe < 8; probe += 1) {
914 if (probe >= i32(ubo.diffuseVolumeInfo.x + 0.5)) { break; }
915 let extent = max(ubo.diffuseVolumeExtent[probe].xyz, vec3f(0.0001));
916 let delta = abs(in.vWorldPos - ubo.diffuseVolumePosition[probe].xyz);
917 if (ubo.diffuseVolumeInfo.y > 0.5 || all(delta <= extent)) {
919 if (ubo.diffuseVolumeInfo.y > 0.5) {
920 let activeAxis = (cellMax - cellMin) > vec3f(0.0001);
921 let lowerCorner = abs(ubo.diffuseVolumePosition[probe].xyz - cellMax) > vec3f(0.0001);
922 var axisWeight = select(cellT, vec3f(1.0) - cellT, lowerCorner);
923 axisWeight = select(vec3f(1.0), axisWeight, activeAxis);
924 weight = axisWeight.x * axisWeight.y * axisWeight.z;
926 weight = 1.0 / max(length(delta / extent), 0.05);
928 var probeIrradiance = vec3f(0.0);
929 for (var coefficient = 0; coefficient < 9; coefficient += 1) {
930 probeIrradiance += ubo.diffuseVolumeSh[probe * 9 + coefficient].rgb *
931 shBasis[coefficient];
933 volumeIrradiance += max(probeIrradiance, vec3f(0.0)) * weight;
934 volumeWeight += weight;
937 if (volumeWeight > 0.0) { irr = volumeIrradiance / volumeWeight; }
938 } else if (ubo.diffuseProbeInfo.x > 0.5) {
940 for (var coefficient = 0; coefficient < 9; coefficient += 1) {
941 irr += ubo.diffuseProbeSh[coefficient].rgb * shBasis[coefficient];
943 irr = max(irr, vec3f(0.0));
945 var color = albedo * irr * (1.0 - metallic) * terrainAo + lo;
946 let wrap = max(dot(n, primaryL) * 0.5 + 0.5, 0.0);
947 color += albedo * ubo.lightColor.rgb * (wrap * wrap) * 0.06 * (1.0 - metallic) * terrainAo;
948 let envIntensity = ubo.lightColor.w;
949 if (envIntensity > 1e-4) {
950 let r = reflect(-v, n);
951 let envSpec = textureSampleLevel(envSampler, mainSamp, r, rough * 5.0).rgb * envIntensity;
952 let f0 = mix(vec3f(0.04), albedo, metallic);
953 let f = fresnelSchlick(max(dot(n, v), 0.0), f0);
954 color += envSpec * f;
955 let irr2 = textureSampleLevel(envSampler, mainSamp, n, 5.0).rgb * envIntensity;
956 color += albedo * irr2 * (1.0 - metallic) * (1.0 - f) * 0.45 * terrainAo;
958 // Screen-space ambient occlusion (G-buffer SSAO pass output; strength in
959 // texBomb.w is 0 when AO is disabled via RenderControl, which also keeps
960 // the binding white in that case).
961 // AO is rendered at half the scene resolution; map the full-res fragment
962 // coordinate into AO texel space before normalizing, otherwise the UV
963 // overruns into [0,2] and clamps to the AO edge (misaligned occlusion).
964 let aoUV = (in.fragCoord.xy * 0.5) / vec2f(textureDimensions(aoTex));
965 let ao = textureSampleLevel(aoTex, aoSamp, aoUV, 0.0).r;
966 color *= mix(1.0, ao, clamp(ubo.surface.z, 0.0, 1.0));
968 let outputAlpha = select(1.0, base.a, ubo.surface.x > 1.5 && ubo.surface.x < 2.5);
970 return vec4f(color, outputAlpha);
978 @location(0) pos: vec3f,
979 @location(1) normal: vec3f,
980 @location(2) uv: vec2f,
981 @location(5) color: vec4f,
998 virtualTexture: vec4f,
1000 envProbeCenter: vec4f,
1001 envProbeExtent: vec4f,
1002 reflectionProbeCenter: array<vec4f, 2>,
1003 reflectionProbeExtent: array<vec4f, 2>,
1007 @builtin(position) pos: vec4f,
1008 @location(0) vNormal: vec3f,
1009 @location(1) vUV: vec2f,
1010 @location(2) vTint: vec4f,
1011 @location(3) vWorldPos: vec3f,
1012 @location(4) vCameraPos: vec3f,
1013 @location(5) vViewPos: vec3f,
1015@group(0) @binding(0) var<uniform> ubo: Frame;
1017fn inverse3x3(m: mat3x3f) -> mat3x3f {
1018 let a = m[0].x; let b = m[1].x; let c = m[2].x;
1019 let d = m[0].y; let e = m[1].y; let f = m[2].y;
1020 let g = m[0].z; let h = m[1].z; let i = m[2].z;
1021 let det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
1025 vec3f((e * i - f * h) / det, (f * g - d * i) / det, (d * h - e * g) / det),
1027 vec3f((c * h - b * i) / det, (a * i - c * g) / det, (b * g - a * h) / det),
1029 vec3f((b * f - c * e) / det, (c * d - a * f) / det, (a * e - b * d) / det),
1034fn vs_main(in: VSIn) -> VSOut {
1036 out.pos = ubo.mvp * vec4f(in.pos, 1.0);
1037 // WebGPU NDC is Y-up; mirror the Vulkan-convention clip Y.
1038 out.pos.y = -out.pos.y;
1039 let world = ubo.model * vec4f(in.pos, 1.0);
1040 out.vWorldPos = world.xyz;
1041 out.vViewPos = (ubo.view * world).xyz;
1042 let nrm = transpose(inverse3x3(mat3x3f(ubo.model[0].xyz, ubo.model[1].xyz, ubo.model[2].xyz))) * in.normal;
1043 out.vNormal = normalize(nrm);
1045 out.vTint = ubo.tint * in.color;
1046 out.vCameraPos = ubo.cameraPos.xyz;
1073 virtualTexture: vec4f,
1074 virtualAtlas: vec4f,
1078 lightVP: array<mat4x4f, 3>,
1082 cascadeTexel: vec4f,
1083 cascadeUvScale: vec4f,
1084 localVP: array<mat4x4f, 4>,
1092 @location(0) vNormal: vec3f,
1093 @location(1) vUV: vec2f,
1094 @location(2) vTint: vec4f,
1095 @location(3) vWorldPos: vec3f,
1096 @location(4) vCameraPos: vec3f,
1097 @location(5) vViewPos: vec3f,
1098 @builtin(position) fragCoord: vec4f,
1100@group(0) @binding(0) var<uniform> ubo: Frame;
1101@group(0) @binding(1) var albedoSampler: texture_2d<f32>;
1102@group(0) @binding(2) var normalSampler: texture_2d<f32>;
1103@group(0) @binding(3) var envSampler: texture_cube<f32>;
1104@group(0) @binding(4) var<uniform> shadow: ShadowFrame;
1105@group(0) @binding(5) var shadowMap: texture_depth_2d_array;
1106@group(0) @binding(6) var heightSampler: texture_2d<f32>;
1107@group(0) @binding(7) var mainSamp: sampler;
1108@group(0) @binding(8) var shadowSamp: sampler_comparison;
1109@group(0) @binding(9) var sceneDepth: texture_depth_2d;
1110@group(0) @binding(10) var<storage, read> lights: array<Light3D>;
1111@group(0) @binding(11) var<storage, read> clusterTable: array<vec2u>;
1112@group(0) @binding(12) var<storage, read> lightIndices: array<u32>;
1113@group(0) @binding(13) var aoTex: texture_2d<f32>;
1114@group(0) @binding(14) var aoSamp: sampler;
1115@group(0) @binding(15) var decalAlbedoLayer: texture_2d<f32>;
1116@group(0) @binding(16) var decalNormalLayer: texture_2d<f32>;
1117@group(0) @binding(17) var decalParamsLayer: texture_2d<f32>;
1120fn sampleClusteredVirtualTexture(atlas: texture_2d<f32>, sourceUv: vec2f,
1121 uvDx: vec2f, uvDy: vec2f) -> vec4f {
1122 let uv = fract(sourceUv);
1123 let pageCounts = max(ubo.virtualTexture.yz, vec2f(1.0));
1124 let slots = max(ubo.virtualAtlas.xy, vec2f(1.0));
1125 let virtualCoord = uv * pageCounts;
1126 let pageLimit = vec2i(textureDimensions(heightSampler)) - vec2i(1);
1127 let page = clamp(vec2i(floor(virtualCoord)), vec2i(0), pageLimit);
1128 let entry = textureLoad(heightSampler, page, 0);
1130 var derivativePages = vec2f(1.0);
1131 if (entry.b > 0.5) {
1132 payloadUv = fract(virtualCoord);
1133 derivativePages = pageCounts;
1135 let slot = floor(entry.rg * slots);
1136 let gutter = clamp(ubo.virtualTexture.w, 0.0, 0.499);
1137 let physicalUv = (slot + mix(vec2f(gutter), vec2f(1.0 - gutter), payloadUv)) / slots;
1138 let derivativeScale = derivativePages * (1.0 - 2.0 * gutter) / slots;
1140 return textureSampleGrad(atlas, mainSamp, physicalUv,
1141 uvDx * derivativeScale, uvDy * derivativeScale);
1144const PI: f32 = 3.14159265359;
1147fn distGGX(n: vec3f, h: vec3f, rough: f32) -> f32 {
1148 let a = max(rough * rough, 0.002);
1150 let ndh = max(dot(n, h), 0.0);
1151 let denom = (ndh * ndh * (a2 - 1.0) + 1.0);
1152 return a2 / max(PI * denom * denom, 1e-4);
1155fn geomSchlick(ndv: f32, rough: f32) -> f32 {
1156 let r = rough + 1.0;
1157 let k = (r * r) / 8.0;
1158 return ndv / max(ndv * (1.0 - k) + k, 1e-4);
1161fn geomSmith(n: vec3f, v: vec3f, l: vec3f, rough: f32) -> f32 {
1163 return geomSchlick(max(dot(n, v), 0.0), rough) * geomSchlick(max(dot(n, l), 0.0), rough);
1166fn fresnelSchlick(cosT: f32, f0: vec3f) -> vec3f {
1167 return f0 + (1.0 - f0) * pow(clamp(1.0 - cosT, 0.0, 1.0), 5.0);
1170fn shadeLight(n: vec3f, v: vec3f, albedo: vec3f, metallic: f32, rough: f32, l: vec3f, rad: vec3f) -> vec3f {
1171 let ndl = max(dot(n, l), 0.0);
1172 let diffuse = mix(ndl, ndl * 0.5 + 0.5, 0.25);
1173 let h = normalize(v + l);
1174 let f0 = mix(vec3f(0.04), albedo, metallic);
1175 let ndf = distGGX(n, h, rough);
1176 let g = geomSmith(n, v, l, rough);
1177 let f = fresnelSchlick(max(dot(h, v), 0.0), f0);
1178 let spec = (ndf * g * f) / max(4.0 * max(dot(n, v), 0.0) * max(ndl, 0.001), 1e-3);
1179 let kd = (vec3f(1.0) - f) * (1.0 - metallic);
1180 return (kd * albedo * diffuse + spec * ndl) * rad;
1183fn sampleShadowCascade(worldPos: vec3f, cascade: i32, bias: f32) -> f32 {
1184 let lightClip = shadow.lightVP[cascade] * vec4f(worldPos, 1.0);
1185 let ndc = lightClip.xyz / max(lightClip.w, 1e-6);
1186 // Shadow-map vertices mirror clip Y for WebGPU. Undo that mirror when
1187 // projecting the shared Vulkan-convention light matrix for sampling.
1188 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
1190 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || depth < 0.0 || depth > 1.0) {
1193 var uvScale = shadow.cascadeUvScale.z;
1194 if (cascade == 0) { uvScale = shadow.cascadeUvScale.x; }
1195 else if (cascade == 1) { uvScale = shadow.cascadeUvScale.y; }
1196 uvScale = max(uvScale, 1e-4);
1197 let mapSize = max(shadow.cascadeUvScale.w, 1.0) * uvScale;
1198 let texel = vec2f(1.0 / mapSize);
1200 let zref = depth - bias;
1201 for (var i = 0; i < 9; i = i + 1) {
1203 if (i == 0) { s = uv + vec2f(-0.5, -0.5) * texel; }
1204 else if (i == 1) { s = uv + vec2f(0.0, -0.6) * texel; }
1205 else if (i == 2) { s = uv + vec2f(0.5, -0.5) * texel; }
1206 else if (i == 3) { s = uv + vec2f(-0.6, 0.0) * texel; }
1207 else if (i == 4) { s = uv; }
1208 else if (i == 5) { s = uv + vec2f(0.6, 0.0) * texel; }
1209 else if (i == 6) { s = uv + vec2f(-0.5, 0.5) * texel; }
1210 else if (i == 7) { s = uv + vec2f(0.0, 0.6) * texel; }
1211 else { s = uv + vec2f(0.5, 0.5) * texel; }
1212 if (s.x >= 0.0 && s.x <= 1.0 && s.y >= 0.0 && s.y <= 1.0) {
1213 sum += textureSampleCompareLevel(shadowMap, shadowSamp, s * uvScale, cascade, zref);
1219fn cascadeNdcBias(cascade: i32) -> f32 {
1221 if (cascade == 0) { bias = shadow.cascadeBias.x; }
1222 else if (cascade == 1) { bias = shadow.cascadeBias.y; }
1223 else { bias = shadow.cascadeBias.z; }
1225 return select(bias, shadow.bias.x, bias < 1e-8);
1228fn slopeScaledBias(cascade: i32, ndl: f32) -> f32 {
1230 return cascadeNdcBias(cascade) * mix(0.75, 1.0, clamp(ndl, 0.0, 1.0));
1233fn sampleShadowPCF(worldPos: vec3f, n: vec3f, viewDepth: f32, ndl: f32) -> f32 {
1234 if (shadow.bias.y < 0.5 || shadow.bias.z < 0.5 || shadow.splits.w < 1e-4) {
1237 var cascade: i32 = 2;
1238 if (viewDepth < shadow.splits.x) { cascade = 0; }
1239 else if (viewDepth < shadow.splits.y) { cascade = 1; }
1241 if (cascade == 0) { tw = shadow.cascadeTexel.x; }
1242 else if (cascade == 1) { tw = shadow.cascadeTexel.y; }
1243 else { tw = shadow.cascadeTexel.z; }
1244 let p = worldPos + n * ((2.0 * max(tw, 1e-6)) / max(ndl, 0.2));
1245 var vis = sampleShadowCascade(p, cascade, slopeScaledBias(cascade, ndl));
1250 hi = shadow.splits.x;
1252 } else if (cascade == 1) {
1253 hi = shadow.splits.y;
1254 lo = shadow.splits.x;
1256 hi = shadow.splits.z;
1257 lo = shadow.splits.y;
1259 let band = max(0.5, (hi - lo) * 0.1);
1260 let toPrev = 1.0 - clamp((viewDepth - lo) / band, 0.0, 1.0);
1261 let toNext = 1.0 - clamp((hi - viewDepth) / band, 0.0, 1.0);
1262 if (toPrev > 0.0 && cascade > 0) {
1263 let previous = sampleShadowCascade(p, cascade - 1, slopeScaledBias(cascade - 1, ndl));
1264 vis = mix(vis, previous, toPrev);
1266 if (toNext > 0.0 && cascade < 2) {
1267 let next = sampleShadowCascade(p, cascade + 1, slopeScaledBias(cascade + 1, ndl));
1268 vis = mix(vis, next, toNext);
1270 vis = mix(1.0, vis, clamp(shadow.splits.w, 0.0, 1.0));
1272 return mix(0.04, 1.0, vis);
1275fn clusterIndex(frag: vec2f, viewDepth: f32) -> u32 {
1276 let tilesX = i32(ubo.gridInfo.x + 0.5);
1277 let tilesY = i32(ubo.gridInfo.y + 0.5);
1278 let slices = i32(ubo.gridInfo.z + 0.5);
1279 let nearZ = max(ubo.clipInfo.x, 1e-3);
1280 let farZ = max(ubo.clipInfo.y, nearZ + 1e-3);
1281 let screenW = max(ubo.clipInfo.z, 1.0);
1282 let screenH = max(ubo.clipInfo.w, 1.0);
1283 let tx = clamp(i32(floor(frag.x / screenW * f32(tilesX))), 0, tilesX - 1);
1284 let ty = clamp(i32(floor(frag.y / screenH * f32(tilesY))), 0, tilesY - 1);
1285 let depth = max(viewDepth, nearZ);
1286 let sz = clamp(i32(floor((depth - nearZ) / (farZ - nearZ) * f32(slices))), 0, slices - 1);
1288 return u32((sz * tilesY + ty) * tilesX + tx);
1291fn applyClusteredNormalMap(nInput: vec3f, mapSample: vec3f, dp1: vec3f, dp2: vec3f,
1292 duv1: vec2f, duv2: vec2f) -> vec3f {
1293 let mapN = mapSample * 2.0 - 1.0;
1294 let n = normalize(nInput);
1295 let det = duv1.x * duv2.y - duv2.x * duv1.y;
1296 if (abs(det) < 1e-6) { return n; }
1297 let invDet = 1.0 / det;
1298 var tangent = (dp1 * duv2.y - dp2 * duv1.y) * invDet;
1299 var bitangent = (dp2 * duv1.x - dp1 * duv2.x) * invDet;
1300 tangent -= n * dot(n, tangent);
1301 if (length(tangent) < 1e-4 || length(bitangent) < 1e-4) { return n; }
1302 tangent = normalize(tangent);
1303 bitangent = normalize(bitangent - n * dot(n, bitangent) - tangent * dot(tangent, bitangent));
1305 return normalize(mat3x3f(tangent, bitangent, n) * mapN);
1309fn fs_main(in: FSIn) -> @location(0) vec4f {
1310 let uvDx = dpdx(in.vUV);
1311 let uvDy = dpdy(in.vUV);
1312 var nGeom = normalize(in.vNormal);
1313 let v = normalize(in.vCameraPos - in.vWorldPos);
1314 if (dot(nGeom, v) < 0.0) { nGeom = -nGeom; }
1315 var base = textureSample(albedoSampler, mainSamp, in.vUV) * in.vTint;
1316 if (ubo.virtualTexture.x > 0.5) {
1317 base = sampleClusteredVirtualTexture(albedoSampler, in.vUV, uvDx, uvDy) * in.vTint;
1319 if (ubo.surface.x > 0.5 && ubo.surface.x < 1.5 && base.a < ubo.surface.y) {
1322 // Screen-space primary; UV secondary so coverage still varies if fragment
1323 // position is degenerate (Dawn Metal CI once kept every pixel with fragCoord-only).
1324 let alphaHash = fract(dot(floor(in.fragCoord.xy), vec2f(0.06711056, 0.00583715)) +
1326 dot(in.vUV * 64.0, vec2f(0.7548777, 0.5698403)));
1327 if (ubo.surface.x > 2.5 && base.a < alphaHash) { discard; }
1328 var albedo = base.rgb;
1329 var metallic = clamp(ubo.ambient.w, 0.0, 1.0);
1330 var rough = clamp(ubo.cameraPos.w, 0.04, 1.0);
1331 var packedNormal = textureSample(normalSampler, mainSamp, in.vUV);
1332 if (ubo.virtualTexture.x > 0.5) {
1333 packedNormal = sampleClusteredVirtualTexture(normalSampler, in.vUV, uvDx, uvDy);
1335 var nSmp = packedNormal.xyz;
1336 var terrainAo = 1.0;
1337 if (ubo.virtualAtlas.z > 0.5) {
1338 let normalXY = packedNormal.rg * 2.0 - 1.0;
1339 nSmp = vec3f(packedNormal.rg, sqrt(1.0 - clamp(dot(normalXY, normalXY), 0.0, 1.0)) * 0.5 + 0.5);
1340 terrainAo = clamp(packedNormal.b, 0.0, 1.0);
1341 rough = clamp(1.0 - packedNormal.a, 0.04, 1.0);
1344 if (length(nSmp - vec3f(0.5, 0.5, 1.0)) > 0.04) {
1345 n = applyClusteredNormalMap(n, nSmp, dpdx(in.vWorldPos), dpdy(in.vWorldPos), uvDx, uvDy);
1347 var emissive = vec3f(0.0);
1348 let decalPos = clamp(vec2<i32>(in.fragCoord.xy), vec2<i32>(0),
1349 vec2<i32>(textureDimensions(decalAlbedoLayer)) - vec2<i32>(1));
1350 let decalA = textureLoad(decalAlbedoLayer, decalPos, 0);
1351 let decalCoverage = clamp(decalA.a, 0.0, 1.0);
1352 if (decalCoverage > 0.001) {
1353 albedo = mix(albedo, decalA.rgb, decalCoverage);
1354 let decalN = textureLoad(decalNormalLayer, decalPos, 0);
1355 let normalWeight = clamp(decalN.a, 0.0, 1.0);
1356 if (normalWeight > 0.001) {
1357 n = normalize(mix(n, normalize(decalN.rgb * 2.0 - 1.0), normalWeight));
1359 let decalP = textureLoad(decalParamsLayer, decalPos, 0);
1360 let paramsWeight = clamp(decalP.a, 0.0, 1.0);
1361 if (paramsWeight > 0.001) {
1362 rough = mix(rough, decalP.r, paramsWeight);
1363 metallic = mix(metallic, decalP.g, paramsWeight);
1364 emissive += decalA.rgb * decalP.b;
1367 var lo = vec3f(0.0);
1368 let viewDepth = max(-in.vViewPos.z, 0.0);
1369 let primaryL = normalize(ubo.lightDir.xyz);
1370 let shadowVis = sampleShadowPCF(in.vWorldPos, n, viewDepth, max(dot(n, primaryL), 0.0));
1371 if (ubo.lightDir.w > 0.5 && length(ubo.lightColor.rgb) > 1e-6) {
1372 lo += shadeLight(n, v, albedo, metallic, rough, primaryL, ubo.lightColor.rgb) * shadowVis;
1374 let cid = clusterIndex(in.fragCoord.xy, viewDepth);
1375 let entry = clusterTable[cid];
1376 let count = min(entry.y, 32u);
1377 for (var i = 0u; i < count; i = i + 1u) {
1378 let li = lightIndices[entry.x + i];
1379 let lgt = lights[li];
1380 let toL = lgt.posRadius.xyz - in.vWorldPos;
1381 let dist = length(toL);
1382 let l = toL / max(dist, 1e-4);
1383 var atten = clamp(1.0 - dist / max(lgt.posRadius.w, 1e-3), 0.0, 1.0);
1384 atten = atten * atten;
1385 if (lgt.spot.w > 0.0) {
1386 let beamLen = length(lgt.spot.xyz);
1387 if (beamLen > 1e-6) {
1388 let cosTheta = dot(normalize(-l), lgt.spot.xyz / beamLen);
1389 atten = atten * clamp(cosTheta * lgt.spot.w + lgt.color.a, 0.0, 1.0);
1390 if (beamLen > 1.005 && shadow.bias.z > 0.5) {
1391 let si = i32(round((beamLen - 1.0) * 100.0)) - 1;
1392 if (si >= 0 && si <= 3) {
1393 let lc = shadow.localVP[si] * vec4f(in.vWorldPos, 1.0);
1394 let ndc = lc.xyz / max(lc.w, 1e-4);
1395 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
1396 let zref = ndc.z - shadow.localBias[si];
1398 if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && lc.w > 0.0) {
1399 vis = textureSampleCompareLevel(shadowMap, shadowSamp, uv, 3 + si, zref);
1401 atten = atten * mix(1.0 - shadow.splits.w, 1.0, vis);
1408 lo += shadeLight(n, v, albedo, metallic, rough, l, lgt.color.rgb * atten);
1410 let hemi = clamp(n.y * 0.5 + 0.5, 0.0, 1.0);
1411 let skyIrr = ubo.ambient.rgb * 1.1 + ubo.lightColor.rgb * 0.12;
1412 let gndIrr = ubo.ambient.rgb * vec3f(0.72, 0.62, 0.52);
1413 let irr = mix(gndIrr, skyIrr, hemi);
1414 var color = albedo * irr * (1.0 - metallic) * terrainAo + lo + emissive;
1415 let envIntensity = ubo.lightColor.w;
1416 if (envIntensity > 1e-4) {
1417 let r = reflect(-v, n);
1418 let envSpec = textureSampleLevel(envSampler, mainSamp, r, rough * 5.0).rgb * envIntensity;
1419 let f0 = mix(vec3f(0.04), albedo, metallic);
1420 let f = fresnelSchlick(max(dot(n, v), 0.0), f0);
1421 color += envSpec * f;
1422 let irr2 = textureSampleLevel(envSampler, mainSamp, n, 5.0).rgb * envIntensity;
1423 color += albedo * irr2 * (1.0 - metallic) * (1.0 - f) * 0.45 * terrainAo;
1425 let aoUV = (in.fragCoord.xy * 0.5) / vec2f(textureDimensions(aoTex));
1426 let ao = textureSampleLevel(aoTex, aoSamp, aoUV, 0.0).r;
1427 color *= mix(1.0, ao, clamp(ubo.surface.z, 0.0, 1.0));
1429 let over = max(color - vec3f(white), vec3f(0.0));
1430 color = min(color, vec3f(white)) + vec3f(1.0 - white) * (over / (over + vec3f(1.0)));
1431 let nearZ = max(ubo.clipInfo.x, 1e-4);
1432 let farZ = max(ubo.clipInfo.y, nearZ + 1e-3);
1433 let viewZ = max(-in.vViewPos.z, 0.0);
1434 let linearDepth = clamp((viewZ - nearZ) / (farZ - nearZ), 0.0, 1.0);
1436 return vec4f(color, linearDepth);
1443@group(0) @binding(3) var<storage, read> skinBones: array<mat4x4f>;
1447 @location(0) pos: vec3f,
1448 @location(1) normal: vec3f,
1449 @location(2) uv: vec2f,
1450 @location(3) joints: vec4u,
1451 @location(4) weights: vec4f,
1460@group(0) @binding(0) var<uniform> pc: Push;
1463fn vs_main(in: VSIn) -> @builtin(position) vec4f {
1464 var localPos = vec4f(in.pos, 1.0);
1465 if (pc.skinInfo.x > 0.5) {
1466 var weights = in.weights;
1467 if (pc.skinInfo.y < 3.5) { weights.z = 0.0; weights.w = 0.0; }
1468 if (pc.skinInfo.y < 1.5) { weights.y = 0.0; }
1469 let weightSum = dot(weights, vec4f(1.0));
1470 if (weightSum > 1e-8) { weights /= weightSum; }
1471 let skin = weights.x * skinBones[in.joints.x]
1472 + weights.y * skinBones[in.joints.y]
1473 + weights.z * skinBones[in.joints.z]
1474 + weights.w * skinBones[in.joints.w];
1475 localPos = skin * localPos;
1477 let clipPos = pc.mvp * localPos;
1478 // WebGPU NDC is Y-up; mirror the Vulkan-convention clip Y.
1480 return vec4f(clipPos.x, -clipPos.y, clipPos.z, clipPos.w);
1486@group(0) @binding(3) var<storage, read> skinBones: array<mat4x4f>;
1490 @location(0) pos: vec3f,
1491 @location(1) normal: vec3f,
1492 @location(2) uv: vec2f,
1493 @location(3) joints: vec4u,
1494 @location(4) weights: vec4f,
1505 @builtin(position) pos: vec4f,
1506 @location(0) uv: vec2f,
1508@group(0) @binding(0) var<uniform> pc: Push;
1511fn vs_main(in: VSIn) -> VSOut {
1513 var localPos = vec4f(in.pos, 1.0);
1514 if (pc.skinInfo.x > 0.5) {
1515 var weights = in.weights;
1516 if (pc.skinInfo.y < 3.5) { weights.z = 0.0; weights.w = 0.0; }
1517 if (pc.skinInfo.y < 1.5) { weights.y = 0.0; }
1518 let weightSum = dot(weights, vec4f(1.0));
1519 if (weightSum > 1e-8) { weights /= weightSum; }
1520 let skin = weights.x * skinBones[in.joints.x]
1521 + weights.y * skinBones[in.joints.y]
1522 + weights.z * skinBones[in.joints.z]
1523 + weights.w * skinBones[in.joints.w];
1524 localPos = skin * localPos;
1526 let clipPos = pc.mvp * localPos;
1527 out.pos = vec4f(clipPos.x, -clipPos.y, clipPos.z, clipPos.w);
1541@group(0) @binding(0) var<uniform> pc: Push;
1542@group(0) @binding(1) var albedoTexture: texture_2d<f32>;
1543@group(0) @binding(2) var albedoSampler: sampler;
1546fn fs_main(@location(0) uv: vec2f, @builtin(position) fragCoord: vec4f) {
1547 if (textureSample(albedoTexture, albedoSampler, uv).a < 0.05) { discard; }
1548 if (pc.lodFade.z > 0.5) {
1549 let h = fract(dot(floor(fragCoord.xy), vec2f(0.06711056, 0.00583715)));
1550 let rejected = select((h < 1.0 - pc.lodFade.x), (h >= pc.lodFade.x),
1551 pc.lodFade.y < 0.5);
1552 if (rejected) { discard; }
1560@group(0) @binding(3) var<storage, read> skinBones: array<mat4x4f>;
1564 @location(0) pos: vec3f,
1565 @location(1) normal: vec3f,
1566 @location(2) uv: vec2f,
1567 @location(3) joints: vec4u,
1568 @location(4) weights: vec4f,
1579 @builtin(position) pos: vec4f,
1580 @location(0) vNormal: vec3f,
1581 @location(1) vNdcZ: f32,
1582 @location(2) vUV: vec2f,
1584@group(0) @binding(0) var<uniform> pc: Push;
1586fn inverseGbufferModel(m: mat3x3f) -> mat3x3f {
1587 let a = m[0].x; let b = m[1].x; let c = m[2].x;
1588 let d = m[0].y; let e = m[1].y; let f = m[2].y;
1589 let g = m[0].z; let h = m[1].z; let i = m[2].z;
1590 let det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
1594 vec3f((e * i - f * h) / det, (f * g - d * i) / det, (d * h - e * g) / det),
1596 vec3f((c * h - b * i) / det, (a * i - c * g) / det, (b * g - a * h) / det),
1598 vec3f((b * f - c * e) / det, (c * d - a * f) / det, (a * e - b * d) / det));
1602fn vs_main(in: VSIn) -> VSOut {
1604 var localPos = vec4f(in.pos, 1.0);
1605 var localNormal = in.normal;
1606 if (pc.skinInfo.x > 0.5) {
1607 var weights = in.weights;
1608 if (pc.skinInfo.y < 3.5) { weights.z = 0.0; weights.w = 0.0; }
1609 if (pc.skinInfo.y < 1.5) { weights.y = 0.0; }
1610 let weightSum = dot(weights, vec4f(1.0));
1611 if (weightSum > 1e-8) { weights /= weightSum; }
1612 let skin = weights.x * skinBones[in.joints.x]
1613 + weights.y * skinBones[in.joints.y]
1614 + weights.z * skinBones[in.joints.z]
1615 + weights.w * skinBones[in.joints.w];
1616 localPos = skin * localPos;
1617 localNormal = mat3x3f(skin[0].xyz, skin[1].xyz, skin[2].xyz) * localNormal;
1619 out.pos = pc.mvp * localPos;
1620 // WebGPU NDC is Y-up; mirror the Vulkan-convention clip Y.
1621 out.pos.y = -out.pos.y;
1622 let model3 = mat3x3f(pc.model[0].xyz, pc.model[1].xyz, pc.model[2].xyz);
1623 out.vNormal = normalize(transpose(inverseGbufferModel(model3)) * localNormal);
1624 out.vNdcZ = out.pos.z / max(out.pos.w, 1e-6);
1634 @builtin(position) position: vec4f,
1635 @location(0) vNormal: vec3f,
1636 @location(1) vNdcZ: f32,
1637 @location(2) vUV: vec2f,
1648 @location(0) normal: vec4f,
1649 @location(1) depthColor: vec4f,
1650 @location(2) albedo: vec4f,
1651 @location(3) pbrParams: vec4f,
1652 @location(4) emissive: vec4f,
1654@group(0) @binding(0) var<uniform> pc: Push;
1655@group(0) @binding(1) var albedoSampler: texture_2d<f32>;
1656@group(0) @binding(2) var mainSamp: sampler;
1659fn fs_main(in: FSIn) -> GBufOut {
1661 let nearZ = max(pc.clip.x, 1e-4);
1662 let farZ = max(pc.clip.y, nearZ + 1e-3);
1663 // NDC z -> linear [0,1] over the clip range (matches scene color A).
1664 let ndc = clamp(in.position.z, 0.0, 1.0);
1665 let eyeZ = nearZ * farZ / max(farZ - ndc * (farZ - nearZ), 1e-6);
1666 let linear = clamp((eyeZ - nearZ) / (farZ - nearZ), 0.0, 1.0);
1667 let packedMotion = u32(pc.clip.w + 0.5);
1668 let motion = (vec2f(f32(packedMotion & 4095u), f32((packedMotion >> 12u) & 4095u)) - 2047.0) / 2047.0;
1669 let packedTint = bitcast<u32>(pc.clip.z);
1670 // Phase B packing: tint RGB6 | rough7 | metal7 (matches Vulkan drawMeshGBuffer).
1671 let roughness = f32((packedTint >> 18u) & 127u) / 127.0;
1672 let metallic = f32((packedTint >> 25u) & 127u) / 127.0;
1673 let pbrLegacy = (u32(roughness * 7.0 + 0.5) & 7u) | ((u32(metallic * 7.0 + 0.5) & 7u) << 3u);
1674 out.normal = vec4f(in.vNormal * 0.5 + 0.5, f32(pbrLegacy) / 255.0);
1675 out.depthColor = vec4f(linear, clamp(motion * 0.5 + 0.5, vec2f(0.0), vec2f(1.0)), 1.0);
1676 let tint = vec3f(f32(packedTint & 63u), f32((packedTint >> 6u) & 63u),
1678 f32((packedTint >> 12u) & 63u)) / 63.0;
1679 out.albedo = vec4f(textureSample(albedoSampler, mainSamp, in.vUV).rgb * tint, linear);
1680 out.pbrParams = vec4f(metallic, roughness, 1.0, 1.0);
1681 out.emissive = vec4f(0.0);
1690 @builtin(position) position: vec4f,
1691 @location(0) vNormal: vec3f,
1692 @location(1) vNdcZ: f32,
1693 @location(2) vUV: vec2f,
1704 @location(0) normal: vec4f,
1705 @location(1) depthColor: vec4f,
1706 @location(2) albedo: vec4f,
1707 @location(3) pbrParams: vec4f,
1708 @location(4) emissive: vec4f,
1710@group(0) @binding(0) var<uniform> pc: Push;
1711@group(0) @binding(1) var albedoTexture: texture_2d<f32>;
1712@group(0) @binding(2) var albedoSampler: sampler;
1715fn fs_main(in: FSIn) -> GBufOut {
1716 let sampled = textureSample(albedoTexture, albedoSampler, in.vUV);
1717 if (sampled.a < 0.05) { discard; }
1719 let nearZ = max(pc.clip.x, 1e-4);
1720 let farZ = max(pc.clip.y, nearZ + 1e-3);
1721 let ndc = clamp(in.position.z, 0.0, 1.0);
1722 let eyeZ = nearZ * farZ / max(farZ - ndc * (farZ - nearZ), 1e-6);
1723 let linear = clamp((eyeZ - nearZ) / (farZ - nearZ), 0.0, 1.0);
1724 let packedMotion = u32(pc.clip.w + 0.5);
1725 let motion = (vec2f(f32(packedMotion & 4095u), f32((packedMotion >> 12u) & 4095u)) - 2047.0) / 2047.0;
1726 let packedTint = bitcast<u32>(pc.clip.z);
1727 let roughness = f32((packedTint >> 18u) & 127u) / 127.0;
1728 let metallic = f32((packedTint >> 25u) & 127u) / 127.0;
1729 let pbrLegacy = (u32(roughness * 7.0 + 0.5) & 7u) | ((u32(metallic * 7.0 + 0.5) & 7u) << 3u);
1730 out.normal = vec4f(in.vNormal * 0.5 + 0.5, f32(pbrLegacy) / 255.0);
1731 out.depthColor = vec4f(linear, clamp(motion * 0.5 + 0.5, vec2f(0.0), vec2f(1.0)), 1.0);
1732 let tint = vec3f(f32(packedTint & 63u), f32((packedTint >> 6u) & 63u),
1734 f32((packedTint >> 12u) & 63u)) / 63.0;
1735 out.albedo = vec4f(sampled.rgb * tint, linear);
1736 out.pbrParams = vec4f(metallic, roughness, 1.0, 1.0);
1737 out.emissive = vec4f(0.0);
1746fn vs_main(@builtin(vertex_index) vertexIndex: u32) -> @builtin(position) vec4f {
1747 let positions = array<vec2f, 3>(
1749 vec2f(-1.0, -1.0), vec2f(3.0, -1.0), vec2f(-1.0, 3.0));
1751 return vec4f(positions[vertexIndex], 0.0, 1.0);
1757struct DecalUniforms {
1758 invViewProj: mat4x4f,
1766 surfaceParams: vec4f,
1771 @location(0) albedo: vec4f,
1772 @location(1) normal: vec4f,
1773 @location(2) params: vec4f,
1775@group(0) @binding(0) var<uniform> u: DecalUniforms;
1776@group(0) @binding(1) var decalAlbedo: texture_2d<f32>;
1777@group(0) @binding(2) var decalNormal: texture_2d<f32>;
1778@group(0) @binding(3) var decalParams: texture_2d<f32>;
1779@group(0) @binding(4) var hwDepth: texture_depth_2d;
1780@group(0) @binding(5) var gbNormal: texture_2d<f32>;
1781@group(0) @binding(6) var mainSamp: sampler;
1784fn sampleAtlas(tex: texture_2d<f32>, localUV: vec2f) -> vec4f {
1785 let atl = u.uvRect.xy + clamp(localUV, vec2f(0.0), vec2f(1.0)) * u.uvRect.zw;
1787 return textureSample(tex, mainSamp, atl);
1790fn projectionUV(localUV: vec2f, wrapMode: i32) -> vec2f {
1791 if (wrapMode == 2) { return fract(localUV); }
1792 if (wrapMode == 1) { return vec2f(fract(localUV.x), clamp(localUV.y, 0.0, 1.0)); }
1793 return clamp(localUV, vec2f(0.0), vec2f(1.0));
1796fn sampleHeight(localUV: vec2f, wrapMode: i32) -> f32 {
1797 let atl = u.uvRect.xy + projectionUV(localUV, wrapMode) * u.uvRect.zw;
1798 return textureSampleLevel(decalParams, mainSamp, atl, 0.0).a;
1801fn parallaxUV(baseUV: vec2f, viewTS: vec3f, wrapMode: i32) -> vec2f {
1802 let scale = u.surfaceParams.x;
1803 if (scale <= 0.0) { return baseUV; }
1804 let minLayers = clamp(u.surfaceParams.y, 1.0, 64.0);
1805 let maxLayers = clamp(u.surfaceParams.z, minLayers, 64.0);
1806 let layers = mix(maxLayers, minLayers, clamp(abs(viewTS.z), 0.0, 1.0));
1807 let layerDepth = 1.0 / layers;
1808 let delta = (viewTS.xy / max(abs(viewTS.z), 0.08)) * scale / layers;
1809 var currentUV = baseUV;
1810 var currentDepth = 0.0;
1811 for (var index = 0; index < 64; index = index + 1) {
1812 let surfaceDepth = 1.0 - sampleHeight(currentUV, wrapMode);
1813 if (currentDepth >= surfaceDepth || f32(index) >= layers) { break; }
1814 currentUV = currentUV - delta;
1815 currentDepth = currentDepth + layerDepth;
1820fn worldNormalFromLocalBasis(packed: vec4f, tangentLocal: vec3f, bitangentLocal: vec3f,
1821 normalLocal: vec3f) -> vec3f {
1822 let model3 = mat3x3f(u.modelR0.xyz, u.modelR1.xyz, u.modelR2.xyz);
1823 let normalWorld = normalize(model3 * normalLocal);
1824 var tangentWorld = normalize(model3 * tangentLocal);
1825 tangentWorld = normalize(tangentWorld - normalWorld * dot(normalWorld, tangentWorld));
1826 var bitangentWorld = normalize(model3 * bitangentLocal);
1827 if (dot(cross(tangentWorld, bitangentWorld), normalWorld) < 0.0) {
1828 bitangentWorld = -bitangentWorld;
1830 let tangentNormal = packed.xyz * 2.0 - 1.0;
1831 return normalize(tangentWorld * tangentNormal.x + bitangentWorld * tangentNormal.y +
1832 normalWorld * tangentNormal.z);
1835fn worldNormalFromWorldBasis(packed: vec4f, tangentWorld: vec3f, bitangentWorld: vec3f,
1836 normalWorld: vec3f) -> vec3f {
1837 let tangentNormal = packed.xyz * 2.0 - 1.0;
1838 return normalize(tangentWorld * tangentNormal.x + bitangentWorld * tangentNormal.y +
1839 normalWorld * tangentNormal.z);
1842fn edgeMask2(coordinates: vec2f) -> f32 {
1843 let width = clamp(u.surfaceParams.w, 0.0, 0.49);
1844 if (width <= 0.0) { return 1.0; }
1845 let edge = smoothstep(vec2f(0.0), vec2f(width), coordinates) *
1846 smoothstep(vec2f(1.0), vec2f(1.0 - width), coordinates);
1847 return edge.x * edge.y;
1850fn edgeMask3(coordinates: vec3f) -> f32 {
1851 let width = clamp(u.surfaceParams.w, 0.0, 0.49);
1852 if (width <= 0.0) { return 1.0; }
1853 let edge = smoothstep(vec3f(0.0), vec3f(width), coordinates) *
1854 smoothstep(vec3f(1.0), vec3f(1.0 - width), coordinates);
1855 return edge.x * edge.y * edge.z;
1860fn fs_main(@builtin(position) pos: vec4f) -> DecalOut {
1861 let uv = pos.xy * u.texel.xy;
1862 let texelPos = vec2<i32>(pos.xy);
1863 let ndcZ = textureLoad(hwDepth, texelPos, 0);
1864 if (ndcZ <= 0.0 || ndcZ >= 1.0) { discard; }
1866 let clip = vec4f(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, ndcZ, 1.0);
1867 let wp = u.invViewProj * clip;
1868 let worldPos = wp.xyz / max(abs(wp.w), 1e-6);
1869 let lp = u.invModel * vec4f(worldPos, 1.0);
1870 let local = lp.xyz / max(abs(lp.w), 1e-6);
1871 if (any(abs(local) > vec3f(0.5))) { discard; }
1873 let surfaceN = textureLoad(gbNormal, texelPos, 0).xyz * 2.0 - 1.0;
1874 let decalFwd = normalize(mat3x3f(u.modelR0.xyz, u.modelR1.xyz, u.modelR2.xyz) *
1876 vec3f(0.0, 0.0, 1.0));
1877 let projectionMode = i32(u.extraParams.z + 0.5);
1878 let useTriplanar = projectionMode == 1;
1879 let useSpherical = projectionMode == 2;
1880 let useWorld = projectionMode == 3;
1881 let facing = dot(surfaceN, decalFwd);
1882 if (useSpherical || useTriplanar || useWorld) {
1883 if (facing < -0.05) { discard; }
1884 } else if (facing < 0.1) {
1892 let nearClip = vec4f(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, 0.0, 1.0);
1893 let nearWorld = u.invViewProj * nearClip;
1894 let nearPos = nearWorld.xyz / max(abs(nearWorld.w), 1e-6);
1895 let invR = mat3x3f(u.invModel[0].xyz, u.invModel[1].xyz, u.invModel[2].xyz);
1896 let viewWorld = normalize(nearPos - worldPos);
1897 let viewLocal = normalize(invR * viewWorld);
1899 if (!useTriplanar && !useSpherical && !useWorld) {
1900 let decalUV = parallaxUV(local.xy + 0.5, viewLocal, 0);
1901 if (any(decalUV < vec2f(0.0)) || any(decalUV > vec2f(1.0))) { discard; }
1902 alb = sampleAtlas(decalAlbedo, decalUV);
1903 let sampledNormal = sampleAtlas(decalNormal, decalUV);
1904 let normalWorld = worldNormalFromLocalBasis(sampledNormal, vec3f(1.0, 0.0, 0.0),
1905 vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, 1.0));
1906 nrm = vec4f(normalWorld * 0.5 + 0.5, sampledNormal.a);
1907 prm = sampleAtlas(decalParams, decalUV);
1908 edgeFade = edgeMask2(decalUV);
1909 } else if (useTriplanar) {
1910 let nLocal = normalize(invR * surfaceN);
1911 let sharpness = max(u.extraParams.w, 1.0);
1912 var w = pow(abs(nLocal), vec3f(sharpness));
1913 w = w / max(w.x + w.y + w.z, 1e-5);
1915 let uvYZ = parallaxUV(local.yz + 0.5, vec3f(viewLocal.yz, viewLocal.x), 0);
1916 let uvXZ = parallaxUV(local.xz + 0.5, vec3f(viewLocal.xz, viewLocal.y), 0);
1917 let uvXY = parallaxUV(local.xy + 0.5, viewLocal, 0);
1918 alb = sampleAtlas(decalAlbedo, uvYZ) * w.x +
1920 sampleAtlas(decalAlbedo, uvXZ) * w.y +
1922 sampleAtlas(decalAlbedo, uvXY) * w.z;
1923 let nrmX = sampleAtlas(decalNormal, uvYZ);
1924 let nrmY = sampleAtlas(decalNormal, uvXZ);
1925 let nrmZ = sampleAtlas(decalNormal, uvXY);
1926 let sx = select(1.0, -1.0, nLocal.x < 0.0);
1927 let sy = select(1.0, -1.0, nLocal.y < 0.0);
1928 let sz = select(1.0, -1.0, nLocal.z < 0.0);
1930 worldNormalFromLocalBasis(nrmX, vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, sx), vec3f(sx, 0.0, 0.0)) * w.x +
1931 worldNormalFromLocalBasis(nrmY, vec3f(1.0, 0.0, 0.0), vec3f(0.0, 0.0, -sy), vec3f(0.0, sy, 0.0)) * w.y +
1932 worldNormalFromLocalBasis(nrmZ, vec3f(1.0, 0.0, 0.0), vec3f(0.0, sz, 0.0), vec3f(0.0, 0.0, sz)) * w.z;
1933 nrm = vec4f(normalize(blendedNormal) * 0.5 + 0.5,
1934 nrmX.a * w.x + nrmY.a * w.y + nrmZ.a * w.z);
1935 prm = sampleAtlas(decalParams, uvYZ) * w.x +
1937 sampleAtlas(decalParams, uvXZ) * w.y +
1939 sampleAtlas(decalParams, uvXY) * w.z;
1941 let t = local + 0.5;
1942 edgeFade = edgeMask3(t);
1943 } else if (useWorld) {
1944 let sharpness = max(u.extraParams.w, 1.0);
1945 var w = pow(abs(normalize(surfaceN)), vec3f(sharpness));
1946 w = w / max(w.x + w.y + w.z, 1e-5);
1947 let worldScale = 1.0;
1948 let uvYZ = fract(parallaxUV(fract(worldPos.yz * worldScale), vec3f(viewWorld.yz, viewWorld.x), 2));
1949 let uvXZ = fract(parallaxUV(fract(worldPos.xz * worldScale), vec3f(viewWorld.xz, viewWorld.y), 2));
1950 let uvXY = fract(parallaxUV(fract(worldPos.xy * worldScale), viewWorld, 2));
1951 alb = sampleAtlas(decalAlbedo, uvYZ) * w.x +
1952 sampleAtlas(decalAlbedo, uvXZ) * w.y +
1953 sampleAtlas(decalAlbedo, uvXY) * w.z;
1954 let nrmX = sampleAtlas(decalNormal, uvYZ);
1955 let nrmY = sampleAtlas(decalNormal, uvXZ);
1956 let nrmZ = sampleAtlas(decalNormal, uvXY);
1957 let sx = select(1.0, -1.0, surfaceN.x < 0.0);
1958 let sy = select(1.0, -1.0, surfaceN.y < 0.0);
1959 let sz = select(1.0, -1.0, surfaceN.z < 0.0);
1961 worldNormalFromWorldBasis(nrmX, vec3f(0.0, 1.0, 0.0), vec3f(0.0, 0.0, sx), vec3f(sx, 0.0, 0.0)) * w.x +
1962 worldNormalFromWorldBasis(nrmY, vec3f(1.0, 0.0, 0.0), vec3f(0.0, 0.0, -sy), vec3f(0.0, sy, 0.0)) * w.y +
1963 worldNormalFromWorldBasis(nrmZ, vec3f(1.0, 0.0, 0.0), vec3f(0.0, sz, 0.0), vec3f(0.0, 0.0, sz)) * w.z;
1964 nrm = vec4f(normalize(blendedNormal) * 0.5 + 0.5,
1965 nrmX.a * w.x + nrmY.a * w.y + nrmZ.a * w.z);
1966 prm = sampleAtlas(decalParams, uvYZ) * w.x +
1967 sampleAtlas(decalParams, uvXZ) * w.y +
1968 sampleAtlas(decalParams, uvXY) * w.z;
1969 let t = local + 0.5;
1970 edgeFade = edgeMask3(t);
1972 let direction = normalize(local + vec3f(1e-7));
1973 let invPi = 0.31830988618;
1974 var decalUV = vec2f(atan2(direction.x, direction.z) * (0.5 * invPi) + 0.5,
1975 asin(clamp(direction.y, -1.0, 1.0)) * invPi + 0.5);
1976 let tangent = normalize(vec3f(direction.z, 0.0, -direction.x) + vec3f(1e-7));
1977 let bitangent = normalize(cross(direction, tangent));
1978 let sphericalView = vec3f(dot(viewLocal, tangent), dot(viewLocal, bitangent),
1979 dot(viewLocal, direction));
1980 decalUV = parallaxUV(decalUV, sphericalView, 1);
1981 decalUV.x = fract(decalUV.x);
1982 if (decalUV.y < 0.0 || decalUV.y > 1.0) { discard; }
1983 alb = sampleAtlas(decalAlbedo, decalUV);
1984 let sampledNormal = sampleAtlas(decalNormal, decalUV);
1985 let normalWorld = worldNormalFromLocalBasis(sampledNormal, tangent, bitangent, direction);
1986 nrm = vec4f(normalWorld * 0.5 + 0.5, sampledNormal.a);
1987 prm = sampleAtlas(decalParams, decalUV);
1988 let t = local + 0.5;
1989 edgeFade = edgeMask3(t);
1992 var coverage = alb.a * clamp(u.fadeParams.x, 0.0, 1.0) * edgeFade;
1993 if (coverage <= 0.001) { discard; }
1996 out.albedo = vec4f(alb.rgb, coverage);
1997 out.normal = vec4f(nrm.rgb * step(0.001, u.fadeParams.y),
1999 coverage * clamp(u.fadeParams.y, 0.0, 1.0));
2000 out.params = vec4f(prm.r * clamp(u.fadeParams.z, 0.0, 1.0),
2001 prm.g * clamp(u.fadeParams.w, 0.0, 1.0),
2002 prm.b * clamp(u.extraParams.x, 0.0, 1.0), coverage);
2011 @builtin(position) pos: vec4f,
2012 @location(1) uv: vec2f,
2016 params: vec4f, // x = radius (UV offset scale), y = power, z = nearZ (hw), w = farZ (hw)
2017 intensity: f32, // 0 = off
2018 invScale: f32, // AO target size / depth size (0.5 = half-res AO)
2021@group(0) @binding(0) var<uniform> ubo: AOUniforms;
2022@group(0) @binding(1) var depthTex: texture_depth_2d;
2023@group(0) @binding(2) var samp: sampler;
2026fn hash12(p: vec2f) -> f32 {
2028 return fract(sin(dot(p, vec2f(127.1, 311.7))) * 43758.5453);
2032fn linearizeDepth(z: f32) -> f32 {
2033 let nearZ = ubo.params.z;
2034 let farZ = ubo.params.w;
2035 return nearZ * farZ / max(farZ - z * (farZ - nearZ), 1e-6);
2040fn fs_main(in: VSOut) -> @location(0) vec4f {
2041 let dims = vec2f(textureDimensions(depthTex));
2042 // AO runs at half resolution; the upsampled bilinear fetch in the mesh
2043 // pass acts as a cheap 2x2 blur, which removes the per-pixel grain.
2044 let uv = (in.pos.xy + vec2f(0.5)) / (dims * ubo.invScale);
2045 // Hardware depth (Depth32Float, NDC z in [0,1], 0 = near, 1 = far) —
2046 // linearized to world units so the occlusion delta is not crushed by
2047 // perspective (hw depth deltas near the far plane are ~1e-4).
2048 let rawZ = textureLoad(depthTex, vec2<i32>(uv * dims), 0);
2049 let centerDepth = linearizeDepth(rawZ);
2050 let nearZ = ubo.params.z;
2051 let farZ = ubo.params.w;
2052 if (centerDepth >= farZ * 0.99 || centerDepth <= nearZ * 1.02) {
2054 return vec4f(1.0, 1.0, 1.0, 1.0);
2056 // Screen radius scales with the compressed depth: near surfaces span more
2057 // pixels for the same world-space radius.
2058 let rad = ubo.params.x / (rawZ + 0.05);
2060 for (var i = 0; i < 12; i = i + 1) {
2061 let f = (f32(i) + 0.5) / 12.0;
2062 let ang = 6.2831853 * f + hash12(uv * 311.7 + vec2f(f32(i), 0.0)) * 0.9;
2063 let r = rad * (0.25 + 0.75 * f);
2064 let sampleUV = clamp(uv + vec2f(cos(ang), sin(ang)) * r, vec2f(0.001), vec2f(0.999));
2065 let sampleDepth = linearizeDepth(textureLoad(depthTex, vec2<i32>(sampleUV * dims), 0));
2066 if (sampleDepth >= farZ * 0.99) { continue; }
2067 // The sample is occluded when it is closer than the center surface
2068 // (positive diff in world units); smoothstep softens the hard cutoff
2069 // that turned tiny depth deltas into per-pixel speckle.
2070 let diff = centerDepth - sampleDepth;
2071 let delta = diff / max(centerDepth, 1e-4);
2072 occ += smoothstep(0.0, 0.4, delta * 35.0);
2074 let ao = pow(clamp(1.0 - occ / 12.0, 0.0, 1.0), ubo.params.y);
2076 return vec4f(vec3f(mix(1.0, ao, ubo.intensity)), 1.0);
2084 @location(0) corner: vec2f,
2085 @location(1) packed: u32,
2086 @location(2) aoWord: u32,
2097 @builtin(position) pos: vec4f,
2098 @location(0) uv: vec2f,
2099 @location(1) tint: vec4f,
2100 @location(2) vAO: f32,
2101 @location(3) @interpolate(flat) atlasBase: vec2f,
2102 @location(4) @interpolate(flat) tileScale: f32,
2104@group(0) @binding(0) var<uniform> pc: PC;
2107fn vs_main(in: VSIn) -> VSOut {
2109 // packed: x(5) y(5) z(5) w(5) h(5) tex(7)
2110 let px = f32(in.packed & 31u);
2111 let py = f32((in.packed >> 5u) & 31u);
2112 let pz = f32((in.packed >> 10u) & 31u);
2113 let w = f32((in.packed >> 15u) & 31u) + 1.0;
2114 let h = f32((in.packed >> 20u) & 31u) + 1.0;
2115 let tex = f32((in.packed >> 25u) & 127u);
2116 // Per-instance AO word: 2 bits per corner (0..3), Vulkan corner order.
2117 let ao0 = in.aoWord & 3u;
2118 let ao1 = (in.aoWord >> 2u) & 3u;
2119 let ao2 = (in.aoWord >> 4u) & 3u;
2120 let ao3 = (in.aoWord >> 6u) & 3u;
2121 let aoTop = select(ao1, ao0, in.corner.x < 0.5);
2122 let aoBot = select(ao2, ao3, in.corner.x < 0.5);
2123 let ao = select(aoBot, aoTop, in.corner.y < 0.5);
2124 var world: vec3f = pc.chunkOrigin.xyz + vec3f(px, py, pz);
2125 let face = i32(pc.chunkOrigin.w + 0.5);
2126 var u: f32 = in.corner.x;
2127 var v: f32 = in.corner.y;
2128 if (face == 0) { world += vec3f(1.0, v * h, u * w); }
2129 else if (face == 1) { world += vec3f(0.0, v * h, (1.0 - u) * w); }
2130 else if (face == 2) { world += vec3f(u * w, 1.0, v * h); }
2131 else if (face == 3) { world += vec3f(u * w, 0.0, (1.0 - v) * h); }
2132 else if (face == 4) { world += vec3f((1.0 - u) * w, v * h, 1.0); }
2133 else { world += vec3f(u * w, v * h, 0.0); }
2134 out.pos = pc.viewProj * vec4f(world, 1.0);
2135 // WebGPU NDC is Y-up; mirror the Vulkan-convention clip Y.
2136 out.pos.y = -out.pos.y;
2137 let tiles = max(pc.atlasInfo.x, 1.0);
2138 let tw = 1.0 / tiles;
2139 let col = tex - floor(tex / tiles) * tiles;
2140 let row = floor(tex / tiles);
2141 out.uv = in.corner * vec2f(w, h);
2142 out.atlasBase = vec2f(col, row);
2145 out.vAO = f32(ao) / 3.0;
2153 @location(0) uv: vec2f,
2154 @location(1) tint: vec4f,
2155 @location(2) vAO: f32,
2156 @location(3) @interpolate(flat) atlasBase: vec2f,
2157 @location(4) @interpolate(flat) tileScale: f32,
2159@group(0) @binding(1) var atlas: texture_2d<f32>;
2160@group(0) @binding(2) var atlasSamp: sampler;
2163fn fs_main(in: FSIn) -> @location(0) vec4f {
2164 // Vertex ambient occlusion (0..1): darkens corners near other voxels.
2165 let aoShade = 0.35 + 0.65 * in.vAO;
2166 let atlasUV = (in.atlasBase + fract(in.uv)) * in.tileScale;
2167 let dx = dpdx(in.uv) * in.tileScale;
2168 let dy = dpdy(in.uv) * in.tileScale;
2170 return textureSampleGrad(atlas, atlasSamp, atlasUV, dx, dy) * in.tint * aoShade;
2179 @builtin(position) pos: vec4f,
2183fn vs_main(@builtin(vertex_index) vid: u32) -> VSOut {
2184 // Fullscreen triangle in WebGPU NDC (Y-up).
2185 var p = vec2f(f32((vid << 1u) & 2u), f32(vid & 2u));
2187 out.pos = vec4f(p * 2.0 - 1.0, 0.0, 1.0);
2205struct DeferredFrame {
2206 invViewProj: mat4x4f,
2217 lightVP: array<mat4x4f, 3>,
2221 cascadeTexel: vec4f,
2222 cascadeUvScale: vec4f,
2223 localVP: array<mat4x4f, 4>,
2231 @builtin(position) fragCoord: vec4f,
2235 @location(0) color: vec4f,
2236 @builtin(frag_depth) depth: f32,
2239@group(0) @binding(0) var<uniform> ubo: DeferredFrame;
2240@group(0) @binding(1) var gbNormal: texture_2d<f32>;
2241@group(0) @binding(2) var gbDepthColor: texture_2d<f32>;
2242@group(0) @binding(3) var gbAlbedo: texture_2d<f32>;
2243@group(0) @binding(4) var gbPbrParams: texture_2d<f32>;
2244@group(0) @binding(5) var gbEmissive: texture_2d<f32>;
2245@group(0) @binding(6) var gbHwDepth: texture_depth_2d;
2246@group(0) @binding(7) var<storage, read> lights: array<Light3D>;
2247@group(0) @binding(8) var<storage, read> clusters: array<vec2u>;
2248@group(0) @binding(9) var<storage, read> lightIndices: array<u32>;
2249@group(0) @binding(10) var<uniform> shadow: ShadowFrame;
2250@group(0) @binding(11) var shadowMap: texture_depth_2d_array;
2251@group(0) @binding(12) var nearestSamp: sampler;
2252@group(0) @binding(13) var shadowSamp: sampler_comparison;
2254const PI: f32 = 3.14159265359;
2257fn distGGX(n: vec3f, h: vec3f, rough: f32) -> f32 {
2258 let a = max(rough * rough, 0.002);
2260 let ndh = max(dot(n, h), 0.0);
2261 let denom = (ndh * ndh * (a2 - 1.0) + 1.0);
2262 return a2 / max(PI * denom * denom, 1e-4);
2265fn geomSchlick(ndv: f32, rough: f32) -> f32 {
2266 let r = rough + 1.0;
2267 let k = (r * r) / 8.0;
2268 return ndv / max(ndv * (1.0 - k) + k, 1e-4);
2271fn geomSmith(n: vec3f, v: vec3f, l: vec3f, rough: f32) -> f32 {
2273 return geomSchlick(max(dot(n, v), 0.0), rough) * geomSchlick(max(dot(n, l), 0.0), rough);
2276fn fresnelSchlick(cosT: f32, f0: vec3f) -> vec3f {
2277 return f0 + (1.0 - f0) * pow(clamp(1.0 - cosT, 0.0, 1.0), 5.0);
2280fn shadeLight(n: vec3f, v: vec3f, l: vec3f, rad: vec3f, albedo: vec3f, metallic: f32,
2281 rough: f32, specularFactor: f32) -> vec3f {
2282 let ndl = max(dot(n, l), 0.0);
2283 let diffuse = mix(ndl, ndl * 0.5 + 0.5, 0.25);
2284 let h = normalize(v + l);
2285 let f0 = mix(vec3f(0.04 * specularFactor), albedo, metallic);
2286 let ndf = distGGX(n, h, rough);
2287 let g = geomSmith(n, v, l, rough);
2288 let f = fresnelSchlick(max(dot(h, v), 0.0), f0);
2289 let spec = (ndf * g * f) / max(4.0 * max(dot(n, v), 0.0) * max(ndl, 0.001), 1e-3);
2290 let kd = (vec3f(1.0) - f) * (1.0 - metallic);
2291 return (kd * albedo * diffuse + spec * ndl) * rad;
2294fn sampleShadowCascade(worldPos: vec3f, cascade: i32, biasAmt: f32) -> f32 {
2295 let lightClip = shadow.lightVP[cascade] * vec4f(worldPos, 1.0);
2296 let ndc = lightClip.xyz / max(lightClip.w, 1e-6);
2297 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
2299 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0 || depth < 0.0 || depth > 1.0) {
2302 var uvScale = shadow.cascadeUvScale.z;
2303 if (cascade == 0) { uvScale = shadow.cascadeUvScale.x; }
2304 else if (cascade == 1) { uvScale = shadow.cascadeUvScale.y; }
2305 uvScale = max(uvScale, 1e-4);
2307 return textureSampleCompareLevel(shadowMap, shadowSamp, uv * uvScale, cascade, depth - biasAmt);
2310fn sampleShadowPCF(worldPos: vec3f, n: vec3f, viewDepth: f32, nDotL: f32) -> f32 {
2311 if (shadow.bias.y < 0.5 || shadow.bias.z < 0.5 || shadow.splits.w < 1e-4) {
2315 if (viewDepth < shadow.splits.x) { cascade = 0; }
2316 else if (viewDepth < shadow.splits.y) { cascade = 1; }
2317 var b = select(shadow.cascadeBias.z,
2319 select(shadow.cascadeBias.y, shadow.cascadeBias.x, cascade == 0),
2321 if (b < 1e-8) { b = shadow.bias.x; }
2322 b = b * mix(0.75, 1.0, clamp(nDotL, 0.0, 1.0));
2323 let tw = select(shadow.cascadeTexel.z,
2325 select(shadow.cascadeTexel.y, shadow.cascadeTexel.x, cascade == 0),
2327 let p = worldPos + n * ((2.0 * max(tw, 1e-6)) / max(nDotL, 0.2));
2328 let vis = sampleShadowCascade(p, cascade, b);
2330 return mix(0.04, 1.0, mix(1.0, vis, clamp(shadow.splits.w, 0.0, 1.0)));
2333fn clusterIndex(viewDepth: f32, fragCoord: vec4f) -> u32 {
2334 let tilesX = i32(ubo.gridInfo.x + 0.5);
2335 let tilesY = i32(ubo.gridInfo.y + 0.5);
2336 let slices = i32(ubo.gridInfo.z + 0.5);
2337 let nearZ = ubo.clipInfo.x;
2338 let farZ = max(ubo.clipInfo.y, nearZ + 1e-3);
2339 let screenW = max(ubo.clipInfo.z, 1.0);
2340 let screenH = max(ubo.clipInfo.w, 1.0);
2341 let tx = clamp(i32(floor(fragCoord.x / screenW * f32(tilesX))), 0, tilesX - 1);
2342 let ty = clamp(i32(floor(fragCoord.y / screenH * f32(tilesY))), 0, tilesY - 1);
2343 let depth = max(viewDepth, nearZ);
2344 let sz = clamp(i32(floor((depth - nearZ) / (farZ - nearZ) * f32(slices))), 0, slices - 1);
2346 return u32((sz * tilesY + ty) * tilesX + tx);
2351fn fs_main(in: FSIn) -> FSOut {
2353 let screenW = max(ubo.clipInfo.z, 1.0);
2354 let screenH = max(ubo.clipInfo.w, 1.0);
2355 let uv = in.fragCoord.xy / vec2f(screenW, screenH);
2356 let dims = vec2i(textureDimensions(gbHwDepth));
2357 let px = clamp(vec2i(in.fragCoord.xy), vec2i(0), dims - vec2i(1));
2358 let hwZ = textureLoad(gbHwDepth, px, 0);
2359 if (hwZ >= 0.9999) {
2362 // WebGPU fragCoord origin is top-left (Y down); NDC is Y-up. GBuffer fill
2363 // also mirrors clip Y, so reconstruct with the same WebGPU NDC.
2364 let clip = vec4f(uv.x * 2.0 - 1.0, 1.0 - uv.y * 2.0, hwZ, 1.0);
2365 let worldH = ubo.invViewProj * clip;
2366 let worldPos = worldH.xyz / max(worldH.w, 1e-6);
2368 var n = normalize(textureSample(gbNormal, nearestSamp, uv).xyz * 2.0 - 1.0);
2369 let albedo = textureSample(gbAlbedo, nearestSamp, uv).rgb;
2370 let pbr = textureSample(gbPbrParams, nearestSamp, uv);
2371 let metallic = clamp(pbr.r, 0.0, 1.0);
2372 let roughness = clamp(pbr.g, 0.04, 1.0);
2373 let occlusion = clamp(pbr.b, 0.0, 1.0);
2374 let specularFactor = clamp(pbr.a, 0.0, 1.0);
2375 let emissive = textureSample(gbEmissive, nearestSamp, uv).rgb;
2377 var v = normalize(ubo.cameraPos.xyz - worldPos);
2378 if (dot(n, v) < 0.0) { n = -n; }
2380 let viewPos = (ubo.view * vec4f(worldPos, 1.0)).xyz;
2381 let viewDepth = max(-viewPos.z, 0.0);
2383 var lo = vec3f(0.0);
2384 if (ubo.lightDir.w > 0.5) {
2385 let l = normalize(ubo.lightDir.xyz);
2386 let nDotL = max(dot(n, l), 0.0);
2387 let shadowVis = sampleShadowPCF(worldPos, n, viewDepth, nDotL);
2388 lo = lo + shadeLight(n, v, l, ubo.lightColor.rgb, albedo, metallic, roughness, specularFactor) * shadowVis;
2391 let ci = clusterIndex(viewDepth, in.fragCoord);
2392 let entry = clusters[ci];
2393 let count = min(entry.y, 32u);
2394 for (var i = 0u; i < count; i = i + 1u) {
2395 let li = lightIndices[entry.x + i];
2396 let light = lights[li];
2397 let toLight = light.posRadius.xyz - worldPos;
2398 let dist = length(toLight);
2399 let radius = max(light.posRadius.w, 1e-3);
2400 if (dist >= radius) { continue; }
2401 let l = toLight / max(dist, 1e-4);
2402 var atten = clamp(1.0 - dist / radius, 0.0, 1.0);
2403 atten = atten * atten;
2404 if (light.spot.w > 0.0) {
2405 let beamLen = length(light.spot.xyz);
2406 if (beamLen > 1e-6) {
2407 let cosTheta = dot(normalize(-l), light.spot.xyz / beamLen);
2408 atten = atten * clamp(cosTheta * light.spot.w + light.color.a, 0.0, 1.0);
2409 if (beamLen > 1.005) {
2410 let si = i32(round((beamLen - 1.0) * 100.0)) - 1;
2411 if (si >= 0 && si <= 3) {
2412 let lc = shadow.localVP[si] * vec4f(worldPos, 1.0);
2413 let ndc = lc.xyz / max(lc.w, 1e-4);
2414 let uv = vec2f(ndc.x, -ndc.y) * 0.5 + 0.5;
2415 let zref = ndc.z - shadow.localBias[si];
2417 if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0 && lc.w > 0.0) {
2418 vis = textureSampleCompareLevel(shadowMap, shadowSamp, uv, 3 + si, zref);
2420 atten = atten * mix(1.0 - shadow.splits.w, 1.0, vis);
2427 lo = lo + shadeLight(n, v, l, light.color.rgb * atten, albedo, metallic, roughness, specularFactor);
2430 let hemi = clamp(n.y * 0.5 + 0.5, 0.0, 1.0);
2431 let skyIrr = ubo.ambient.rgb * 1.1 + ubo.lightColor.rgb * 0.12;
2432 let gndIrr = ubo.ambient.rgb * vec3f(0.72, 0.62, 0.52);
2433 let irr = mix(gndIrr, skyIrr, hemi);
2434 var ambient = albedo * irr * (1.0 - metallic) * occlusion;
2435 let wrap = max(dot(n, normalize(ubo.lightDir.xyz)) * 0.5 + 0.5, 0.0);
2436 ambient = ambient + albedo * ubo.lightColor.rgb * (wrap * wrap) * 0.06 * (1.0 - metallic) * occlusion;
2437 out.color = vec4f(ambient + lo + emissive, 1.0);
const char * kDecalFragWgsl
const char * kColorVertWgsl
const char * kMesh3DFragWgsl
const char * kDecalVertWgsl
const char * kColorFragWgsl
const char * kTexturedVertWgsl
const char * kDeferredLightingVertWgsl
Immutable deferred-lighting vertex WGSL. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DGbufferFragWgsl
Immutable GBuffer fill WGSL. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DShadowAlphaVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kLit2DVertWgsl
const char * kMesh3DShadowVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kSSAOFragWgsl
const char * kTexturedFragWgsl
const char * kLit2DFragWgsl
const char * kVoxelRectVertWgsl
const char * kMesh3DShadowAlphaFragWgsl
const char * kVoxelRectFragWgsl
const char * kMesh3DClusteredVertWgsl
const char * kMesh3DGbufferAlphaFragWgsl
Immutable GBuffer alpha-cutout WGSL. @borrowed Static storage; valid for the process lifetime.
const char * kMesh3DClusteredFragWgsl
const char * kMesh3DGbufferVertWgsl
Immutable shader source. @borrowed Static storage; valid for the process lifetime.
const char * kDeferredLightingFragWgsl
Immutable clustered deferred lighting fragment WGSL (core metallic-roughness PBR + CSM + clustered po...
const char * kCustom2DFragWgsl