Starbound / Terraria Style Lighting

So I am currently developing a 2D sandbox game similar to Terraria and Starbound. I would like to set up the lighting for the game at this point similar to that of the games mentioned above. I have never done lighting before so I am curious if there are any guides or methods or anything out there that anybody might suggest.

Here is what we have right now:

Lighting used is just a simple directional light. We are looking for something more like this:

As you can see in the second screen shot (Starbound), the light does not penetrate all the way through the terrain. Almost like a fog of war type of effect going on there, except instead of showing previously explored areas it only shows illuminated areas. I am curious how this effect might be achieved, any and all help is appreciated!

P.S. - We are currently using a mesh based system for loading chunks of blocks near the player. Block data is saved to map file in the form of an array of ushorts (for blockID) that makes up the front and back blocks.

Its totally fake, from what I can tell. They are looking at cells in the tilemap and calculating the distance to the nearest open air block, and using that to decide how bright to make the tile. Sort of similar to a blur algorithm.

I wish it were that simple, unfortunately this photo tells a different story:

Here we see the player in a cave and even though the blocks are exposed to air, the cave portion just beyond the player goes totally dark. Below the player offscreen is lava which is illuminating the blocks from below, and the orange colored ore is also giving off lighting of its own.

I did some custom work for a company not so long ago with Unity 5 which does what you want and they are using it for a mobile title. PM me if you want it.

Below is an image of how it Works, there are no real lights in this scene at all thus you can have as many lights as you want without any impact on the graphics.

2 Likes

I believe Terraria uses a flood fill style algorithm where each light looks like this:

[0][0][0][0][0][0][0][0]
[0][0][0][1][0][0][0][0]
[0][0][1][2][1][0][0][0]
[0][1][2][3][2][1][0][0] <Starts at 3 and then calls all directions decreasingly
[0][0][1][2][1][0][0][0]
[0][0][0][1][0][0][0][0]
[0][0][0][0][0][0][0][0]

When the light encounters a solid block, it decreases the light value. When a light hits another light, it chooses the higher of the two values. The last post in this thread is very helpful for understanding how to attempt this:

http://www.java-gaming.org/topics/starbound-lighting-techneques/26363/view.html

I too am unsure of the best way to apply the result in a shader, however. It looks cool in daytime/cave settings:


But finding an efficient shader for only draw on the foreground elements at night was frustrating (sky should not be lit!).

It’s possible that the lights were simulated using a Line of Sight (also known as Field of Vision) algorithm found in most Rogue-like games, with the light intensity dimming as it goes further from the point of origin or until it hits a solid surface. In Terraria’s case, the FOV will need to be calculated for the Player character as well as for each of the light sources.

Once you’ve got the values from the algorithm, you can use it to change the color (or vertex color) of each tile to simulate the shadows. And for performance reasons, the lights should only be recalculated if a light casting object changes position.

Late to the party, but here’s an example on GitHub: GitHub - BigDaddyGameDev/Tile-Light-and-Shadows-Like-Terraria: An example of how to drawing Terraria style block lighting and shadows.

It uses a two cameras to get a buffered blend. One camera takes an image of areas with no blocks, applies a gaussian blur and blends the image with the other camera.