Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Better bloom for MGE XE
- // Does brightpass before blur and up to 4 passes of 9 weighted blur samples
- // Configurable variables:
- //More passes = higher quality, values above 4 or under 1 have no effect
- #define BLOOM_PASSES 4
- static const float bloom_blur_radius = 2.0f; // Blur radius, higher needs more passes to look good
- static const float bloom_threshold = 0.9f; // Brightness threshold for blooming
- static const float bloom_power = 0.7f; // Overall maximum bloom blending power
- static const float bloom_blue = 1.3f; // How much to multiply the blue channel
- static const float bloom_saturation = 0.7f; // Overall bloom saturation
- // End of configurable variables
- static const float3 coef = {0.3, 0.59, 0.11};
- static const float bloom_br = bloom_blur_radius * 0.001;
- texture lastpass;
- texture lastshader;
- sampler s0 = sampler_state { texture=<lastshader>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
- sampler s1 = sampler_state { texture=<lastpass>; minfilter = linear; magfilter = linear; mipfilter = linear; addressu=mirror; addressv=mirror; };
- float4 blurHor1(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- //cheap blur
- c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
- c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
- return c;
- }
- float4 blurHor2(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- //cheap blur
- c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
- c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
- return c;
- }
- float4 blurHor3(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- //cheap blur
- c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
- c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
- return c;
- }
- float4 blurHor4(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- //cheap blur
- c += tex2D(s1, float2(Tex.x - 4.0*bloom_br, Tex.y)) * 0.05;
- c += tex2D(s1, float2(Tex.x - 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x - 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x - bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x + bloom_br, Tex.y)) * 0.15;
- c += tex2D(s1, float2(Tex.x + 2.0*bloom_br, Tex.y)) * 0.12;
- c += tex2D(s1, float2(Tex.x + 3.0*bloom_br, Tex.y)) * 0.09;
- c += tex2D(s1, float2(Tex.x + 4.0*bloom_br, Tex.y)) * 0.05;
- return c;
- }
- float4 blurVert1(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
- c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
- return c;
- }
- float4 blurVert2(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
- c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
- return c;
- }
- float4 blurVert3(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
- c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
- return c;
- }
- float4 blurVert4(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = 0.0;
- c += tex2D(s1, float2(Tex.x, Tex.y - 4.0*bloom_br)) * 0.05;
- c += tex2D(s1, float2(Tex.x, Tex.y - 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y - 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y - bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y)) * 0.16;
- c += tex2D(s1, float2(Tex.x, Tex.y + bloom_br)) * 0.15;
- c += tex2D(s1, float2(Tex.x, Tex.y + 2.0*bloom_br)) * 0.12;
- c += tex2D(s1, float2(Tex.x, Tex.y + 3.0*bloom_br)) * 0.09;
- c += tex2D(s1, float2(Tex.x, Tex.y + 4.0*bloom_br)) * 0.05;
- return c;
- }
- float4 tB0(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 scene = tex2D(s0, Tex);
- float4 blur = tex2D(s1, Tex);
- // apply power mult
- blur *= bloom_power;
- // apply saturation
- blur.rgb = lerp(blur.rgb, dot(coef.xyz, blur.rgb), 1-saturate(bloom_saturation) );
- blur.b = saturate(blur.b*bloom_blue);
- // apply bloom to scene
- scene = 1.0 - ((1.0 - scene) * (1.0 - blur));
- return scene;
- }
- //First brightpass
- float4 brightPass(in float2 Tex : TEXCOORD) : COLOR0
- {
- float4 c = tex2D(s0, Tex);
- c = smoothstep(bloom_threshold, 1.0, c );
- return saturate(c);
- }
- technique T0 < string MGEinterface = "MGE XE 0"; >
- {
- //Brightpass before the blur
- pass brightPass { PixelShader = compile ps_2_0 brightPass(); }
- //Main blur pass
- pass blurH1 { PixelShader = compile ps_2_0 blurHor1(); }
- pass blurV1 { PixelShader = compile ps_2_0 blurVert1(); }
- //More blur passes if desired?
- #if BLOOM_PASSES > 1
- pass blurH2 { PixelShader = compile ps_2_0 blurHor2(); }
- pass blurV2 { PixelShader = compile ps_2_0 blurVert2(); }
- #if BLOOM_PASSES > 2
- pass blurH3 { PixelShader = compile ps_2_0 blurHor3(); }
- pass blurV3 { PixelShader = compile ps_2_0 blurVert3(); }
- #if BLOOM_PASSES > 3
- pass blurH4 { PixelShader = compile ps_2_0 blurHor4(); }
- pass blurV4 { PixelShader = compile ps_2_0 blurVert4(); }
- #endif
- #endif
- #endif
- pass ApplyBloom { PixelShader = compile ps_2_0 tB0(); }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement