SHARE
TWEET

Bloom soft

a guest Aug 25th, 2016 1,550 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Author: peachykeen tweaked july 2013 by Sarafan
  2. //Title: trueBloom g6 - intermediate dev copy (g6.6)
  3. //Pack: MGE Shader Library
  4. //Desc:
  5. //        Adds complex bloom. Simulates the glare of bright areas. Adds Skybloom
  6.  
  7. // Compatibility: MGE XE 0, partially working >> Differences underwater
  8.  
  9. // USER VARS
  10.  
  11. // Switches
  12. #define BLOOM_QUALITY 3
  13. // This controls the quality of the bloom. 1 is basic, cheap bloom (twice-blurred), 2 is standard g6 quality, and 3 is SM3.0 high-quality bloom.
  14. #define BLOOM_FX_WATER 0
  15. // This turns underwatever effects on (1) and off (0)
  16. #define BLOOM_FX_SKY 1
  17. // Enables/disables blooming the sky (0 excludes sky)
  18. #define BLOOM_FX_DARK 1
  19. // Darkens/lightens scene. On is default (same as previous versions), off will brighten the scene but uses a more accurate divisor
  20. #define BLOOM_FX_HANDS 0
  21. // Enables/disables blooming the player's hands (and usually weapon) (0 excludes these)
  22. #define BLOOM_FX_IWATER 0
  23. // Enables/disables using the underwater and interior multipliers together. As interior water is usually darker, you may want to leave this off
  24.  
  25.  
  26.  
  27. // Bloom
  28. //
  29. // Most of these multipliers are exclusive, except for bloom_mult_global and bloom_mult_uwater. Uwater can combine with all others.
  30.  
  31. static const float bloom_mult_global = 0.26f; // The overall multiplier for bloom. Effects all areas.
  32. static const float bloom_mult_uwater = 1.2f; // Underwater mutliplier for bloom. Only takes effect when the camera is underwater.
  33. static const float bloom_mult_inside = 1.1f; // Interior multiplier for bloom. Can combine with bloom_mult_uwater when both are true.
  34. static const float bloom_mult_outside = 0.68f; // Exterior multiplier for bloom. Can combine with bloom_mult_uwater when both are true.
  35.  
  36. static const float bloom_cutoff = 1.08f; // Effects the strength and focus of highlights (higher produces softer, weaker highlights).
  37. static const float bloom_power = 0.82f; // Effects the focus and hue of highlights (higher creates more narrow, single-color highlights).
  38.  
  39.  
  40.  
  41. #if BLOOM_QUALITY >= 3
  42.     #define SMODEL ps_3_0
  43. #else
  44.     #define SMODEL ps_2_0
  45. #endif
  46.  
  47. texture lastpass;
  48. texture lastshader;
  49.  
  50. float3 eyepos;
  51. vector rcpres;
  52. float fogrange, fogstart;
  53. bool isInterior, isUnderwater;
  54.  
  55. sampler s0 = sampler_state { texture=<lastshader>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
  56. sampler s1 = sampler_state { texture=<lastpass>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
  57.  
  58. #if ( ( BLOOM_FX_SKY != 1 ) || ( BLOOM_FX_HANDS != 1 ) )
  59.     texture depthframe;
  60.     sampler sDepth = sampler_state { texture=<depthframe>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=clamp; addressv = clamp; };
  61. #endif
  62.  
  63.  
  64. float4 blurHorI(in float2 Tex : TEXCOORD) : COLOR0
  65. { // a simple 7-sample horizontal blur pass
  66.     #if ( ( BLOOM_FX_SKY != 1 ) || ( BLOOM_FX_HANDS != 1 ) )
  67.     #define TSAMP s1
  68.     #else
  69.     #define TSAMP s0
  70.     #endif
  71.  
  72.     float4 Color = tex2D(TSAMP, Tex);
  73.  
  74.     #if ( BLOOM_FX_DARK == 1 )
  75.         float div = 4.7;
  76.     #else
  77.         float div = 4.2;
  78.     #endif
  79.  
  80.     Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*1), Tex.y)) * 0.8;
  81.     Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*1), Tex.y)) * 0.8;
  82.  
  83.     Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*2), Tex.y)) * 0.65;
  84.     Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*2), Tex.y)) * 0.65;
  85.  
  86.     Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*3), Tex.y)) * 0.4;
  87.     Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*3), Tex.y)) * 0.4;
  88.  
  89.     #if BLOOM_QUALITY >= 2
  90.         Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*4), Tex.y)) * 0.25;
  91.         Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*4), Tex.y)) * 0.25;
  92.         #if ( BLOOM_FX_DARK == 1 )
  93.             div = 5.2;
  94.         #else
  95.             div = 4.7;
  96.         #endif
  97.    
  98.         #if BLOOM_QUALITY >= 3
  99.             Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*5), Tex.y)) * 0.15;
  100.             Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*5), Tex.y)) * 0.15;
  101.  
  102.             Color += tex2D(TSAMP, float2(Tex.x-(rcpres.x*6), Tex.y)) * 0.10;
  103.             Color += tex2D(TSAMP, float2(Tex.x+(rcpres.x*6), Tex.y)) * 0.10;
  104.             #if ( BLOOM_FX_DARK == 1 )
  105.                 div = 5.65;
  106.             #else
  107.                 div = 5.2;
  108.             #endif
  109.         #endif
  110.     #endif
  111.  
  112.     Color /= div;
  113.  
  114.     return Color*Color;
  115. }
  116.  
  117. float4 blurHor2(in float2 Tex : TEXCOORD) : COLOR0
  118. { // a simple 7-sample horizontal blur pass  
  119.  
  120.     float4 Color = tex2D(s1, Tex);
  121.  
  122.     #if ( BLOOM_FX_DARK == 1 )
  123.         float div = 4.7;
  124.     #else
  125.         float div = 4.2;
  126.     #endif
  127.  
  128.     float spread = 1-( Color.r * 0.3 ) + ( Color.g * 0.6 ) + ( Color.b * 0.1 );
  129.     spread = ( smoothstep(0, 0.7, spread) * 4 ) + 1.0f;
  130.  
  131.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*spread*1), Tex.y)) * 0.8;
  132.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*spread*1), Tex.y)) * 0.8;
  133.  
  134.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*spread*2), Tex.y)) * 0.65;
  135.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*spread*2), Tex.y)) * 0.65;
  136.  
  137.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*spread*3), Tex.y)) * 0.4;
  138.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*spread*3), Tex.y)) * 0.4;
  139.  
  140.     #if BLOOM_QUALITY >= 2
  141.         Color += tex2D(s1, float2(Tex.x-(rcpres.x*spread*4), Tex.y)) * 0.25;
  142.         Color += tex2D(s1, float2(Tex.x+(rcpres.x*spread*4), Tex.y)) * 0.25;
  143.         #if ( BLOOM_FX_DARK == 1 )
  144.             div = 5.2;
  145.         #else
  146.             div = 4.7;
  147.         #endif
  148.     #endif
  149.  
  150.     Color /= div;
  151.  
  152.     return Color;
  153. }
  154.  
  155. float4 blurHor3(in float2 Tex : TEXCOORD) : COLOR0
  156. { // a simple 7-sample horizontal blur pass  
  157.     float4 Color = tex2D(s1, Tex);
  158.  
  159.     #if ( BLOOM_FX_DARK == 1 )
  160.         float div = 4.7;
  161.     #else
  162.         float div = 4.2;
  163.     #endif
  164.  
  165.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*1), Tex.y)) * 0.8;
  166.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*1), Tex.y)) * 0.8;
  167.  
  168.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*2), Tex.y)) * 0.65;
  169.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*2), Tex.y)) * 0.65;
  170.  
  171.     Color += tex2D(s1, float2(Tex.x-(rcpres.x*3), Tex.y)) * 0.4;
  172.     Color += tex2D(s1, float2(Tex.x+(rcpres.x*3), Tex.y)) * 0.4;
  173.  
  174.     #if BLOOM_QUALITY >= 2
  175.         Color += tex2D(s1, float2(Tex.x-(rcpres.x*4), Tex.y)) * 0.25;
  176.         Color += tex2D(s1, float2(Tex.x+(rcpres.x*4), Tex.y)) * 0.25;
  177.         #if ( BLOOM_FX_DARK == 1 )
  178.             div = 5.2;
  179.         #else
  180.             div = 4.7;
  181.         #endif
  182.    
  183.         #if BLOOM_QUALITY >= 3
  184.             Color += tex2D(s1, float2(Tex.x-(rcpres.y*5), Tex.y)) * 0.15;
  185.             Color += tex2D(s1, float2(Tex.x+(rcpres.y*5), Tex.y)) * 0.15;
  186.        
  187.             Color += tex2D(s1, float2(Tex.x-(rcpres.y*6), Tex.y)) * 0.10;
  188.             Color += tex2D(s1, float2(Tex.x+(rcpres.y*6), Tex.y)) * 0.10;
  189.             #if ( BLOOM_FX_DARK == 1 )
  190.                 div = 5.65;
  191.             #else
  192.                 div = 5.2;
  193.             #endif
  194.         #endif
  195.     #endif
  196.  
  197.     Color /= div;
  198.  
  199.     return Color;
  200. }
  201.  
  202. float4 blurVertI(in float2 Tex : TEXCOORD) : COLOR0
  203. { // a simple 7-sample vertical blur pass
  204.     float4 Color = tex2D(s1, Tex);
  205.  
  206.     #if ( BLOOM_FX_DARK == 1 )
  207.         float div = 4.7;
  208.     #else
  209.         float div = 4.2;
  210.     #endif
  211.  
  212.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*1))) * 0.8;
  213.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*1))) * 0.8;
  214.  
  215.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*2))) * 0.65;
  216.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*2))) * 0.65;
  217.  
  218.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*3))) * 0.4;
  219.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*3))) * 0.4;
  220.  
  221.     #if BLOOM_QUALITY >= 2
  222.         Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*4))) * 0.25;
  223.         Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*4))) * 0.25;
  224.         #if ( BLOOM_FX_DARK == 1 )
  225.             div = 5.2;
  226.         #else
  227.             div = 4.7;
  228.         #endif
  229.    
  230.         #if BLOOM_QUALITY >= 3
  231.             Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*5))) * 0.15;
  232.             Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*5))) * 0.15;
  233.        
  234.             Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*6))) * 0.10;
  235.             Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*6))) * 0.10;
  236.        
  237.             #if ( BLOOM_FX_DARK == 1 )
  238.                 div = 5.65;
  239.             #else
  240.                 div = 5.2;
  241.             #endif
  242.         #endif
  243.     #endif
  244.  
  245.     Color /= div;
  246.  
  247.     return Color;
  248. }
  249.  
  250. float4 blurVert2(in float2 Tex : TEXCOORD) : COLOR0
  251. { // a simple 7-sample vertical blur pass
  252.     float4 Color = tex2D(s1, Tex);
  253.  
  254.     #if ( BLOOM_FX_DARK == 1 )
  255.         float div = 4.7;
  256.     #else
  257.         float div = 4.2;
  258.     #endif
  259.  
  260.     float spread = 1-( Color.r * 0.3 ) + ( Color.g * 0.6 ) + ( Color.b * 0.1 );
  261.     spread = ( smoothstep(0, 0.7, spread) * 4 ) + 1.0f;
  262.  
  263.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*spread*1))) * 0.8;
  264.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*spread*1))) * 0.8;
  265.  
  266.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*spread*2))) * 0.65;
  267.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*spread*2))) * 0.65;
  268.  
  269.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*spread*3))) * 0.4;
  270.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*spread*3))) * 0.4;
  271.  
  272.     #if BLOOM_QUALITY >= 2
  273.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*spread*4))) * 0.25;
  274.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*spread*4))) * 0.25;
  275.     #if ( BLOOM_FX_DARK == 1 )
  276.         div = 5.2;
  277.     #else
  278.         div = 4.7;
  279.     #endif
  280.     #endif
  281.  
  282.     Color /= div;
  283.  
  284.     return Color;
  285. }
  286.  
  287. float4 blurVert3(in float2 Tex : TEXCOORD) : COLOR0
  288. { // a simple 7-sample vertical blur pass
  289.     float4 Color = tex2D(s1, Tex);
  290.  
  291.     #if ( BLOOM_FX_DARK == 1 )
  292.         float div = 4.7;
  293.     #else
  294.         float div = 4.2;
  295.     #endif
  296.  
  297.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*1))) * 0.8;
  298.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*1))) * 0.8;
  299.  
  300.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*2))) * 0.65;
  301.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*2))) * 0.65;
  302.  
  303.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*3))) * 0.4;
  304.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*3))) * 0.4;
  305.  
  306.     #if BLOOM_QUALITY >= 2
  307.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*4))) * 0.25;
  308.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*4))) * 0.25;
  309.     #if ( BLOOM_FX_DARK == 1 )
  310.         div = 5.2;
  311.     #else
  312.         div = 4.7;
  313.     #endif
  314.  
  315.     #if BLOOM_QUALITY >= 3
  316.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*5))) * 0.15;
  317.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*5))) * 0.15;
  318.  
  319.     Color += tex2D(s1, float2(Tex.x, Tex.y-(rcpres.y*6))) * 0.10;
  320.     Color += tex2D(s1, float2(Tex.x, Tex.y+(rcpres.y*6))) * 0.10;
  321.     #if ( BLOOM_FX_DARK == 1 )
  322.         div = 5.65;
  323.     #else
  324.         div = 5.2;
  325.     #endif
  326.     #endif
  327.  
  328.     #endif
  329.  
  330.     Color /= div;
  331.  
  332.     return Color;
  333. }
  334.  
  335.  
  336. float4 brightPass(in float2 Tex : TEXCOORD ) :COLOR0
  337. {
  338.     float4 c = tex2D(s1, Tex);
  339.     float4 cB = c;
  340.  
  341.     c = smoothstep(0, bloom_cutoff, c);
  342.     c = pow(c, bloom_power);
  343.     c += smoothstep(bloom_cutoff, 0.5+bloom_cutoff, cB);
  344.     c.a = 1.0f;
  345.  
  346.     return c;
  347. }
  348.  
  349. float4 recombA(in float2 Tex: TEXCOORD) : COLOR0
  350. {
  351.     #if ( ( BLOOM_FX_SKY != 1 ) || ( BLOOM_FX_HANDS != 1 ) )
  352.         float d = tex2D(sDepth, Tex).r;
  353.         float4 cB = tex2D(s0, Tex);
  354.         #if ( BLOOM_FX_SKY != 1 )
  355.             cB.rgb *= saturate((fogrange - d) / (fogrange - fogstart));
  356.         #endif
  357.         #if ( BLOOM_FX_HANDS != 1 )
  358.             cB.rgb *= smoothstep(35, 50, d);
  359.         #endif
  360.     #else
  361.         float4 cB = tex2D(s0, Tex);
  362.     #endif
  363.    
  364.     float4 c = tex2D(s1, Tex);
  365.  
  366.     c = smoothstep(0, bloom_cutoff, c);
  367.     c += smoothstep(bloom_cutoff-0.5, bloom_cutoff+0.5, cB);
  368.  
  369.     return saturate(c);
  370. }
  371.  
  372. float4 tB0(in float2 Tex : TEXCOORD) : COLOR0
  373. {
  374.     // grab scene and blurred highlights
  375.     float4 scene = tex2D(s0, Tex);
  376.     float4 blur = tex2D(s1, Tex);
  377.     float4 highlights = 0;
  378.    
  379.     // apply all needed mults
  380.     blur *= bloom_mult_global;
  381.    
  382.     if ( isInterior )
  383.         blur *= bloom_mult_inside;
  384.     else
  385.         blur *= bloom_mult_outside;
  386.        
  387.     if ( isUnderwater )
  388.         blur *= bloom_mult_uwater;
  389.  
  390.     scene += blur;
  391.  
  392.     return scene;
  393. }
  394.  
  395. #if ( ( BLOOM_FX_SKY != 1 ) || ( BLOOM_FX_HANDS != 1 ) )
  396. float4 doDepthFix(in float2 Tex : TEXCOORD) : COLOR0
  397. {
  398.     float d = tex2D(sDepth, Tex).r;
  399.     float4 Color = tex2D(s0, Tex);
  400.     #if ( BLOOM_FX_SKY != 1 )
  401.         Color.rgb *= saturate((fogrange - d) / (fogrange - fogstart));
  402.     #endif
  403.     #if ( BLOOM_FX_HANDS != 1 )
  404.         Color.rgb *= smoothstep(35, 50, d);
  405.     #endif
  406.     return Color;
  407. }
  408. #endif
  409.  
  410. technique T0 < string MGEinterface = "MGE XE 0"; >
  411. {
  412.     #if ( ( BLOOM_FX_SKY != 1 ) || ( BLOOM_FX_HANDS != 1 ) )
  413.     pass fixSky { PixelShader = compile ps_2_0 doDepthFix(); }
  414.     #endif
  415.     pass blurHBA { PixelShader = compile ps_2_0 blurHorI(); }
  416.     pass blurVBA { PixelShader = compile ps_2_0 blurVertI(); }
  417.     pass filter_Bright { PixelShader = compile ps_2_0 brightPass(); }
  418.     pass blurHA { PixelShader = compile ps_2_0 blurHor2(); }
  419.     pass blurVA { PixelShader = compile ps_2_0 blurVert2(); }
  420.  
  421.     #if BLOOM_QUALITY >= 2
  422.     pass recombine_A { PixelShader = compile ps_2_0 recombA(); }
  423.     pass blurHB { PixelShader = compile SMODEL blurHor3(); }
  424.     pass blurVB { PixelShader = compile SMODEL blurVert3(); }
  425.     #endif
  426.  
  427.     pass BloomB { PixelShader = compile ps_2_0 tB0(); }
  428. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top