Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Shaped Bokeh Depth of Field
- // CoC Calculations based off Knu's DoF shader for MGE
- // Blurring Algorithm Created by Tomerk
- // Optimized by Ethatron
- // Ported to MGE by vtastek
- int mgeflags = 0;
- // ---------------------------------------
- // TWEAKABLE VARIABLES.
- float fogrange;
- float fogstart;
- extern float fr = 59.715; // retina focus point, dpt
- // set slightly lower than fp to simulate myopia or as an alternative to MGE's distant blur
- // set slightly higher than fp+fpa to simulate hyperopia
- extern float fp = 60.0; // eye relaxed focus power, dpt
- extern float fpa = 0.0; // accomodation, dpt
- // set lower to simulate presbyopia
- extern float base_blur_radius = 0.383; // base blur radius;
- // higher values mean more blur when out of DoF and shorter DoF.
- extern float R = 32.0; // maximum blur radius in pixels;
- extern bool EdgeWeighting = 1; // 0 has constant weighting in highlights,
- // 1 gives edges more weight in highlights
- extern bool NoWeaponBlur = 1; // Set to true to keep weapons from blurring
- extern float weaponblur_cutoff = 40.0; // Cutoff distance for what is considered a weapon
- extern float fogblur_modifier = 10.0;
- #undef SMOOTHBLUR // when on, smooths the blur, may have a performance hit
- extern float zzTargetPosX = 0.5;
- extern float zzTargetPosY = 0.5;
- // forcus target position
- // END OF TWEAKABLE VARIABLES.
- // ---------------------------------------
- float2 rcpres;
- texture lastshader;
- texture lastpass;
- texture depthframe;
- sampler PassSamplerL = sampler_state {
- Texture = <lastshader>;
- AddressU = Mirror;
- AddressV = Mirror;
- MagFilter = Linear;
- MinFilter = Linear;
- MipFilter = None;
- };
- sampler s1 = sampler_state { texture=<depthframe>; AddressU=Clamp; AddressV=Clamp; };
- sampler s3 = sampler_state { texture=<lastpass>; AddressU=Mirror; AddressV=Mirror; MagFilter=Linear; MinFilter=Linear; };
- struct VSOUT
- {
- float4 vertPos : POSITION;
- float2 UVCoord : TEXCOORD0;
- };
- struct VSIN
- {
- float4 vertPos : POSITION0;
- float2 UVCoord : TEXCOORD0;
- };
- VSOUT FrameVS(VSIN IN)
- {
- VSOUT OUT = (VSOUT)0.0f;
- OUT.vertPos = IN.vertPos;
- OUT.UVCoord = IN.UVCoord;
- return OUT;
- }
- #define M 60
- static float2 taps[M] =
- {
- float2( 0.0000, 0.2500 ),
- float2( -0.2165, 0.1250 ),
- float2( -0.2165, -0.1250 ),
- float2( -0.0000, -0.2500 ),
- float2( 0.2165, -0.1250 ),
- float2( 0.2165, 0.1250 ),
- float2( 0.0000, 0.5000 ),
- float2( -0.2500, 0.4330 ),
- float2( -0.4330, 0.2500 ),
- float2( -0.5000, 0.0000 ),
- float2( -0.4330, -0.2500 ),
- float2( -0.2500, -0.4330 ),
- float2( -0.0000, -0.5000 ),
- float2( 0.2500, -0.4330 ),
- float2( 0.4330, -0.2500 ),
- float2( 0.5000, -0.0000 ),
- float2( 0.4330, 0.2500 ),
- float2( 0.2500, 0.4330 ),
- float2( 0.0000, 0.7500 ),
- float2( -0.2565, 0.7048 ),
- float2( -0.4821, 0.5745 ),
- float2( -0.6495, 0.3750 ),
- float2( -0.7386, 0.1302 ),
- float2( -0.7386, -0.1302 ),
- float2( -0.6495, -0.3750 ),
- float2( -0.4821, -0.5745 ),
- float2( -0.2565, -0.7048 ),
- float2( -0.0000, -0.7500 ),
- float2( 0.2565, -0.7048 ),
- float2( 0.4821, -0.5745 ),
- float2( 0.6495, -0.3750 ),
- float2( 0.7386, -0.1302 ),
- float2( 0.7386, 0.1302 ),
- float2( 0.6495, 0.3750 ),
- float2( 0.4821, 0.5745 ),
- float2( 0.2565, 0.7048 ),
- float2( 0.0000, 1.0000 ),
- float2( -0.2588, 0.9659 ),
- float2( -0.5000, 0.8660 ),
- float2( -0.7071, 0.7071 ),
- float2( -0.8660, 0.5000 ),
- float2( -0.9659, 0.2588 ),
- float2( -1.0000, 0.0000 ),
- float2( -0.9659, -0.2588 ),
- float2( -0.8660, -0.5000 ),
- float2( -0.7071, -0.7071 ),
- float2( -0.5000, -0.8660 ),
- float2( -0.2588, -0.9659 ),
- float2( -0.0000, -1.0000 ),
- float2( 0.2588, -0.9659 ),
- float2( 0.5000, -0.8660 ),
- float2( 0.7071, -0.7071 ),
- float2( 0.8660, -0.5000 ),
- float2( 0.9659, -0.2588 ),
- float2( 1.0000, -0.0000 ),
- float2( 0.9659, 0.2588 ),
- float2( 0.8660, 0.5000 ),
- float2( 0.7071, 0.7071 ),
- float2( 0.5000, 0.8660 ),
- float2( 0.2588, 0.9659 )
- };
- static float k = 0.00001;
- float4 DepthOfField(float2 tex : TEXCOORD0) : COLOR0 {
- float depth = tex2D(s1,tex).r;
- float focus = tex2D(s1, float2(0.5, 0.5)).r;
- float s = focus * 0.0142;
- float z = depth * 0.0142;
- float weapon = 1;
- float fpf = clamp(1 / s + fr, fp, fp + fpa);
- float c = base_blur_radius * 0.009 * (fr - fpf + 1 / z) / fr / k;
- c = sign(z - s) * min(abs(c), R) / (2 * R);
- float fogblur = smoothstep((fogstart/fogblur_modifier)-(fogblur_modifier*25), fogstart/fogblur_modifier, z);
- c *= fogblur;
- if (NoWeaponBlur) {
- weapon = smoothstep(weaponblur_cutoff-0.05, weaponblur_cutoff, z);
- c *= weapon;
- }
- return float4(tex2D(PassSamplerL, tex).rgb, c + 0.5);
- }
- float4 SmartBlur( float2 tex : TEXCOORD0 ) : COLOR0 {
- float4 color = tex2D(s3, tex);
- const float c = 2 * R * (color.a - 0.5);
- float weight = (1 / (c * c + 1)) * dot(color.rgb + 0.01, float3(0.2126, 0.7152, 0.0722));
- weight = max(0, weight);
- if (EdgeWeighting)
- weight *= 0.25;
- color *= weight;
- float amount = weight;
- for (int i = 0; i < M; i++) {
- float2 dir = taps[i];
- float2 s_tex = tex + rcpres * dir * c;
- float4 s_color = tex2D(s3, s_tex);
- float s_c = abs((s_color.a - 0.5) * 2 * R);
- if (c < 0)
- s_c = max(abs(c), s_c);
- weight = (1 / (s_c * s_c + 1)) * dot(s_color.rgb + 0.01, float3(0.2126, 0.7152, 0.0722));
- weight *= saturate(1 - smoothstep(s_c, s_c * 1.1, length(dir) * abs(c)));
- if (EdgeWeighting)
- weight *= saturate(0.25 + 0.75 * pow(length(dir) * c / (s_c), 2));
- color += s_color * max(0, weight);
- amount += max(0, weight);
- }
- #ifdef SMOOTHBLUR
- return float4(color.rgb / amount, c / (2 * R) + 0.5);
- #endif
- return float4(color.rgb / amount, 1);
- }
- float4 HorizontalBlur(float2 tex : TEXCOORD0) : COLOR0 {
- float4 color = tex2D(s3, tex);
- const float c = 2 * R * (color.a - 0.5);
- float scale = c / 8; color *= 6;
- color += tex2D(s3, float2(tex.x - (rcpres.x ) * scale, tex.y)) * 4;
- color += tex2D(s3, float2(tex.x + (rcpres.x ) * scale, tex.y)) * 4;
- color += tex2D(s3, float2(tex.x - (rcpres.x * 2) * scale, tex.y)) * 1;
- color += tex2D(s3, float2(tex.x + (rcpres.x * 2) * scale, tex.y)) * 1;
- return float4(color.rgb / 16, c / (2 * R) + 0.5);
- }
- float4 VerticalBlur(float2 tex : TEXCOORD0) : COLOR0 {
- float4 color = tex2D(s3, tex);
- const float c = 2 * R * (color.a - 0.5);
- float scale = c / 8; color *= 6;
- color += tex2D(s3, float2(tex.x, tex.y - (rcpres.y ) * scale)) * 4;
- color += tex2D(s3, float2(tex.x, tex.y + (rcpres.y ) * scale)) * 4;
- color += tex2D(s3, float2(tex.x, tex.y - (rcpres.y * 2) * scale)) * 1;
- color += tex2D(s3, float2(tex.x, tex.y + (rcpres.y * 2) * scale)) * 1;
- return float4(color.rgb / 16, 1);
- }
- technique T0 < string MGEinterface = "MGE XE 0"; >
- {
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 DepthOfField();
- }
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 SmartBlur();
- }
- #ifdef SMOOTHBLUR
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 HorizontalBlur();
- }
- pass {
- VertexShader = compile vs_3_0 FrameVS();
- PixelShader = compile ps_3_0 VerticalBlur();
- }
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement