2D Lighting System

Well, currently I’m working on a top-down, 2D action RPG. Things are interesting.

While getting things going for that, I’ve put together a little 2D lighting system that I’m really happy with - it’s pretty, simple, and fast.

Here’s a screenshot:

I also made a dev video here:

GI63eeaXbWs

What do you guys think?

Looks like a nice engine that will be very useful in your game. Might I also say that you talk very well, giving detailed instruction with clear pronounciation and no stuttering. As you develop your game more and make more development videos, people’s interest will grow more because of how you address them with new features in your game. I wish you the best of luck in making your game and hope that this will turn out to be awesome! ;D

Thank you. :slight_smile: It actually took me a lot of re-recordings because I kept on flubbing. I’m glad that I managed to get a decent take, though.

Very pretty! Right now it looks like the lights act as pinpoints – you planning on adding penumbra as well?

I’m not sure - quite probably not.

Right now, the algorithm is just not set up to handle that whatsoever; the reason it’s so fast is that I can cut corners (ensure that the geometry of the light is always clockwise, know that you never have to fade the light, just clip geometry, etc) - I’d probably have to do a full redesign to properly allow for that.

I also really like the look of hard lighting; I think it gives a scene a really sharp, edgy look, while soft shadows usually just make things look less dramatic.

That said, it’s something I can come back to at any point in time; my lighting system is separate from everything else so I don’t think there will be a major problem if I decide to upgrade.

If it is fast enough to render a bunch of lights, you can simply fake soft shadows by creating a tiny circle of lights.

That renderer looks pretty good, but you should render things in a differend order, maybe?

Currently it has this:

Also: this one was drawn in a minute with a mouse…

@Riven
Eh, I’m not sure it would be a significant enough jump in quality to warrant the extra render costs. I’ll probably look back into this later to see if I can change the algorithm to make the lights have a penumbra, but it’s not a high priority.

@matheus23
Haha yeah, that’s one of the main issues with pillars in my lighting engine. Straight-line terrain is fine, but curves are difficult, because the entire algorithm is based on line-segment polygon clipping.

The way that the lights work is that I render my scene normally into a texture, like this:

Then, I render my lights into a lighting mask texture:

And then I render the two buffers together in a shader, to make something this:

Voila. Easy-peasy lights.

To create the geometry for each light, I create the base geometry of a point light (a textured quad), and then I clip out sections using line segments as barriers. So, for the columns, I take the most efficient route and use the cross-section of the column as the barrier.

It looks great except for the top of the object. :frowning:

It’s possible that I can get away with this - one thing I did not show in the video was a “line of sight” mask that doesn’t allow you to see through walls. The tops of the pillars will be covered, so the glaring discrepancy won’t be as obvious.

I did something similar, with infinitely long shadows and a bunch of point lights:

I’m sorry, what am I looking at exactly?

:emo: Triangles casting shadows.

Could you make the shadows darker? I think I can see the shadows, but they look almost identical to the white background…

Calibrate your monitor :slight_smile:

The shadows are grey-scale 223/255 (87% white).
If that shows up as white on your monitor, it’s way off.

Ah, got it now.

How’d you go about doing this, may I ask?

My shadow-caster only knew about lines. Every line casts a shadow (a quad) that extends beyond the viewport. Obviously there is a lot of overdraw, but it was fast enough. I wrote a simple software rasterizer for it.

Ah…

So you rendered the light and then cast shadows from it? How did you handle multiple lights?

Every light is rendered into a ‘texture’ (int[w*h]) Note that you can fit 32 lights in each ‘texture’, if you set (binary OR) 1 bit per light.

It’s either 1 (shadow) or 0 (lit). As a final pass I subtract the shadows from the scene.

Ah… that’s an interesting technique. I would not have thought of that.

However, I don’t think that would work very well with what I’m trying to do. Colored lights, for example, wouldn’t work, and it could potentially add up to a ridiculous amount of texture memory being used. One of the big things I am trying to do with my current setup is to keep my lights as lightweight as humanly possible (both from an API standpoint and used resources); I’m pretty close to that right now and don’t want to add to it.

Still, thanks for the insight. Always good to see difference techniques.

Riven – any code? Sounds intriguing. Is the shadow casting (the “lines” you described) similar to OrangyTang’s method for shadow geometry?

I don’t know whether I still have the code, I’ll look into it.

And no, it’s not anything like described by OrangyTang. It’s really as simple as it can possibly be:

[x] take 1 light (L1)
[x] take 1 line (P1 -> P2)
[x] draw a line from L1 to P1, and extend it (to infinity) and call it P3
[x] draw a line from L1 to P2, and extend it (to infinity) and call it P4
[x] the quad P1,P2,P4,P3 is the shadow area.
[x] for each pixel in the shadow area, set the n-th bit to 1.