8

Forensic Friday 8: New Lighting System

New Lighting System #

Hello all sorry for the delay with this weeks Forensic Friday, the weekend was quite hectic which took up our time a bit. Now time for the new stuff. As you can tell from the large title, Containcorp has a new bespoke tile based lighting system. No longer shall lights phase through walls, now they use raycasting to light each tile individually resulting in more realistic lighting. The lighting system currently supports coloured lights and seamlessly blends between colours. So if you want your whole facility to be constantly lit red, we got you covered.

How it Works #

The tile based lighting system works by overlaying a render texture over the world. This render texture is then blended onto the world using a shader.

light grid *Initial tests with overlaying render texture *

light source test light source test

At first the shader was simply just a multiply shader. This worked well however it made lights a bit dull. There was no way to make lights look super intense, which is vital for things such as fire. After a bit of tinkering I stumbled upon premultiplied alpha blending, which allows us to change whether a color is multiplicative or additive depending on the alpha value. This was insanely useful as it meant both additive and multiplicative lights could exist on one texture!

light source test

Fire lighting using multiplicative shading

light source test

Fire lighting using additive lighting (looks much better)

Premultiplied alpha blending is described as Blend One OneMinusSrcAlpha in shader language which basically just means finalColor = source + (destination * (1.0 - source.a)); This ends up giving us the control we need over the additive and multiplicative effects.

Tiles & Color Blending #

With this new system, every tile has a continuous light value ranging from 0 to 1. 0 being a dark value and 1 being a light value. Every tile also has a stack of lights what affect that tile, which are used to calculate the total color and intensity of that tile. So a tile with 2 lights affecting it, a blue and red, would result in a magenta color. This representation is quite great as it allows for many complex interactions between different lights.

Early color mixing attempts producing less than desirable resultsfinally got color mixing working
early color mixingcolor mixing working

disco party

Small disco party around a blazing fire (demonstration of additive and multiplicative lights mixing)

Rays for Days #

After getting all this done I moved onto making lights actually interact with tiles in the world. I initially went for a simple floodfill algorithm for the lights which worked fine, however playing around with them a bit I realised that’s not how light travels in real life. So I decided to go for a more realistic approach using raycasting. It worked by sending a bunch of rays in a circle, and lighting up the tiles along the ray until it hits something or the ray surpasses its range. This worked beautifully and stopped weird issues like light bending around walls and such. I can also use this method for explosions to calculate the damage, since pressure waves and light can be modelled roughly the same way. In the future I am hoping to add bounce lighting to make the lighting even more snazzy.

lights no longer ignoring walls

Lights no longer ignoring walls

dark room with a single light illuminating it

Dark room with a single light illuminating it

raycasted lights creating more realistic behaviours

Raycasted lights creating more realistic behaviours

Case Closed #

Well that’s all for this fortnight. We didn’t get an insane amount of progress on the game due to the first week being a little break for us. Containcorp requires a lot of work, most of which is spent in front of a computer. Naturally this isn’t the most healthy ordeal, and Containcorp has been affecting our health a bit. However we are learning the importance of breaks in maintaining productivity and preventing burnout. In the end we are still laying the foundations for what will hopefully be a great and fun game. That’s all I have for this Forensic Friday, case closed.

The Team,
Plasmarc Studios