#define DoF //#define LensFlares //#define FakeGodRays //need to ajust, can not working
#define DoF_Vignetting //#define DoF_Manual #define DoF_Auto //#define DoF_PentagonShape
//Bokeh
float penta(float2 coords) //pentagonal shape { float scale = float(rings) - 1.5; float4 HS0 = float4( -20.0, -15.0, -15.0, -20.0); float4 HS1 = float4( -20.0, -15.0, -15.0, -20.0); float4 HS2 = float4( -20.0, -15.0, -15.0, -20.0); float4 HS3 = float4( -20.0, -15.0, -15.0, -20.0); float4 HS4 = float4( -20.0, -15.0, -15.0, -20.0); float4 HS5 = float4( -20.0, -15.0, -15.0, -20.0); float4 one = float4(8.0, 8.0, 8.0, 8.0); float4 P = float4(coords,float2(scale, scale)); float4 dist = float4(4.0, 4.0, 4.0, 4.0); float inorout = 0.0; dist.x = dot( P, HS0 ); dist.y = dot( P, HS1 ); dist.z = dot( P, HS2 ); dist.w = dot( P, HS3 ); dist = smoothstep( -feather, feather, dist ); inorout += dot( dist, one ); dist.x = dot( P, HS4 ); dist.y = HS5.w - abs( P.z ); dist = smoothstep( -feather, feather, dist ); inorout += dist.x; return saturate( inorout ); }
float linearize(float depth) { return -zfar * znear / (depth * (zfar - znear) - zfar); }
float2 rand(float2 coord) //generating noise/pattern texture for dithering { float noiseX = ((frac(3.0-coord.x*(screenRes.x/0.2))*3.25)+(frac(coord.y*(screenRes.y/0.2))*3.75))*0.1-0.2; float noiseY = ((frac(3.0-coord.x*(screenRes.x/0.2))*3.75)+(frac(coord.y*(screenRes.y/0.2))*3.25))*0.1-0.2; return float2(noiseX,noiseY); }
float4 colorDof(float2 coords,float blur) //processing the sample { float4 colDF = float4(1,1,1,1); colDF.x = tex2D(InputSampler,coords + float2(0.0,1.0)*texel*fringe*blur).x; colDF.y = tex2D(InputSampler,coords + float2(-0.866,-0.5)*texel*fringe*blur).y; colDF.z = tex2D(InputSampler,coords + float2(0.866,-0.5)*texel*fringe*blur).z; float3 lumcoeff = float3(0.299,0.587,0.114); float lum = dot(colDF.xyz,lumcoeff); float thresh = max((lum-threshold)*gain, 0.0); float3 nullcol = float3(0,0,0); colDF.xyz +=lerp(nullcol,colDF.xyz,thresh*blur); return colDF; }
float4 PS_ProcessDoFBokeh(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR { float depth = linearize(tex2D(SamplerDepth,IN.txcoord.xy).x); float fDepth = focalDepth; #ifdef DoF_Auto fDepth = linearize(tex2D(SamplerDepth,focus).x); #endif float blur = 2.0; #ifdef DoF_Manual float a = depth-fDepth; //focal plane float b = (a-fdofstart)/fdofdist; //far DoF float c = (-a-ndofstart)/ndofdist; //near Dof blur = (a>0.0)?b:c; #else float f = focalLength; //focal length in mm float d = fDepth*1000.0; //focal plane in mm float o = depth*1000.0; //depth in mm float a = (o*f)/(o-f); float b = (d*f)/(d-f); float c = (d-f)/(d*fstop*CoC); blur = abs(a-b)*c; #endif blur = saturate(blur); float2 noise = rand(IN.txcoord.xy)*namount*blur; float w = (1.0/screenRes.x)*blur*maxblur+noise.x; float h = (1.0/screenRes.y)*blur*maxblur+noise.y; float4 col = float4(0,0,0,1); if(blur < 0.05) //some optimization thingy { col = tex2D(InputSampler, IN.txcoord.xy); } else { col = tex2D(InputSampler, IN.txcoord.xy); float s = 1.0; int ringsamples; for (int i = 1; i <= rings; i += 1) { ringsamples = i * samples; for (int j = 0; j < ringsamples; j += 1) { float step = PI*2.0 / ringsamples; float pw = cos(j*step)*i; float ph = sin(j*step)*i; float p = 1.0; #ifdef DoF_PentagonShape p = penta(float2(pw,ph)); #endif col.xyz += colorDof(IN.txcoord.xy + float2(pw*w,ph*h),blur).xyz; s += 1.0*lerp(1.0,i/rings,bias)*p; } } col = col/s; //divide by sample count } #ifdef DoF_Vignetting col *= vignette(IN.txcoord.xy,vignint); #endif return col; }
//-------------------------------------------------------------------------------------- // Compiler 1 //--------------------------------------------------------------------------------------
technique PostProcess { #ifdef DoF pass P0 { VertexShader = compile vs_3_0 VS_PostProcess(); PixelShader = compile ps_3_0 PS_ProcessDoFBokeh(); } #endif #ifdef LensFlares pass P1 { VertexShader = compile vs_3_0 VS_PostProcess(); PixelShader = compile ps_3_0 PS_ProcessLensFlares(); AlphaBlendEnable=True; SrcBlend = One; DestBlend = One; } #endif #ifdef FakeGodRays pass P2 { VertexShader = compile vs_3_0 VS_PostProcess(); PixelShader = compile ps_3_0 PS_ProcessSunShafts(); AlphaBlendEnable=True; SrcBlend = One; DestBlend = One; } #endif }
by Boris vorontsov Edited by MM |