Advertisement
Guest User

MGE fog-linked DoF

a guest
Jan 7th, 2015
4,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.28 KB | None | 0 0
  1. // Shaped Bokeh Depth of Field
  2. // CoC Calculations based off Knu's DoF shader for MGE
  3. // Blurring Algorithm Created by Tomerk
  4. // Optimized by Ethatron
  5. // Ported to MGE by vtastek
  6.  
  7. int mgeflags = 0;
  8.  
  9. // ---------------------------------------
  10. // TWEAKABLE VARIABLES.
  11.  
  12. float fogrange;
  13. float fogstart;
  14.  
  15. extern float fr = 59.715; // retina focus point, dpt
  16. // set slightly lower than fp to simulate myopia or as an alternative to MGE's distant blur
  17. // set slightly higher than fp+fpa to simulate hyperopia
  18.  
  19. extern float fp = 60.0; // eye relaxed focus power, dpt
  20. extern float fpa = 0.0; // accomodation, dpt
  21. // set lower to simulate presbyopia
  22.  
  23. extern float base_blur_radius = 0.383; // base blur radius;
  24. // higher values mean more blur when out of DoF and shorter DoF.
  25.  
  26. extern float R = 32.0; // maximum blur radius in pixels;
  27.  
  28. extern bool EdgeWeighting = 1; // 0 has constant weighting in highlights,
  29. // 1 gives edges more weight in highlights
  30.  
  31. extern bool NoWeaponBlur = 1; // Set to true to keep weapons from blurring
  32. extern float weaponblur_cutoff = 40.0; // Cutoff distance for what is considered a weapon
  33.  
  34. extern float fogblur_modifier = 10.0;
  35.  
  36. #undef SMOOTHBLUR // when on, smooths the blur, may have a performance hit
  37.  
  38. extern float zzTargetPosX = 0.5;
  39. extern float zzTargetPosY = 0.5;
  40. // forcus target position
  41.  
  42.  
  43. // END OF TWEAKABLE VARIABLES.
  44. // ---------------------------------------
  45.  
  46.  
  47.  
  48. float2 rcpres;
  49.  
  50. texture lastshader;
  51. texture lastpass;
  52. texture depthframe;
  53.  
  54. sampler PassSamplerL = sampler_state {
  55. Texture = <lastshader>;
  56.  
  57. AddressU = Mirror;
  58. AddressV = Mirror;
  59.  
  60. MagFilter = Linear;
  61. MinFilter = Linear;
  62. MipFilter = None;
  63. };
  64.  
  65. sampler s1 = sampler_state { texture=<depthframe>; AddressU=Clamp; AddressV=Clamp; };
  66. sampler s3 = sampler_state { texture=<lastpass>; AddressU=Mirror; AddressV=Mirror; MagFilter=Linear; MinFilter=Linear; };
  67.  
  68.  
  69.  
  70. struct VSOUT
  71. {
  72. float4 vertPos : POSITION;
  73. float2 UVCoord : TEXCOORD0;
  74. };
  75.  
  76. struct VSIN
  77. {
  78. float4 vertPos : POSITION0;
  79. float2 UVCoord : TEXCOORD0;
  80. };
  81.  
  82. VSOUT FrameVS(VSIN IN)
  83. {
  84. VSOUT OUT = (VSOUT)0.0f;
  85.  
  86. OUT.vertPos = IN.vertPos;
  87. OUT.UVCoord = IN.UVCoord;
  88.  
  89. return OUT;
  90. }
  91.  
  92.  
  93.  
  94. #define M 60
  95. static float2 taps[M] =
  96. {
  97. float2( 0.0000, 0.2500 ),
  98. float2( -0.2165, 0.1250 ),
  99. float2( -0.2165, -0.1250 ),
  100. float2( -0.0000, -0.2500 ),
  101. float2( 0.2165, -0.1250 ),
  102. float2( 0.2165, 0.1250 ),
  103. float2( 0.0000, 0.5000 ),
  104. float2( -0.2500, 0.4330 ),
  105. float2( -0.4330, 0.2500 ),
  106. float2( -0.5000, 0.0000 ),
  107. float2( -0.4330, -0.2500 ),
  108. float2( -0.2500, -0.4330 ),
  109. float2( -0.0000, -0.5000 ),
  110. float2( 0.2500, -0.4330 ),
  111. float2( 0.4330, -0.2500 ),
  112. float2( 0.5000, -0.0000 ),
  113. float2( 0.4330, 0.2500 ),
  114. float2( 0.2500, 0.4330 ),
  115. float2( 0.0000, 0.7500 ),
  116. float2( -0.2565, 0.7048 ),
  117. float2( -0.4821, 0.5745 ),
  118. float2( -0.6495, 0.3750 ),
  119. float2( -0.7386, 0.1302 ),
  120. float2( -0.7386, -0.1302 ),
  121. float2( -0.6495, -0.3750 ),
  122. float2( -0.4821, -0.5745 ),
  123. float2( -0.2565, -0.7048 ),
  124. float2( -0.0000, -0.7500 ),
  125. float2( 0.2565, -0.7048 ),
  126. float2( 0.4821, -0.5745 ),
  127. float2( 0.6495, -0.3750 ),
  128. float2( 0.7386, -0.1302 ),
  129. float2( 0.7386, 0.1302 ),
  130. float2( 0.6495, 0.3750 ),
  131. float2( 0.4821, 0.5745 ),
  132. float2( 0.2565, 0.7048 ),
  133. float2( 0.0000, 1.0000 ),
  134. float2( -0.2588, 0.9659 ),
  135. float2( -0.5000, 0.8660 ),
  136. float2( -0.7071, 0.7071 ),
  137. float2( -0.8660, 0.5000 ),
  138. float2( -0.9659, 0.2588 ),
  139. float2( -1.0000, 0.0000 ),
  140. float2( -0.9659, -0.2588 ),
  141. float2( -0.8660, -0.5000 ),
  142. float2( -0.7071, -0.7071 ),
  143. float2( -0.5000, -0.8660 ),
  144. float2( -0.2588, -0.9659 ),
  145. float2( -0.0000, -1.0000 ),
  146. float2( 0.2588, -0.9659 ),
  147. float2( 0.5000, -0.8660 ),
  148. float2( 0.7071, -0.7071 ),
  149. float2( 0.8660, -0.5000 ),
  150. float2( 0.9659, -0.2588 ),
  151. float2( 1.0000, -0.0000 ),
  152. float2( 0.9659, 0.2588 ),
  153. float2( 0.8660, 0.5000 ),
  154. float2( 0.7071, 0.7071 ),
  155. float2( 0.5000, 0.8660 ),
  156. float2( 0.2588, 0.9659 )
  157. };
  158.  
  159. static float k = 0.00001;
  160.  
  161. float4 DepthOfField(float2 tex : TEXCOORD0) : COLOR0 {
  162. float depth = tex2D(s1,tex).r;
  163. float focus = tex2D(s1, float2(0.5, 0.5)).r;
  164. float s = focus * 0.0142;
  165. float z = depth * 0.0142;
  166. float weapon = 1;
  167.  
  168. float fpf = clamp(1 / s + fr, fp, fp + fpa);
  169. float c = base_blur_radius * 0.009 * (fr - fpf + 1 / z) / fr / k;
  170. c = sign(z - s) * min(abs(c), R) / (2 * R);
  171. float fogblur = smoothstep((fogstart/fogblur_modifier)-(fogblur_modifier*25), fogstart/fogblur_modifier, z);
  172. c *= fogblur;
  173. if (NoWeaponBlur) {
  174. weapon = smoothstep(weaponblur_cutoff-0.05, weaponblur_cutoff, z);
  175. c *= weapon;
  176. }
  177.  
  178. return float4(tex2D(PassSamplerL, tex).rgb, c + 0.5);
  179. }
  180.  
  181. float4 SmartBlur( float2 tex : TEXCOORD0 ) : COLOR0 {
  182. float4 color = tex2D(s3, tex);
  183. const float c = 2 * R * (color.a - 0.5);
  184.  
  185. float weight = (1 / (c * c + 1)) * dot(color.rgb + 0.01, float3(0.2126, 0.7152, 0.0722));
  186. weight = max(0, weight);
  187. if (EdgeWeighting)
  188. weight *= 0.25;
  189.  
  190. color *= weight;
  191. float amount = weight;
  192.  
  193. for (int i = 0; i < M; i++) {
  194. float2 dir = taps[i];
  195.  
  196. float2 s_tex = tex + rcpres * dir * c;
  197. float4 s_color = tex2D(s3, s_tex);
  198. float s_c = abs((s_color.a - 0.5) * 2 * R);
  199.  
  200. if (c < 0)
  201. s_c = max(abs(c), s_c);
  202.  
  203. weight = (1 / (s_c * s_c + 1)) * dot(s_color.rgb + 0.01, float3(0.2126, 0.7152, 0.0722));
  204. weight *= saturate(1 - smoothstep(s_c, s_c * 1.1, length(dir) * abs(c)));
  205.  
  206. if (EdgeWeighting)
  207. weight *= saturate(0.25 + 0.75 * pow(length(dir) * c / (s_c), 2));
  208.  
  209. color += s_color * max(0, weight);
  210. amount += max(0, weight);
  211. }
  212.  
  213. #ifdef SMOOTHBLUR
  214. return float4(color.rgb / amount, c / (2 * R) + 0.5);
  215. #endif
  216.  
  217. return float4(color.rgb / amount, 1);
  218. }
  219.  
  220. float4 HorizontalBlur(float2 tex : TEXCOORD0) : COLOR0 {
  221.  
  222. float4 color = tex2D(s3, tex);
  223. const float c = 2 * R * (color.a - 0.5);
  224. float scale = c / 8; color *= 6;
  225.  
  226. color += tex2D(s3, float2(tex.x - (rcpres.x ) * scale, tex.y)) * 4;
  227. color += tex2D(s3, float2(tex.x + (rcpres.x ) * scale, tex.y)) * 4;
  228.  
  229. color += tex2D(s3, float2(tex.x - (rcpres.x * 2) * scale, tex.y)) * 1;
  230. color += tex2D(s3, float2(tex.x + (rcpres.x * 2) * scale, tex.y)) * 1;
  231.  
  232. return float4(color.rgb / 16, c / (2 * R) + 0.5);
  233. }
  234.  
  235. float4 VerticalBlur(float2 tex : TEXCOORD0) : COLOR0 {
  236.  
  237. float4 color = tex2D(s3, tex);
  238. const float c = 2 * R * (color.a - 0.5);
  239. float scale = c / 8; color *= 6;
  240.  
  241. color += tex2D(s3, float2(tex.x, tex.y - (rcpres.y ) * scale)) * 4;
  242. color += tex2D(s3, float2(tex.x, tex.y + (rcpres.y ) * scale)) * 4;
  243.  
  244. color += tex2D(s3, float2(tex.x, tex.y - (rcpres.y * 2) * scale)) * 1;
  245. color += tex2D(s3, float2(tex.x, tex.y + (rcpres.y * 2) * scale)) * 1;
  246.  
  247. return float4(color.rgb / 16, 1);
  248. }
  249.  
  250.  
  251. technique T0 < string MGEinterface = "MGE XE 0"; >
  252. {
  253. pass {
  254. VertexShader = compile vs_3_0 FrameVS();
  255. PixelShader = compile ps_3_0 DepthOfField();
  256. }
  257.  
  258. pass {
  259. VertexShader = compile vs_3_0 FrameVS();
  260. PixelShader = compile ps_3_0 SmartBlur();
  261. }
  262.  
  263. #ifdef SMOOTHBLUR
  264. pass {
  265. VertexShader = compile vs_3_0 FrameVS();
  266. PixelShader = compile ps_3_0 HorizontalBlur();
  267. }
  268.  
  269. pass {
  270. VertexShader = compile vs_3_0 FrameVS();
  271. PixelShader = compile ps_3_0 VerticalBlur();
  272. }
  273. #endif
  274. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement