@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 4.25 kB
JavaScript
import{FILTER_LINEAR as t,ADDRESS_CLAMP_TO_EDGE as e}from"../../platform/graphics/constants.js";import{Texture as r}from"../../platform/graphics/texture.js";import{shaderChunks as o}from"../../scene/shader-lib/chunks/chunks.js";import{RenderPassShaderQuad as s}from"../../scene/graphics/render-pass-shader-quad.js";import{RenderTarget as i}from"../../platform/graphics/render-target.js";class n extends s{constructor(t,e,r){super(t),this.historyIndex=0,this.historyTexture=null,this.historyTextures=[],this.historyRenderTargets=[],this.sourceTexture=e,this.cameraComponent=r;const s=o.sampleCatmullRomPS;this.shader=this.createQuadShader("TaaResolveShader","\n\t\t\t#define QUALITY_HIGH\n\t\t"+s+"\n\tuniform highp sampler2D uSceneDepthMap;\n\tuniform sampler2D sourceTexture;\n\tuniform sampler2D historyTexture;\n\tuniform mat4 matrix_viewProjectionPrevious;\n\tuniform mat4 matrix_viewProjectionInverse;\n\tuniform vec4 jitters;\n\tuniform vec2 textureSize;\n\tvarying vec2 uv0;\n\tvec2 reproject(vec2 uv, float depth) {\n\t\t#ifndef WEBGPU\n\t\t\tdepth = depth * 2.0 - 1.0;\n\t\t#endif\n\t\tvec4 ndc = vec4(uv * 2.0 - 1.0, depth, 1.0);\n\t\tndc.xy -= jitters.xy;\n\t\tvec4 worldPosition = matrix_viewProjectionInverse * ndc;\n\t\tworldPosition /= worldPosition.w;\n\t\n\t\tvec4 screenPrevious = matrix_viewProjectionPrevious * worldPosition;\n\t\treturn (screenPrevious.xy / screenPrevious.w) * 0.5 + 0.5;\n\t}\n\tvec4 colorClamp(vec2 uv, vec4 historyColor) {\n\t\tvec3 minColor = vec3(9999.0);\n\t\tvec3 maxColor = vec3(-9999.0);\n \n\t\tfor(float x = -1.0; x <= 1.0; ++x)\n\t\t{\n\t\t\tfor(float y = -1.0; y <= 1.0; ++y)\n\t\t\t{\n\t\t\t\tvec3 color = texture2D(sourceTexture, uv + vec2(x, y) / textureSize).rgb;\n\t\t\t\tminColor = min(minColor, color);\n\t\t\t\tmaxColor = max(maxColor, color);\n\t\t\t}\n\t\t}\n \n\t\tvec3 clamped = clamp(historyColor.rgb, minColor, maxColor);\n\t\treturn vec4(clamped, historyColor.a);\n\t}\n\tvoid main()\n\t{\n\t\tvec2 uv = uv0;\n\t\t#ifdef WEBGPU\n\t\t\tuv.y = 1.0 - uv.y;\n\t\t#endif\n\t\tvec4 srcColor = texture2D(sourceTexture, uv);\n\t\tfloat depth = texture2DLodEXT(uSceneDepthMap, uv, 0.0).r;\n\t\tvec2 historyUv = reproject(uv0, depth);\n\t\t#ifdef QUALITY_HIGH\n\t\t\tvec4 historyColor = SampleTextureCatmullRom(TEXTURE_PASS(historyTexture), historyUv, textureSize);\n\t\t#else\n\t\t\tvec4 historyColor = texture2D(historyTexture, historyUv);\n\t\t#endif\n\t\tvec4 historyColorClamped = colorClamp(uv, historyColor);\n\t\tfloat mixFactor = (historyUv.x < 0.0 || historyUv.x > 1.0 || historyUv.y < 0.0 || historyUv.y > 1.0) ?\n\t\t\t1.0 : 0.05;\n\t\tgl_FragColor = mix(historyColorClamped, srcColor, mixFactor);\n\t}\n");const{scope:i}=t;this.sourceTextureId=i.resolve("sourceTexture"),this.textureSizeId=i.resolve("textureSize"),this.textureSize=new Float32Array(2),this.historyTextureId=i.resolve("historyTexture"),this.viewProjPrevId=i.resolve("matrix_viewProjectionPrevious"),this.viewProjInvId=i.resolve("matrix_viewProjectionInverse"),this.jittersId=i.resolve("jitters"),this.setup()}destroy(){this.renderTarget&&(this.renderTarget.destroyTextureBuffers(),this.renderTarget.destroy(),this.renderTarget=null)}setup(){for(let o=0;o<2;++o)this.historyTextures[o]=new r(this.device,{name:`TAA-History-${o}`,width:4,height:4,format:this.sourceTexture.format,mipmaps:!1,minFilter:t,magFilter:t,addressU:e,addressV:e}),this.historyRenderTargets[o]=new i({colorBuffer:this.historyTextures[o],depth:!1});this.historyTexture=this.historyTextures[0],this.init(this.historyRenderTargets[0],{resizeSource:this.sourceTexture})}before(){this.sourceTextureId.setValue(this.sourceTexture),this.historyTextureId.setValue(this.historyTextures[1-this.historyIndex]),this.textureSize[0]=this.sourceTexture.width,this.textureSize[1]=this.sourceTexture.height,this.textureSizeId.setValue(this.textureSize);const t=this.cameraComponent.camera;this.viewProjPrevId.setValue(t._viewProjPrevious.data),this.viewProjInvId.setValue(t._viewProjInverse.data),this.jittersId.setValue(t._jitters)}update(){return this.historyIndex=1-this.historyIndex,this.historyTexture=this.historyTextures[this.historyIndex],this.renderTarget=this.historyRenderTargets[this.historyIndex],this.historyTexture}}export{n as RenderPassTAA};