2D – Smooth lighting with shadows for a tile-based game

| | August 4, 2015

for the last week or so I have been completely stuck on trying to implement efficient lighting. I’ve been researching and experimenting and I’ve looked everywhere that I have been capable of finding, and I just cannot figure it out. So, now I’m posting here, with the hope that someone can maybe help me. I’m trying to make a 2D tile-based game, similar to Terraria, and I’m trying to avoid Terraria’s style of tile-based lighting and go for something smoother, like Starbound (here), except with more visual shadows. I have successfully implemented something to what I would like to see, but, sadly, it’s incredibly inefficient. This is what I’ve tried so far:

  1. Tiled based lighting that is put in a lightmap as a single grayscale pixel and is then scaled up and (with a shader?) smoothed out so that it looks non-tiled. This approach is similar to what I’ve seen (here), but is done in xna and I’m not sure if there is a BlendState equivalent thing. I also had never done anything with shaders and my attempt at smoothing out the tiles ended after I could only smooth them horizontally or vertically (not in a circle as it should be).

  2. Raycasting and drawing each pixel with a certain alpha value for the distance it is away from the light source. This approach is the one that worked, but was inefficient. The first problem I encountered with this is that raycasting with around 360 rays per point light missed a bunch of pixels. I solved this by then using a shader to make a black pixel within the radius of a light the proper color if it was next to at least two non-black pixels, so that it wasn’t a shadow. This worked, but sadly, it was far too inefficient, dropping about 300 FPS per light. But did bring me close to what I want, and I would post a picture, but sadly I’m limited to two links :(

and now I’m here, quite unsure of what to do next, are either of these two approaches that I’ve done before usable? Is there an even better route to take?

Thank you for taking the time to read my problem and hopefully help me out :)

P.S. I’ve heard of Box2D, and I really don’t want to use it, mainly because this is a learning experience for me and I don’t learn too much by using someone else’s code.

Edit: I’m completely new to libGDX and OpenGL in general, and I know that there’s a way to create a mesh using vertices and I was wondering if that might be the key, if I create a mesh from the raycasts’ end points and somehow, using a shader, create an alpha gradient on the mesh? I really don’t know how to create a mesh nor do I know how to create a gradient on a weirdly shaped object.

2 Responses to “2D – Smooth lighting with shadows for a tile-based game”

  1. This is a post by @OrangePixel for one way to make efficient lightning with Libgdx.

    It describes a simple technique. You can put alpha blended Dot above the map and make it seem like a lightning effect. The article is interesting and all the thanks go to the author!


  2. dnk drone.vs.drones on November 30, -0001 @ 12:00 AM

    Have you consider using chunk of pixel in your second point to get better performance?

    If you search for a different aproach you can investigate Cellular automaton , starting from lights chunk for each neighbour lite them according to distance and material repeat for its neighbours …

Leave a Reply