UNPKG

playcanvas

Version:

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

3 lines (2 loc) 4.36 kB
declare const _default: "\n #include \"sampleCatmullRomPS\"\n #include \"screenDepthPS\"\n\n var sourceTexture: texture_2d<f32>;\n var sourceTextureSampler: sampler;\n var historyTexture: texture_2d<f32>;\n var historyTextureSampler: sampler;\n uniform matrix_viewProjectionPrevious: mat4x4f;\n uniform matrix_viewProjectionInverse: mat4x4f;\n uniform jitters: vec4f; // xy: current frame, zw: previous frame\n uniform textureSize: vec2f;\n\n varying uv0: vec2f;\n\n fn reproject(uv_in: vec2f, depth: f32) -> vec2f {\n\n // uv was Y-flipped by getImageEffectUV for texture sampling,\n // un-flip to reconstruct correct NDC (viewProj matrices use standard Y convention)\n var uv = vec2f(uv_in.x, 1.0 - uv_in.y);\n\n var ndc = vec4f(uv * 2.0 - 1.0, depth, 1.0);\n\n // remove jitter from the current frame\n ndc = vec4f(ndc.xy - uniform.jitters.xy, ndc.zw);\n\n // Transform NDC to world space of the current frame\n var worldPosition = uniform.matrix_viewProjectionInverse * ndc;\n worldPosition = worldPosition / worldPosition.w;\n\n // world position to screen space of the previous frame\n let screenPrevious = uniform.matrix_viewProjectionPrevious * worldPosition;\n\n // flip result back to texture sampling convention\n var result = (screenPrevious.xy / screenPrevious.w) * 0.5 + 0.5;\n result.y = 1.0 - result.y;\n\n return result;\n }\n\n fn colorClampPremul(uv: vec2f, historyPremul: vec3f) -> vec3f {\n\n var minPremul = vec3f(9999.0);\n var maxPremul = vec3f(-9999.0);\n\n // 3x3 neighborhood in premultiplied space \u2014 same domain as the temporal mix (see fragmentMain).\n for (var ix: i32 = -1; ix <= 1; ix = ix + 1) {\n for (var iy: i32 = -1; iy <= 1; iy = iy + 1) {\n let s = textureSample(sourceTexture, sourceTextureSampler, uv + vec2f(f32(ix), f32(iy)) / uniform.textureSize);\n let premul = s.rgb * s.a;\n minPremul = min(minPremul, premul);\n maxPremul = max(maxPremul, premul);\n }\n }\n\n return clamp(historyPremul, minPremul, maxPremul);\n }\n\n @fragment\n fn fragmentMain(input: FragmentInput) -> FragmentOutput {\n var output: FragmentOutput;\n\n // current frame\n let srcColor = textureSample(sourceTexture, sourceTextureSampler, uv0);\n\n // current depth is in linear space, convert it to non-linear space\n let linearDepth = getLinearScreenDepth(uv0);\n let depth = delinearizeDepth(linearDepth);\n\n // previous frame\n let historyUv = reproject(uv0, depth);\n\n #ifdef QUALITY_HIGH\n\n var historySample: vec4f = SampleTextureCatmullRom(historyTexture, historyTextureSampler, historyUv, uniform.textureSize);\n\n #else\n\n var historySample: vec4f = textureSample(historyTexture, historyTextureSampler, historyUv);\n\n #endif\n\n // Premultiplied (rgb * a) is the coverage-correct space for TAA: straight RGB interpolates\n // uncorrelated color and opacity at edges, which causes colored fringes and alpha-related\n // ghosting when history is blended or clamped against a 3x3 neighborhood. We clamp and mix\n // in premultiplied space, then un-premultiply once using the current frame's alpha only\n // (alpha is not temporally filtered \u2014 output stays tied to this frame's coverage).\n let historyPremul = historySample.rgb * historySample.a;\n let srcPremul = srcColor.rgb * srcColor.a;\n\n let historyPremulClamped = colorClampPremul(uv0, historyPremul);\n\n let mixFactor_condition = historyUv.x < 0.0 || historyUv.x > 1.0 || historyUv.y < 0.0 || historyUv.y > 1.0;\n let mixFactor = select(0.05, 1.0, mixFactor_condition);\n\n let mixedPremul = mix(historyPremulClamped, srcPremul, mixFactor);\n let a = srcColor.a;\n // UNORM8 alpha step \u2014 avoid un-premul in the lowest band (quantization / noise).\n let UNPREMUL_EPS = 1.0 / 255.0;\n let rgbStraight = select(srcColor.rgb, mixedPremul / a, a > UNPREMUL_EPS);\n output.color = vec4f(rgbStraight, a);\n return output;\n }\n"; export default _default;