6 registered members (AndrewAMD, Ayumi, degenerate_762, 7th_zorro, VoroneTZ, HoopyDerFrood),
1,268
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Gaussian Blur PP Pixel Shader
#350421
12/15/10 22:40
12/15/10 22:40
|
Joined: Mar 2005
Posts: 564 /www/europe/ germany/index.php
TSG_Torsten
OP
User
|
OP
User
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
|
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
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: TSG_Torsten]
#350487
12/16/10 14:31
12/16/10 14:31
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Pretty awesome, it works . You are blurring in one direction though, that's quite well visible on draw_line3d. Isn't gaussian a non-directional blur? Edit: For fun, when I move the blur is applied.
Last edited by Joozey; 12/16/10 14:38.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Joozey]
#350499
12/16/10 15:39
12/16/10 15:39
|
Joined: Mar 2005
Posts: 564 /www/europe/ germany/index.php
TSG_Torsten
OP
User
|
OP
User
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
|
Nice to hear that you can use it Well, you're right, actually gaussian should be a non-directional blur filter. But the template already was designed to move the samples directional. Unfortunaley, I still didn't find a way to render the samples non-directional. If I have some time, I will try to change the code. Do you have any significant performance issues using the shader? Or is everything fast enough? Regards TSGames
|
|
|
Re: Gaussian Blur PP Pixel Shader
[Re: Pappenheimer]
#350518
12/16/10 18:32
12/16/10 18:32
|
Joined: Mar 2006
Posts: 2,252
Hummel
Expert
|
Expert
Joined: Mar 2006
Posts: 2,252
|
#ifdef 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. );
#endif
instead of
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. );
}
was what I meant. Using 'if' the shader checks whether you want to use the 'hard' version every frame for every screen pixel. Thats slow.
|
|
|
|