Hi all,
for some time I wasn't very active in the forums, so I hope some of you still know me
I want to post a small piece of code I was working one since I think may some of you could use it.
GS includes a pp_gaussian.fx shader file, which is great, but I missed a way to set the strength of the gaussian blur filter.
So, I was working on it to make it possible to change the strength.
But, before any codes, of course everyone wants to see a picture
As already mentioned, I used the essential GS-gaussian-filter file and just added the strength-option and changed the sample-algorithm.
pp_gaussian.fx File:
// Gaussian Blur PixelShader 2.0
// Template from GameStudio
// Changed by Torsten Simon - © 2010
// Accepts incomming skill1 - strength (0 ... 10)
// mat_name.skill1=floatv(0...10);
#include <define>
float4 vecSkill1;
// Min: 0, Max:10
Texture TargetMap;
sampler2D g_samSrcColor = sampler_state { texture = <TargetMap>; MipFilter = Linear; };
float4 vecViewPort;
float4 postprocessing_gaussian(float2 Tex : TEXCOORD0) : COLOR0
{
// If Strength>5 -> More samples needed
bool hard=vecSkill1.x>5;
// Real sample
float4 sampleM = tex2D(g_samSrcColor, Tex.xy);
// Moved samples
float4 sampleB0 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw );
float4 sampleF0 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw );
float4 sampleB1 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*3. );
float4 sampleF1 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*3. );
float4 sampleB2 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*4. );
float4 sampleF2 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*4. );
float4 sampleB3 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*5. );
float4 sampleF3 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*5. );
float4 sampleB4 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*8. );
float4 sampleF4 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*8. );
float4 sampleB5;
float4 sampleF5;
float4 sampleB6;
float4 sampleF6;
if(hard)
{
// Additional samples for high strength
sampleB5 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*11. );
sampleF5 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*11. );
sampleB6 = tex2D( g_samSrcColor, Tex.xy - vecViewPort.zw*14. );
sampleF6 = tex2D( g_samSrcColor, Tex.xy + vecViewPort.zw*14. );
}
// Brightness calculation script
float s=(10-vecSkill1.x)*0.0512;
float r=vecSkill1.x/5;
float facSub=0;
float facHard;
if(hard)
{
float temp=((vecSkill1.x-5)/5);
facSub=temp*0.0050;
facHard=temp*0.0261;
}
float fac=(0.0261-facSub)*r;
float4 ret; // return variable
if(hard) ret=(s * sampleM + fac * (sampleB0 + sampleF0) + fac * (sampleB1 + sampleF1) + fac * (sampleB2 + sampleF2)+ fac * (sampleB3 + sampleF3)+ fac * (sampleB4 + sampleF4)+ facHard * (sampleB5 + sampleF5)+ facHard * (sampleB6 + sampleF6))*1.8;
else ret=(s * sampleM + fac * (sampleB0 + sampleF0) + fac * (sampleB1 + sampleF1) + fac * (sampleB2 + sampleF2)+ fac * (sampleB3 + sampleF3)+ fac * (sampleB4 + sampleF4))*1.8;
return ret;
}
technique PostProcess
{
pass p1
{
AlphaBlendEnable = false;
VertexShader = null;
PixelShader = compile ps_2_0 postprocessing_gaussian();
}
}
Just create a file named "pp_gaussian.fx" in your project folder and paste the code.
You can implement it like this:
// Get the GS view import
#include <mtlView.c>
function main()
{
pp_set(camera,mtl_gaussian);
mtl_gaussian.skill1=floatv(7); // enter a value between 0 and 10
}
Please be aware that the look of the strength depends on the current window/screen resolution.
This is my first shader, so feel free to optimize or "correct" it. Have fun!
Regards
TSGames