UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

3 lines (2 loc) 3.42 kB
declare const _default: "\n #include \"sampleCatmullRomPS\"\n #include \"screenDepthPS\"\n\n uniform sampler2D sourceTexture;\n uniform sampler2D historyTexture;\n uniform mat4 matrix_viewProjectionPrevious;\n uniform mat4 matrix_viewProjectionInverse;\n uniform vec4 jitters; // xy: current frame, zw: previous frame\n uniform vec2 textureSize;\n\n varying vec2 uv0;\n\n vec2 reproject(vec2 uv, float depth) {\n\n // fragment NDC\n #ifndef WEBGPU\n depth = depth * 2.0 - 1.0;\n #endif\n vec4 ndc = vec4(uv * 2.0 - 1.0, depth, 1.0);\n\n // remove jitter from the current frame\n ndc.xy -= jitters.xy;\n\n // Transform NDC to world space of the current frame\n vec4 worldPosition = matrix_viewProjectionInverse * ndc;\n worldPosition /= worldPosition.w;\n\n // world position to screen space of the previous frame\n vec4 screenPrevious = matrix_viewProjectionPrevious * worldPosition;\n\n return (screenPrevious.xy / screenPrevious.w) * 0.5 + 0.5;\n }\n\n vec4 colorClamp(vec2 uv, vec4 historyColor) {\n\n // out of range numbers\n vec3 minColor = vec3(9999.0);\n vec3 maxColor = vec3(-9999.0);\n\n // sample a 3x3 neighborhood to create a box in color space\n for(float x = -1.0; x <= 1.0; ++x) {\n for(float y = -1.0; y <= 1.0; ++y) {\n vec3 color = texture2D(sourceTexture, uv + vec2(x, y) / textureSize).rgb;\n minColor = min(minColor, color);\n maxColor = max(maxColor, color);\n }\n }\n\n // clamp the history color to min/max bounding box\n vec3 clamped = clamp(historyColor.rgb, minColor, maxColor);\n return vec4(clamped, historyColor.a);\n }\n\n void main()\n {\n vec2 uv = uv0;\n\n #ifdef WEBGPU\n // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n // This hack is needed on webgpu, which makes TAA work but the resulting image is upside-down.\n // We could flip the image in the following pass, but ideally a better solution should be found.\n uv.y = 1.0 - uv.y;\n // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n #endif\n\n // current frame\n vec4 srcColor = texture2D(sourceTexture, uv);\n\n // current depth is in linear space, convert it to non-linear space\n float linearDepth = getLinearScreenDepth(uv0);\n float depth = delinearizeDepth(linearDepth);\n\n // previous frame\n vec2 historyUv = reproject(uv0, depth);\n\n #ifdef QUALITY_HIGH\n\n // high quality history, sharper result\n vec4 historyColor = SampleTextureCatmullRom(TEXTURE_PASS(historyTexture), historyUv, textureSize);\n\n #else\n\n // single sample history, more blurry result\n vec4 historyColor = texture2D(historyTexture, historyUv);\n\n #endif\n\n // handle disocclusion by clamping the history color\n vec4 historyColorClamped = colorClamp(uv, historyColor);\n\n // handle history buffer outside of the frame\n float mixFactor = (historyUv.x < 0.0 || historyUv.x > 1.0 || historyUv.y < 0.0 || historyUv.y > 1.0) ?\n 1.0 : 0.05;\n\n gl_FragColor = mix(historyColorClamped, srcColor, mixFactor);\n }\n"; export default _default;