Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (SBGuy), 652 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
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 Offline OP

User
TSG_Torsten  Offline 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 laugh

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 laugh


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:
Code:
// 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:
Code:
// 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] #350424
12/15/10 22:56
12/15/10 22:56
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Your screens are a bit blurry


Click and join the 3dgs irc community!
Room: #3dgs
Re: Gaussian Blur PP Pixel Shader [Re: Joozey] #350427
12/15/10 23:16
12/15/10 23:16
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
TSG_Torsten Offline OP

User
TSG_Torsten  Offline OP

User

Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
Sorry to write this, but, you know what a gaussian BLUR shader is? wink

Regards
TSGames

Re: Gaussian Blur PP Pixel Shader [Re: TSG_Torsten] #350436
12/16/10 01:17
12/16/10 01:17
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Yeah was just in a laughing mood tongue. Nice work on it. Doesn't seem to work on draw_line functions though. But I'm sure that's an inability of 3dgs rather than your sample.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Gaussian Blur PP Pixel Shader [Re: Joozey] #350470
12/16/10 12:03
12/16/10 12:03
Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
TSG_Torsten Offline OP

User
TSG_Torsten  Offline OP

User

Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
That's correct, anything within the gui like PANEL, TEXT or draw_line won't be blurred. Post-Processing shaders only apply to the given camera-view and everything that is visible in there.
To make blurry lines, try using draw_line3d.

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 Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Pretty awesome, it works laugh. 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 Offline OP

User
TSG_Torsten  Offline OP

User

Joined: Mar 2005
Posts: 564
/www/europe/ germany/index.php
Nice to hear that you can use it wink

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? wink

Regards
TSGames

Re: Gaussian Blur PP Pixel Shader [Re: Joozey] #350500
12/16/10 15:41
12/16/10 15:41
Joined: Mar 2006
Posts: 2,252
Hummel Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
Doesnt look like you are using real gaussian weights, so it´s actually not a gaussian-blur. Also Joozey is right, the blur should be non-directional.
Another thing: using something like '#define HARD' instead of 'if(hard)' is smarter and faster.

Re: Gaussian Blur PP Pixel Shader [Re: Hummel] #350514
12/16/10 18:01
12/16/10 18:01
Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
Pappenheimer Offline
Senior Expert
Pappenheimer  Offline
Senior Expert

Joined: Sep 2003
Posts: 5,900
Bielefeld, Germany
You made me curious about the "#define" versus "if()". And, I asked myself how you write a define with several lines.
In case someone cares, I found this about those defines:
http://www.coker.com.au/russell/ccode/macros.html

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 Offline
Expert
Hummel  Offline
Expert

Joined: Mar 2006
Posts: 2,252
Code:
#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
Code:
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.

Page 1 of 3 1 2 3

Moderated by  adoado, checkbutton, mk_1, Perro 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1