• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Shaders [SOLVED] Simplest 2d emboss bumpmapping shader

M

Mann_mit_Hut

Guest
Hey guys!

I searched the old forums and the web for a simple and fast emboss bumpmapping shader.

I think most mobile games are completly fine without cumputationally expensive multiple lightsources and real shadows, but a little bumpmapping from a long distant 45° "sunlight" would add much to the visuals of rotating sprites.

So what i'm searching for is a simple shader which a sprite and a bumpmap get passed to, and it renders simple (but fast) emboss bumpmapping for a fixed angle of incoming light.

I am not good at shader programming, so i hope someone has some code snippets or ideas for me ;)

Thanks!
 
M

Mann_mit_Hut

Guest
I already experimented with normal maps, of course its the 'perfect' solution, but i'm searching for a simpler solution for mobile.

1. Generating real normalmaps is lots of work, bumpmaps for are just paintable (i know you can convert them, but thats not perfect at all)
2. The typical normalmap shaders as you posted are using pointlights, i'm thinking of one distand light that might look cool with longshadows.
3. Normalmap shaders are expensive to compute, a little fake should do it
 
M

Mann_mit_Hut

Guest
Hey guys,

now i'm trying myself because it shouldn't be so hard ;)

I got a simple shader up and running, but i'm stuck at getting the bumpmap into the shader. It seems to ignore my second texture and handles it as it was the original gm_basetexture.

Code:
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform sampler2D bump; //bump map

void main()
{
    gl_FragColor = texture2D( bump, v_vTexcoord );
}
Now this should draw the bumpmap instead of the usual sprite, but it draws the usual sprite as well.
I'm calling it with the draw event:

Code:
texture_bump = sprite_get_texture(sprite_index, 1);  //index 1 of the sprite is the bumpmap
shader_bump = shader_get_sampler_index(shd_emboss,"bump");

shader_set( shd_emboss );
texture_set_stage(shader_bump,texture_bump)
draw_sprite_ext(sprite1,0,x,y,1,1,image_angle,c_white,1)
shader_reset();
Any ideas what could be wrong?
Thanks!!
 
Hmm... nothing much is coming to mind. Is the sprite set to use 3d?

By the way what does your bump shader look like? A normal map shader should be a lot faster than starting with a bump map, since won't you basically have to convert the bump map into a normal map inside of the shader anyway? I tend to draw my normal maps by hand using a normalSphere as my pallete. And even though the process is pretty crude, the result is satisfactory (although these are pretty much just geometric shapes (not a lot of organic slopes):
 
M

Mann_mit_Hut

Guest
Wow that was it, the "use for 3d" checkbox.
Whatever that means :D Thank you very much
No i don't convert them to normalmaps, i just want to cheaply fake some bumpmapping for a mobile game ;)
 
I

icuurd12b42

Guest
I'll repeat what's been said, a normal map is faster.

If you want to use a grayscale to figure out what is bumpy then you need to do pixel analysis... so instead of doing 1 lookup on the extra sampler you'll need to do 8 to figure out the direction of the bump. all that to arrive at the same result, a directional vector to apply your offset or bump or light or whatever you are planning.

If you are aiming for a mobile then the 8 fetch per pixel is way expensive, essentially doing live what this can to for you pre-cooked.

http://cpetry.github.io/NormalMap-Online/
 
M

Mann_mit_Hut

Guest
You are right, but i just wanted to do some emboss 'fake' bumpmapping.

I'm pretty proud of my first selfmade-shader!
Emboss bumpmapping from 1 directional light with 2 samplers.

The luminousity of the final pixel just gets added, not calculated 'correctly', anyway the effect is fine for me.

Code:
uniform sampler2D bump; //bump map
uniform float angle; //Angle in degrees
uniform float texsize; // size of the texture in px

void main()
{
    float A = radians(angle-45.0);//Angle in radians
    vec2 Offset = vec2(1.3/texsize*sin(A),-1.3/texsize*cos(A));
    vec4 Emboss = texture2D( bump, v_vTexcoord - Offset ) - texture2D( bump, v_vTexcoord+Offset ) ;
    gl_FragColor = v_vColour * texture2D( gm_BaseTexture, v_vTexcoord ) + Emboss ;
}
 

Attachments

Top