Advertisement
Guest User

Depth of Field by Vtastek

a guest
Jul 11th, 2017
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. // Depth of Field
  2. // v12 by Knu
  3. // tweaked to save your poor blurry hands by peachykeen
  4. // includes distance blur in fog conditions
  5.  
  6. // Compatibility: MGE XE 0, fully working
  7.  
  8. float2 rcpres;
  9. float fov;
  10. float fogstart, fogrange;
  11.  
  12. static float t = 2.0 * tan(radians(0.5 * fov));
  13. static float k = 0.00001;
  14. static float unit2m = 0.0142;
  15. static float eps = 0.000001;
  16. static float fogoffset = saturate(-fogstart / (fogrange - fogstart));
  17.  
  18. matrix mview;
  19. matrix mproj;
  20. float3 eyepos;
  21. float3 eyevec;
  22.  
  23.  
  24.  
  25. // **
  26. // ** ADJUSTABLE VARIABLES
  27.  
  28. #define SM ps_3_0 // Shader model: ps_3_0, ps_2_0
  29. #define ROTATE // Random rotation on, if defined. ps_3_0 only
  30.  
  31. static const float fr = 60.0; // retina focal point, dpt
  32. // set slightly lower than fp to simulate myopia or as an alternative to MGE's distant blur
  33. // set slightly higher than fp+fpa to simulate hyperopia
  34.  
  35. static const float fp = 60.0; // eye relaxed focal power, dpt
  36. static const float fpa = 10.0; // accomodation, dpt
  37. // set lower to simulate presbyopia
  38.  
  39. static const float pupil = 0.006; // pupil diameter, m
  40.  
  41. static const float blur_radius = 0.273; // base blur radius;
  42. // higher values mean more blur when out of DoF and shorter DoF
  43. static const float blur_falloff = 2.0; // More means more blur and less respect for edges
  44. static const float R = 6.0; // maximum blur radius in pixels
  45. static float Rfixed = R / (1280 * rcpres.x); // standardize blur across resolutions
  46.  
  47. static float fogbias = 2.6; //more fog blur, starts earlier
  48. static float fogpower = 1.3; //fog blur power
  49.  
  50. // ** END OF
  51. // **
  52.  
  53.  
  54. texture lastshader;
  55. texture lastpass;
  56. texture depthframe;
  57.  
  58. sampler s0 = sampler_state { texture = <lastshader>; addressu = clamp; addressv = clamp; magfilter = point; minfilter = point; };
  59. sampler s1 = sampler_state { texture = <depthframe>; addressu = clamp; addressv = clamp; magfilter = point; minfilter = point; };
  60. sampler s3 = sampler_state { texture = <lastpass>; addressu = mirror; addressv = mirror; magfilter = linear; minfilter = linear; };
  61.  
  62.  
  63. #ifdef ROTATE
  64. texture tex1 < string src="noise64.tga"; >;
  65. sampler s2 = sampler_state { texture = <tex1>; addressu = wrap; addressv = wrap; magfilter = linear; minfilter = linear; };
  66. #endif
  67.  
  68.  
  69. #define M 12
  70. static float2 taps[M] =
  71. {
  72. float2(-0.326212,-0.40581),
  73. float2(-0.840144,-0.07358),
  74. float2(-0.695914,0.457137),
  75. float2(-0.203345,0.620716),
  76. float2(0.96234,-0.194983),
  77. float2(0.473434,-0.480026),
  78. float2(0.519456,0.767022),
  79. float2(0.185461,-0.893124),
  80. float2(0.507431,0.064425),
  81. float2(0.89642,0.412458),
  82. float2(-0.32194,-0.932615),
  83. float2(-0.791559,-0.59771)
  84. };
  85.  
  86.  
  87. float3 toWorld(float2 tex)
  88. {
  89. float3 v = float3(mview[0][2], mview[1][2], mview[2][2]);
  90. v += (1/mproj[0][0] * (2*tex.x-1)).xxx * float3(mview[0][0], mview[1][0], mview[2][0]);
  91. v += (-1/mproj[1][1] * (2*tex.y-1)).xxx * float3(mview[0][1], mview[1][1], mview[2][1]);
  92. return v;
  93. }
  94.  
  95.  
  96.  
  97. float4 dof(float2 tex : TEXCOORD) : COLOR0
  98. {
  99. float s = tex2D(s1, float2(0.5, 0.5)).r * unit2m;
  100.  
  101. float3 v = toWorld(tex);
  102.  
  103. v = v / length(v.xy);
  104. float hrzmask = smoothstep(0.0,0.3 + fogoffset,v.z);
  105.  
  106.  
  107. float z_corr = length(float3((tex.x - 0.5) * t, (tex.y - 0.5) * t / rcpres.y * rcpres.x, 1));
  108. float z = z_corr * unit2m * tex2D(s1, tex).r;
  109.  
  110. float savemyhands = smoothstep(0.568, 0.781, z);
  111. float savesky = hrzmask;
  112. float fpf = clamp(1 / s + fr, fp, fp + fpa);
  113. float c = pupil * (fr - fpf + 1 / z) / fr / k * blur_radius;
  114. float fog = (fogbias + fogoffset) * saturate(z / (4 * fogrange * unit2m)) * saturate(1-savesky);
  115.  
  116.  
  117.  
  118. c = min(abs(c / Rfixed) + fog * fogpower, 1) * savemyhands;
  119. //return c.xxxx;
  120. return float4(tex2D(s0, tex).rgb, c);
  121. }
  122.  
  123. float4 smartblur(float2 tex : TEXCOORD) : COLOR0
  124. {
  125. float4 color = tex2D(s3, tex);
  126. float c = color.a * Rfixed;
  127.  
  128. #ifdef ROTATE
  129. float2 rnd = normalize(tex2D(s2, tex / rcpres / 64).xy * 2 + 1);
  130. #endif
  131.  
  132. float amount = 1.0;
  133. for(int i = 0; i < M; i++)
  134. {
  135. #ifdef ROTATE
  136. float2 dir = reflect(taps[i], rnd);
  137. #else
  138. float2 dir = taps[i];
  139. #endif
  140.  
  141. float2 s_tex = tex + rcpres * dir * c;
  142. float4 s_color = tex2D(s3, s_tex);
  143. float s_c = s_color.a * Rfixed;
  144. float weight = exp2(-abs(c - s_c) / blur_falloff);
  145.  
  146. color += s_color * weight;
  147. amount += weight;
  148. }
  149.  
  150. return float4(color.rgb / amount, 1);
  151. }
  152.  
  153.  
  154.  
  155. technique T0 < string MGEinterface = "MGE XE 0"; >
  156. {
  157. pass { PixelShader = compile SM dof(); }
  158. pass { PixelShader = compile SM smartblur(); }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement