Tile based terrain lighting

Hi Guys,

So I’ve been trying to write a shader to light the terrain in my 2d tile based game, I’d like to achieve something that looks like Starbound’s terrain lighting:
http://flamingbard.files.wordpress.com/2013/12/starbound_mushrooms.jpg

My attempts so far have not been so successful:

It’s not as smooth as Starbound, plus it does not penetrate the ground as much.

My approach is something along the lines of:
Render transparent pixels to FBO, then apply some form of blur:

Blend this FBO with my foreground:

The performance isn’t stella, ground penetration is low, the blur isn’t smooth…

Does anyone have any pointers that could steer me towards a better solution?

thanks.

What type of blur are you using? Because it seems to me that a Gaussian blur would look a lot smoother than what you are doing now.

I tried gaussian blur (standard 2-pass), but in order to achieve a blur as smooth with a high enough blur radius, I’d have to sample too many times, and again performance goes down the gutter…

The blur im using is similar, but achieved using less samples

Whaaaa, don’t create another thread if you posted in the wrong subforum, better ask for a mod to move the topic into another subforum :confused:

Anyways, I hope you have seen my answer in the other thread :slight_smile:

Would lighting my game suffice? It’s not exactly like Starbound but it is very efficient and I can tell you how I did it if you want.

Yes please, that’d be much appreciated

Assuming that you are holding your blocks in a 2d array, all you have to do is create 3 variable to hold the rgb values of the block that has been lit and then sends a reduced amount of that light to the neighbors of the lighted block and repeat until too much light is lost;

So:


float r,g,b;
Block north,south,east,west;
public void light(){

if(r>0||g>0||b>0){
   east.r=r/4;
   east.g=g/4;
   east.b=b/4;

   west.r=r/4;
   west.g=g/4;
   west.b=b/4;
//...repeat for north and south
}
}

That’s basically how I did it. It allows for colored lights that mix and create new colors.
Note:

  • This does not use any rays of any kind so if you want walls to block light, this wont work
  • if your game does not properly render and derender blocks, this will lag a bit.

I think Craft the World uses a similar approach to lighting when zoomed out (i.e. per-block-lighting which is fast) combined with per-pixel-lighting (which is slow) that only kicks in when zoomed in. Works quite well.

Hey peeps,

After much coding and research, I’ve finally come up with a very efficient algorithm for lighting, its shader based, and looks quite close to that of starbound:

Thanks for the suggestions everyone.

Would you mind sharing the shader code? I am interested in what you came up with.

The shader code itself is actually pretty simple, it uses a standard 2-pass gaussian blur.

But applying this to the entire screen would destroy performance, as the blur radius needs to be very large for this to look good.

The trick is to render the transparent pixels as solid pixels to a screen sized FBO, then down-scale this FBO to a smaller FBO, say 1/16 the original size.

Apply the 2-pass gaussian blur to the down-scaled FBO, I used a 21-sample gaussian blur.

Then use linear sampling and up-scale this FBO to screen size, you will now have a nice smooth blurred version of the transparent pixels with a large blur radius at 16x less GPU cost.

blend this FBO with the texture being drawn, voila