Guest User

BetterBloomFixed

a guest
Jul 11th, 2017
1,767
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Better bloom for MGE XE
  2. // Does brightpass before blur and up to 4 passes of 9 weighted blur samples
  3.  
  4. // Configurable variables:
  5.  
  6. //More passes = higher quality, values above 4 or under 1 have no effect
  7. #define BLOOM_PASSES 4
  8.  
  9. static const float bloom_blur_radius = 2.0f; // Blur radius, higher needs more passes to look good
  10. static const float bloom_threshold = 0.9f; // Brightness threshold for blooming
  11. static const float bloom_power = 0.7f; // Overall maximum bloom blending power
  12. static const float bloom_blue = 1.3f; // How much to multiply the blue channel
  13. static const float bloom_saturation = 0.7f; // Overall bloom saturation
  14.  
  15. // End of configurable variables
  16.  
  17. static const float3 coef = {0.3, 0.59, 0.11};
  18. static const float bloom_br = bloom_blur_radius * 0.001;
  19. texture lastpass;
  20. texture lastshader;
  21.  
  22. sampler s0 = sampler_state { texture=<lastshader>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
  23.  
  24. sampler s1 = sampler_state { texture=<lastpass>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
  25.  
  26.  
  27. float4 blurHor1(in float2 Tex : TEXCOORD) : COLOR0
  28. {
  29.  
  30. float4 c = 0.0;
  31.  
  32. //cheap blur
  33. c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
  34. c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
  35. c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
  36. c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
  37. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  38. c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
  39. c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
  40. c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
  41. c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
  42.  
  43. return c;
  44. }
  45.  
  46. float4 blurHor2(in float2 Tex : TEXCOORD) : COLOR0
  47. {
  48.  
  49. float4 c = 0.0;
  50.  
  51. //cheap blur
  52. c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
  53. c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
  54. c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
  55. c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
  56. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  57. c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
  58. c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
  59. c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
  60. c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
  61.  
  62. return c;
  63. }
  64.  
  65. float4 blurHor3(in float2 Tex : TEXCOORD) : COLOR0
  66. {
  67.  
  68. float4 c = 0.0;
  69.  
  70. //cheap blur
  71. c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
  72. c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
  73. c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
  74. c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
  75. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  76. c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
  77. c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
  78. c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
  79. c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
  80.  
  81. return c;
  82. }
  83.  
  84. float4 blurHor4(in float2 Tex : TEXCOORD) : COLOR0
  85. {
  86.  
  87. float4 c = 0.0;
  88.  
  89. //cheap blur
  90. c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
  91. c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
  92. c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
  93. c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
  94. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  95. c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
  96. c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
  97. c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
  98. c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
  99.  
  100. return c;
  101. }
  102.  
  103. float4 blurVert1(in float2 Tex : TEXCOORD) : COLOR0
  104. {
  105.  
  106. float4 c = 0.0;
  107.  
  108. c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
  109. c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
  110. c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
  111. c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
  112. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  113. c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
  114. c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
  115. c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
  116. c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
  117.  
  118. return c;
  119.  
  120. }
  121.  
  122. float4 blurVert2(in float2 Tex : TEXCOORD) : COLOR0
  123. {
  124.  
  125. float4 c = 0.0;
  126.  
  127. c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
  128. c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
  129. c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
  130. c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
  131. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  132. c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
  133. c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
  134. c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
  135. c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
  136.  
  137. return c;
  138.  
  139. }
  140.  
  141. float4 blurVert3(in float2 Tex : TEXCOORD) : COLOR0
  142. {
  143.  
  144. float4 c = 0.0;
  145.  
  146. c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
  147. c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
  148. c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
  149. c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
  150. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  151. c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
  152. c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
  153. c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
  154. c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
  155.  
  156. return c;
  157.  
  158. }
  159.  
  160. float4 blurVert4(in float2 Tex : TEXCOORD) : COLOR0
  161. {
  162.  
  163. float4 c = 0.0;
  164.  
  165. c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
  166. c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
  167. c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
  168. c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
  169. c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
  170. c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
  171. c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
  172. c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
  173. c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
  174.  
  175. return c;
  176.  
  177. }
  178.  
  179. float4 tB0(in float2 Tex : TEXCOORD) : COLOR0
  180. {
  181. float4 scene = tex2D(s0, Tex);
  182. float4 blur = tex2D(s1, Tex);
  183.  
  184. // apply power mult
  185. blur *= bloom_power;
  186.  
  187. // apply saturation
  188. blur.rgb = lerp(blur.rgb, dot(coef.xyz, blur.rgb), 1-saturate(bloom_saturation) );
  189. blur.b = saturate(blur.b*bloom_blue);
  190.  
  191. // apply bloom to scene
  192. scene = 1.0 - ((1.0 - scene) * (1.0 - blur));
  193.  
  194. return scene;
  195. }
  196.  
  197. //First brightpass
  198. float4 brightPass(in float2 Tex : TEXCOORD) : COLOR0
  199. {
  200. float4 c = tex2D(s0, Tex);
  201. c = smoothstep(bloom_threshold, 1.0, c );
  202.  
  203. return saturate(c);
  204. }
  205.  
  206. technique T0 < string MGEinterface = "MGE XE 0"; >
  207. {
  208. //Brightpass before the blur
  209. pass brightPass { PixelShader = compile ps_2_0 brightPass(); }
  210.  
  211. //Main blur pass
  212. pass blurH1 { PixelShader = compile ps_2_0 blurHor1(); }
  213. pass blurV1 { PixelShader = compile ps_2_0 blurVert1(); }
  214.  
  215. //More blur passes if desired?
  216. #if BLOOM_PASSES > 1
  217.  
  218. pass blurH2 { PixelShader = compile ps_2_0 blurHor2(); }
  219. pass blurV2 { PixelShader = compile ps_2_0 blurVert2(); }
  220.  
  221. #if BLOOM_PASSES > 2
  222.  
  223. pass blurH3 { PixelShader = compile ps_2_0 blurHor3(); }
  224. pass blurV3 { PixelShader = compile ps_2_0 blurVert3(); }
  225.  
  226. #if BLOOM_PASSES > 3
  227.  
  228. pass blurH4 { PixelShader = compile ps_2_0 blurHor4(); }
  229. pass blurV4 { PixelShader = compile ps_2_0 blurVert4(); }
  230.  
  231. #endif
  232.  
  233. #endif
  234.  
  235. #endif
  236.  
  237. pass ApplyBloom { PixelShader = compile ps_2_0 tB0(); }
  238.  
  239.  
  240.  
  241. }
RAW Paste Data