UNPKG

playcanvas

Version:

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

77 lines (59 loc) 2.27 kB
var dofBlur_default = ( /* glsl */ ` #if defined(NEAR_BLUR) uniform sampler2D nearTexture; #endif uniform sampler2D farTexture; uniform sampler2D cocTexture; uniform vec2 kernel[{KERNEL_COUNT}]; uniform float blurRadiusNear; uniform float blurRadiusFar; varying vec2 uv0; void main() { vec2 coc = texture2D(cocTexture, uv0).rg; float cocFar = coc.r; vec3 sum = vec3(0.0, 0.0, 0.0); #if defined(NEAR_BLUR) // near blur float cocNear = coc.g; if (cocNear > 0.0001) { // blur radius as a fraction of frame height, aspect-corrected so the kernel stays // circular in screen pixels - this makes the effect resolution-independent vec2 nearTextureSize = vec2(textureSize(nearTexture, 0)); vec2 step = cocNear * blurRadiusNear * vec2(nearTextureSize.y / nearTextureSize.x, 1.0); for (int i = 0; i < {KERNEL_COUNT}; i++) { vec2 uv = uv0 + step * kernel[i]; vec3 tap = texture2DLod(nearTexture, uv, 0.0).rgb; sum += tap.rgb; } sum *= float({INV_KERNEL_COUNT}); } else #endif if (cocFar > 0.0001) { // far blur vec2 farTextureSize = vec2(textureSize(farTexture, 0)); vec2 step = cocFar * blurRadiusFar * vec2(farTextureSize.y / farTextureSize.x, 1.0); float sumCoC = 0.0; for (int i = 0; i < {KERNEL_COUNT}; i++) { vec2 uv = uv0 + step * kernel[i]; vec3 tap = texture2DLod(farTexture, uv, 0.0).rgb; // block out sharp objects to avoid leaking to far blur float cocThis = texture2DLod(cocTexture, uv, 0.0).r; tap *= cocThis; sumCoC += cocThis; sum += tap; } // average out the sum if (sumCoC > 0.0) sum /= sumCoC; // compensate for the fact the farTexture was premultiplied by CoC sum /= cocFar; } pcFragColor0 = vec4(sum, 1.0); } ` ); export { dofBlur_default as default };