UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

3 lines (2 loc) 1.87 kB
declare const _default: "\n #include \"screenDepthPS\"\n\n varying uv0: vec2f;\n\n var uFogTexture: texture_2d<f32>;\n var uFogTextureSampler: sampler;\n uniform uFogTextureSize: vec4f; // xy: fog texture resolution, zw: inverse resolution\n\n @fragment\n fn fragmentMain(input: FragmentInput) -> FragmentOutput {\n var output: FragmentOutput;\n\n let depth: f32 = getLinearScreenDepth(input.uv0);\n\n // 4 nearest texel centers of the low resolution fog texture\n let texel: vec2f = input.uv0 * uniform.uFogTextureSize.xy - 0.5;\n let base: vec2f = (floor(texel) + 0.5) * uniform.uFogTextureSize.zw;\n let f: vec2f = fract(texel);\n\n var uvs: array<vec2f, 4>;\n uvs[0] = base;\n uvs[1] = base + vec2f(uniform.uFogTextureSize.z, 0.0);\n uvs[2] = base + vec2f(0.0, uniform.uFogTextureSize.w);\n uvs[3] = base + uniform.uFogTextureSize.zw;\n\n let bilinear: vec4f = vec4f((1.0 - f.x) * (1.0 - f.y), f.x * (1.0 - f.y), (1.0 - f.x) * f.y, f.x * f.y);\n\n // depth-aware upsample - weight the low resolution fog samples by depth similarity to\n // avoid fog leaking across geometry edges\n var sum: vec4f = vec4f(0.0);\n var sumWeight: f32 = 0.0;\n for (var i: i32 = 0; i < 4; i += 1) {\n let sampleDepth: f32 = getLinearScreenDepth(uvs[i]);\n let w: f32 = bilinear[i] / (1.0 + 16.0 * abs(sampleDepth - depth) / max(depth, 0.001));\n sum += textureSampleLevel(uFogTexture, uFogTextureSampler, uvs[i], 0.0) * w;\n sumWeight += w;\n }\n\n // rgb: in-scattered light, a: transmittance. Blended over the scene using\n // scene * transmittance + inscatter.\n output.color = sum / max(sumWeight, 0.0001);\n return output;\n }\n"; export default _default;