Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Bloom shader... — Gideros Forum

Bloom shader...

SinisterSoftSinisterSoft Maintainer
edited August 2016 in Code snippets
Here is a faster, but lower quality, bloom shader if anyone is interested - it does it in less passes and samples only 25 pixels for the blur.

Use the normal vShader.glsl, and have this code for the fShader.glsl
uniform lowp sampler2D fTexture;
varying mediump vec2 fTexCoord;
 
void main() {
	lowp vec4 colour=texture2D(fTexture,fTexCoord);
	int i,j;
	lowp vec4 sum=vec4(0);
	for (i=-2;i<=2;i++){
		for (j=-2;j<=2;j++){
			lowp vec2 offset=vec2(i,j)*0.0015;
			sum+=texture2D(fTexture,fTexCoord+offset);
		}	
	}
	gl_FragColor=(sum/20.0)+colour;
}
You can experiment with the 0.0015 value to change how wide the sample is, and the 20.0 to change the intensity of the blur (you need the decimal point for glsl to know it's a floating point number).

First create a RenderTarget, you will draw all your game sprites to this texture during your game loop.

Apply the shader to the bitmap of the texture, like this (screendraw is the name of the bitmap that points to the texture):
	shader=Shader.new("vShader","fShader",0,{
{name="vMatrix",type=Shader.CMATRIX,sys=Shader.SYS_WVP,vertex=true}, 
{name="fColor",type=Shader.CFLOAT4,sys=Shader.SYS_COLOR,vertex=false}, 
{name="fTexture",type=Shader.CTEXTURE,vertex=false},  }, 
{{name="vVertex",type=Shader.DFLOAT,mult=3,slot=0,offset=0}, 
{name="vColor",type=Shader.DUBYTE,mult=4,slot=1,offset=0}, 
{name="vTexCoord",type=Shader.DFLOAT,mult=2,slot=2,offset=0}, });
	screenDraw:setShader(shader)
Point a bitmap sprite to the texture and add it to the stage.

If the texture isn't the same size as the screen, then you can adjust the bitmaps scaling to fit.
screenDraw:setScale(width/screenSize,height/(screenSize/screenDiv))
I have the texture at 1024*512, with screenDiv set to 2, if set to 1 then the texture would have to be square.

You can also adjust the sprite that you use as the fake 'stage' do that it the same too (I call this sprite 'screenRender').
	screenRender=Sprite.new()
	screenRender:setScale(screenSize/width,(screenSize/screenDiv)/height)
Add your sprites to the fake stage sprite.

Then in your game loop, after processing your sprites, just draw them to the render texture like this:
		screenTexture:clear(0,1)
		screenTexture:draw(screenRender)
bloom.jpg
1381 x 868 - 43K

Likes: antix, n1cke, NatWobble, pie

Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
https://deluxepixel.com
+1 -1 (+4 / -0 )Share on Facebook
Sign In or Register to comment.