#define NUM_DOF_TAPS 8 // Values that are tweakable for DOF //float maxCoC; //float focalDist; //float focalRange; half3 depthOfFieldValues; //float2 filterTaps2[12]; half4 ProcessDOF( sampler2D screenTexture, sampler2D depthTexture, half2 texCoord, half4 startColour, half centreDepth) { const half2 filterTaps[8] = { half2(-0.0001h,-0.0001h), half2(-0.0001h, 0h), half2(-0.0001h, 0.0001h), half2( 0h,-0.0001h), half2( 0h, 0.0001h), half2( 0.0001h,-0.0001h), half2( 0.0001h, 0h), half2( 0.0001h, 0.0001h), }; half maxCoC = depthOfFieldValues.x; half focalDist = depthOfFieldValues.y; half focalRange = depthOfFieldValues.z; // Start with center sample color half4 colorSum = startColour; half totalContribution = 1.0h; half oneOverFocalRange = 1.h / focalRange; // Compute CoC size based on blurriness half sizeCoC = saturate(abs(centreDepth - focalDist) * oneOverFocalRange) * maxCoC; // // Run through all filter taps for (int i = 0; i < NUM_DOF_TAPS; i++) { // Compute sample coordinates half2 tapCoord = texCoord + filterTaps[i] * sizeCoC; // Fetch filter tap sample half4 tapColour = tex2D(screenTexture, tapCoord); tapColour /= tapColour.a; const half3 depthFactor = float3(65536.0h/16777215.0h, 256.0h/16777215.0h, 1.0h/16777215.0h ); half4 RawDepth = tex2D(depthTexture, texCoord); half Depth = dot(round( RawDepth.xyz * 255.0h.xxx ), depthFactor.xyz); half ScreenDepth = (Depth + inverseOffset.z) * inverseScale.z; half tapDepth = -inverseParams.w / (ScreenDepth + inverseParams.z); // Compute tap contribution based on depth and blurriness //float tapContribution = (tapDepth.x > centreDepth.x) ? 1.0f : saturate( abs(tapDepth.x - focalDist) * oneOverFocalRange ); half tapSelector = step(centreDepth, tapDepth); half tapContribution = (tapSelector * saturate( abs(tapDepth - focalDist) * oneOverFocalRange )) + (1.h- tapSelector); // Accumulate color and sample contribution colorSum += tapColour * tapContribution; totalContribution += tapContribution; } // Normalize color sum half4 finalColor = colorSum / totalContribution; return finalColor; }