float2 center;
float depth;
float focus;
r0=tex2D(s1, _v0.xy); //GBufferTextureSampler3
r0.y=-_c77.x + _c77.y; //-dofProj.x + dofProj.y
r0.y=1.0/r0.y;
r0.z=r0.y * _c77.y; //dofProj.y
r0.z=r0.z * -_c77.x; //dofProj.x
r0.x=_c77.y * -r0.y + r0.x;
r0.x=1.0/r0.x;
r0.y=r0.z * r0.x;
r0.w=abs(_c79.w);
r3=_c4; //-0.5, -1.5, 1.5, 0.5
float4 vDofParams = float4(-2, 1, 2000.0, 1.0);
depth=r0.z * r0.x - _c78.w; //dofDist.w
depth+=_c78.y * -r3.w; //dofDist.y
depth+=148;//offset
center.x=_v0.x-0.5;
center.y=0.5-_v0.y;
#ifdef DEPTH_OF_FIELD
r4=tex2D(s1, FocusPoint); //GBufferTextureSampler3
r4.y=-_c77.x + _c77.y; //-dofProj.x + dofProj.y
r4.y=1/r4.y;
r4.z=r4.y * _c77.y; //dofProj.y
r4.z=r4.z * -_c77.x; //dofProj.x
r4.x=_c77.y * -r4.y + r4.x;
r4.x=1/r4.x;
r4.y=r4.z * r4.x;
#ifdef DYNAMIC_DEPTH_OF_FIELD
float cdepth;
cdepth=r4.z * r4.x - _c78.w; //dofDist.w
cdepth+=_c78.y * -r3.w; //dofDist.y
cdepth+=148;//offset
r4.w = tex2D(s0, FocusPoint).w;
if(r4.w > 0)
{
vDofParams.z = cdepth*4.0;
vDofParams.y = cdepth;
}
#endif
depth = max(depth, 0);
float dist;
if(depth < vDofParams.y)
dist = (depth - vDofParams.y)/(vDofParams.y - vDofParams.x);
else
{
dist = (depth - vDofParams.y)/(vDofParams.z - vDofParams.y);
dist = clamp(dist, 0, vDofParams.w);
}
dist = dist * 0.5 + 0.5;
#ifdef DOF_NORMAL_QUALITY
static float3 poisson[24]=
{
float3(1.0, 0.0, 1.0000 ), //0 degrees
float3(0.0, -1.0, 1.0000 ),
float3(-1.0, 0.0, 1.0000 ),
float3(0.0, 1.0, 1.0000 ),
float3(0.7071, 0.7071, 1.0000 ), //45 degrees
float3(0.7071, 0.7071, 1.0000 ),
float3(-0.7071, -0.7071, 1.0000 ),
float3(-0.7071, 0.7071, 1.0000 ),
float3(0.9659, 0.2588, 1.0000 ), //15 degrees
float3(0.2588, -0.9659, 1.0000 ),
float3(-0.9659, -0.2588, 1.0000 ),
float3(-0.2588, 0.9659, 1.0000 ),
float3(0.8660, 0.5000, 1.0000 ), //60 degrees
float3(0.5000, -0.8660, 1.0000 ),
float3(-0.8660, -0.5000, 1.0000 ),
float3(-0.5000, 0.8660, 1.0000 ),
float3(0.5000, 0.8660, 1.0000 ), //30 degrees
float3(0.8660, -0.5000, 1.0000 ),
float3(-0.5000, -0.8660, 1.0000 ),
float3(-0.8660, 0.5000, 1.0000 ),
float3(0.2588, 0.9659, 1.0000 ), //75 degrees
float3(0.9659, -0.2588, 1.0000 ),
float3(-0.2588, -0.9659, 1.0000 ),
float3(-0.9659, 0.2588, 1.0000 ),
};
#endif
#ifdef DOF_HIGH_QUALITY
static float3 poisson[60] =
{
float3( 0.2165, 0.1250, 1.0000 ),
float3( 0.0000, 0.2500, 1.0000 ),
float3( -0.2165, 0.1250, 1.0000 ),
float3( -0.2165, -0.1250, 1.0000 ),
float3( -0.0000, -0.2500, 1.0000 ),
float3( 0.2165, -0.1250, 1.0000 ),
float3( 0.4330, 0.2500, 1.0000 ),
float3( 0.0000, 0.5000, 1.0000 ),
float3( -0.4330, 0.2500, 1.0000 ),
float3( -0.4330, -0.2500, 1.0000 ),
float3( -0.0000, -0.5000, 1.0000 ),
float3( 0.4330, -0.2500, 1.0000 ),
float3( 0.6495, 0.3750, 1.0000 ),
float3( 0.0000, 0.7500, 1.0000 ),
float3( -0.6495, 0.3750, 1.0000 ),
float3( -0.6495, -0.3750, 1.0000 ),
float3( -0.0000, -0.7500, 1.0000 ),
float3( 0.6495, -0.3750, 1.0000 ),
float3( 0.8660, 0.5000, 1.0000 ),
float3( 0.0000, 1.0000, 1.0000 ),
float3( -0.8660, 0.5000, 1.0000 ),
float3( -0.8660, -0.5000, 1.0000 ),
float3( -0.0000, -1.0000, 1.0000 ),
float3( 0.8660, -0.5000, 1.0000 ),
float3( 0.2163, 0.3754, 0.8670 ),
float3( -0.2170, 0.3750, 0.8670 ),
float3( -0.4333, -0.0004, 0.8670 ),
float3( -0.2163, -0.3754, 0.8670 ),
float3( 0.2170, -0.3750, 0.8670 ),
float3( 0.4333, 0.0004, 0.8670 ),
float3( 0.4328, 0.5004, 0.8847 ),
float3( -0.2170, 0.6250, 0.8847 ),
float3( -0.6498, 0.1246, 0.8847 ),
float3( -0.4328, -0.5004, 0.8847 ),
float3( 0.2170, -0.6250, 0.8847 ),
float3( 0.6498, -0.1246, 0.8847 ),
float3( 0.6493, 0.6254, 0.9065 ),
float3( -0.2170, 0.8750, 0.9065 ),
float3( -0.8663, 0.2496, 0.9065 ),
float3( -0.6493, -0.6254, 0.9065 ),
float3( 0.2170, -0.8750, 0.9065 ),
float3( 0.8663, -0.2496, 0.9065 ),
float3( 0.2160, 0.6259, 0.8851 ),
float3( -0.4340, 0.5000, 0.8851 ),
float3( -0.6500, -0.1259, 0.8851 ),
float3( -0.2160, -0.6259, 0.8851 ),
float3( 0.4340, -0.5000, 0.8851 ),
float3( 0.6500, 0.1259, 0.8851 ),
float3( 0.4325, 0.7509, 0.8670 ),
float3( -0.4340, 0.7500, 0.8670 ),
float3( -0.8665, -0.0009, 0.8670 ),
float3( -0.4325, -0.7509, 0.8670 ),
float3( 0.4340, -0.7500, 0.8670 ),
float3( 0.8665, 0.0009, 0.8670 ),
float3( 0.2158, 0.8763, 0.9070 ),
float3( -0.6510, 0.6250, 0.9070 ),
float3( -0.8668, -0.2513, 0.9070 ),
float3( -0.2158, -0.8763, 0.9070 ),
float3( 0.6510, -0.6250, 0.9070 ),
float3( 0.8668, 0.2513, 0.9070 )
};
#endif
float2 pixelSizeHigh;
float2 pixelSizeLow;
pixelSizeHigh.x = 1.000 / _c44.x;
pixelSizeHigh.y = 1.000 / _c44.y;
//pixelSizeHigh.x = 2.0 / _c44.x;
//pixelSizeHigh.y = 2.0 / _c44.y;
pixelSizeHigh.xy *= PixelSizeMultiplier;
pixelSizeLow.xy = pixelSizeHigh.xy;
float2 vMaxCoC = float2(5.0, 10.0);
float radiusScale = BlurIntensity;
float discRadius;
float discRadiusLow;
float centerDepth;
float4 tdepth;
float4 tapHigh;
float4 tapLow;
centerDepth = dist;
discRadius = abs(centerDepth * vMaxCoC.y - vMaxCoC.x);
discRadiusLow = discRadius * radiusScale;
r1 = 0.0;
for(int t = 0; t < DoF_TAPS; t++)
{
float2 coordLow = _v0.xy + (pixelSizeLow.xy * poisson[t].xy * discRadiusLow);
float2 coordHigh = _v0.xy + (pixelSizeHigh.xy * poisson[t].xy * discRadius);
tapLow = tex2D(s2, coordLow.xy) * poisson[t].z;
tapHigh = tex2D(s2, coordHigh.xy) * poisson[t].z;
tdepth = tex2D(s1, coordHigh.xy); //GBufferTextureSampler3
tdepth.y = -_c77.x + _c77.y; //-dofProj.x + dofProj.y
tdepth.y = 1.0 / tdepth.y;
tdepth.z = tdepth.y * _c77.y; //dofProj.y
tdepth.z = tdepth.z * -_c77.x; //dofProj.x
tdepth.x = _c77.y * -tdepth.y + tdepth.x;
tdepth.x = 1.0 / tdepth.x;
tdepth.w = tdepth.z * tdepth.x - _c78.w; //dofDist.w
tdepth.w += _c78.y * -r3.w; //dofDist.y
tdepth.w += 148;//offset
tdepth.w = max(tdepth.w, 0); //fix zoom bug
if(tdepth.w < vDofParams.y)
tapHigh.w = (tdepth.w - vDofParams.y)/(vDofParams.y - vDofParams.x);
else
{
tapHigh.w = (tdepth.w - vDofParams.y)/(vDofParams.z - vDofParams.y);
tapHigh.w = clamp(tapHigh.w, 0, vDofParams.w);
}
tapHigh.w = tapHigh.w * 0.5 + 0.5;
tapLow.w = tapHigh.w;
float tapBlur = abs(tapHigh.w * 2.0 - 1.0);
float4 tap = lerp(tapHigh, tapLow, tapBlur);
if(tap.w >= centerDepth) tap.w = 1.0;
else tap.w = abs(tap.w * 2.0 - 1.0);
r1.xyz += tap.xyz * tap.w;
r1.w += tap.w;
}
r1.xyz /= r1.w;
#endif