Efficient Terraria-Like 2D Lighting

I am working on a terraria-clone and I am aiming for it to be able to be run on most Android devices. I am able to generate a world, jump around, etc but I am not looking to add in some efficient lighting. In the past I have tried doing a tile-based lighting system but the project quickly came to an end after that because it dropped performance hugely. In that demo I was drawing a transparent black square over each tile and setting the alpha value dependent on surrounding lights. I envision that this will not be efficient on mobile so I don’t even know if I want to go down that road. I don’t need the shadows or anything like that (as of right now), I just need the lighting portion implemented. I have also looked at the other suggestions on the page here but am not sure which is the best option.

If tile-based lighting is the way to go I’d have to think of the best way to go about it. I don’t think that it would be a problem to just have it work with a torch giving off light and only updating light values when a light source is added/removed, but when entities are incorporated I also have to think about how will I efficiently update an entities light value based off of surround light values. I have optimized the hell out of the game so far, with a world of 1,620,000 tiles being contained within it and getting a stable, smooth 30 fps even on mobile. I haven’t attempted creating larger worlds than that but I don’t see why, at this rate, it wouldn’t be able to become any larger. I am trying to keep it as optimized as possible throughout development of the entire game so coming up with an efficient, optimized lighting system is essential.

Any suggestions are greatly appreciated. Thanks.

You could use an image mask: a transparent/colored circle that fades to black. Then just position that circle where you want a light to be. Google image search “game development lighting mask” for a bunch of results.

Or if you’re using libGDX, just use Box2DLights.

Also, why would you need to light every tile in the game? You just have to light the ones you’re drawing, which should only be the ones on the screen.

I’d try to do it with masking I guess. I have never done any lightning effects but I came across this site and favorited it for future use. Hopefully it helps you with your problem.

I never said that I was lighting every tile in the game at all times. Obviously I wouldn’t do that. I said that every tile needs to be able to be lit. I think that I came up with a different idea for the tile-based lighting which I’m going to test out now. I don’t think the circle lighting will look good in the game.

Yes I’ve used this in one of my published games, Undetected. Im not sure that I want the circular lighting anymore now though.

Excited to say that I’ve implemented the basis of the lighting system! It is tile based like I said and it also “crawls” along areas when blocks aren’t in their way. I used a portion of the algorithm provided here and modified it to fit my system.

Here is an image of the lighting so far:

I have to add in the handling of breaking/placing blocks and updating the light efficiently.

Looks great! How did you do the lightning effect? With masking?

I actually just used basic coloring tinting! So in a way this may be see as a mask but this allowed me to create the light effect with only one draw pass and save a LOT of performance. I’m using LibGDX as the backend so, if you’re familiar with it, I just set the spritebatch’s color to a shade of black dependent on the amount of light coming in.

I’m using a two pass process, with a light-mask rendered over the whole scene in a second pass. It works fine. You can see the results over there:

That looks awesome! I think the main reason I don’t want to do that type of lighting though is because I want the blocks to occlude light (to diminish light when it passes them). This algorithm I modified let’s me do it very efficiently, especially when a block is removed in the light and the light needs to be recalculated.

Edit: Here is a new update from the lighting I’ve been working on. I also added in basic walls to go behind the blocks.

I’m now working on removing lights which is being a pain with the calculations when more than one light source is next to eachother. I also have to fix the way that the sun generates light down onto the ground because my algorithm now doesn’t work how it should when a block is modified.