Help - Search - Members - Calendar
Full Version: Lighting In Games.
Game Maker Community > Working with Game Maker > Advanced Users Only
andrewmc
More and more lighting/shadow engines are getting created recently. Although most of them do their job they are extremely slow. I created this topic so we can discuss the best way to go about creating one of these systems (particularly for a topdown game, I don't see any other uses unless we're talking 3D games). Hopefully people will post their engines so others can improve on the engine and we can set a lighting standard for GM games!
EricDB
The most basic function of a lighting engine is to obscure the parts of the scene that aren't near a light. This can be done by creating a surface the same size as the visible area on screen, and clearing it to white. Then, for each light, you draw a fuzzy black blob at the same position on the surface where the light will appear on screen. Then after you draw your room, you draw the surface on top of it, in bm_subtract mode. That causes the white areas to completely subtract from the underlying colors, resulting in a black area. And the black areas (where your lights are), don't subtract anything, so the graphics there remain visible. The gray areas partially darken the underlying image.

That's the basic idea. You can make a light dimmer by using draw_set_alpha to reduce the alpha level when you draw your light's blob onto the shadow surface. You can make the light bigger or smaller by scaling the blob sprite. You can make the light colored by using a color other than black for the blob...you need to use the inverse color...in other words, a red light would need a cyan blob (green + blue). You can vary the ambient lighting (the default light level) by clearing your shadow surface to some shade of gray instead of white.

So, that's lighting. Shadow casting is a whole different subject, and much more intricate. I would love to be proven wrong on this, but I would say that fast, accurate, dynamic shadow casting is probably a bit beyond GM's capabilities at this point, unless the number of lights and shadowed objects is kept very small (5 lights and 20 objects = 100 shadows to draw every step!).

If I were going to attempt it, I'd probably compromise and treat every object geometrically like a circle, rather than trying to cast a pixel-perfect shadow. For each light, I'd start with a fresh black shadow surface, then I'd draw its blob in white (or gray, more likely). Then, for every object in the blob's radius that can cast a shadow, I'd draw a polygon in black, starting at that object, and extending outward from the light...basically, in the shape of the shadow that object would cast.

So, now I have a black surface, with the white (or gray) areas showing where my light is shining. I'd use additive mode to draw it onto the "master" shadow surface, repeating the process for each light. Then, I'd negate the master surface (draw it on a white surface in bm_subtract mode), and use it as described above. Obviously, that's a lot of drawing for each step, so you'd want to keep the number of lights to a minimum.
FireScreen
I think this is the fastest one so far:
http://forums.gamemaker.nl/index.php?showtopic=163769
cbdman25
QUOTE (Dark Walk @ Oct 18 2005, 01:03 AM)
I think this is the fastest one so far:
http://forums.gamemaker.nl/index.php?showtopic=163769
*


It just happens to be your own! rolleyes.gif
RhysAndrews
What a coincedence! tongue.gif

One method, using d3d_lighting, was to imitate a 2D game but in d3d mode.. Which is quite easy to do.

As for 2D games; I think Dark_Walks Engine is the best for what we can do without a DLL.

Regards
Rhys Andrews
joecreator
There is an extremely simple way!!!!

draw_set_blend_mode(bm_add)
draw_circle(x,y,32,c_white,c_black,0)
draw_set_blend_mode(bm_normal)

That's all there is to it!!!
GearGOD
I would also -love- to be proven wrong, but shadow casting is indeed beyond GM from the (numerous) tests I've done. There is only so much optimization you can do, you still face the brick wall of having to go through every vertex of the shadow-casting objects and perform a few calculations on them. There's also the problem that all the methods to merge shadow-casting with proper lighting requires two surfaces and switching between them. The switching function is actually quite nasty on the speed.

Raw lighting is extremely fast. If I remember right, Luminaire is the fastest at it so far. When there are no shadows, you only deal with one surface and additive sprites. If you're willing to give up per-pixel quality, you can even juice more speed out of it. At this point the lighting renders are going to be fast enough not to worry about any speed drop at all.

Dark Walk has ventured into bloom filters and the rest with his lighting lib, something I've also done but not released because I found it too slow. In my opinion, very basic shadow casting and raw lighting is as far as one can go in GM. Blooms, blurs, soft shadows, complex shadows etc are all outside the realistic speed bounds. I've released a proper hard edged shadow casting demo here. It's not perfectly optimized, but fairly close to the best that can be juiced out of GM, and you can judge the speeds yourself.
Kairos
Here's one interesting WIP that I found that seems to use D3D/ surfaces of some sort (a guess). I don't know how it works, he never mentioned any hint of how the shadows work in the first place.
http://forums.gamemaker.nl/index.php?showtopic=160121

Anyway...
QUOTE
There is an extremely simple way!!!!

draw_set_blend_mode(bm_add)
draw_circle(x,y,32,c_white,c_black,0)
draw_set_blend_mode(bm_normal)

That's all there is to it!!!


If you read the posts above more clearly, the topic isn't going to be about simple lighting and unrealistic engines...
Dark Walk's engine is, by far, the fastest one I've seen so far, although at some point the whole thing looks choppy, due to the shadows. I'm interested in the link I posted above though, because the shadow system seems to work, and look somewhat realistic without dropping the speed of the game, but I have no idea what the GM user did to create the shadows.
Catdaemon
Nice fast way:
Draw black rectangle with alpha of your choice ( can simulate time of day ).
Draw white circle sprites with blend mode add with alpha mask so it is more visible in the center. edit: draw them ABOVE the black rectangle.

It does the job pretty well, you can used different coloured sprites to make different coloured lights. That is how I do it in my game.
Potnop
What's faster though, using a surface or drawing a rectangle across the screen? I want to do this for a 640X480 view.
Catdaemon
QUOTE (Potnop @ Nov 13 2005, 03:15 PM)
What's faster though, using a surface or drawing a rectangle across the screen?  I want to do this for a 640X480 view.
*


Probably a rectangle
FireScreen
QUOTE (Kairos @ Oct 18 2005, 05:02 PM)
Here's one interesting WIP that I found that seems to use D3D/ surfaces of some sort (a guess). I don't know how it works, he never mentioned any hint of how the shadows work in the first place.
http://forums.gamemaker.nl/index.php?showtopic=160121

Anyway...
QUOTE
There is an extremely simple way!!!!

draw_set_blend_mode(bm_add)
draw_circle(x,y,32,c_white,c_black,0)
draw_set_blend_mode(bm_normal)

That's all there is to it!!!


If you read the posts above more clearly, the topic isn't going to be about simple lighting and unrealistic engines...
Dark Walk's engine is, by far, the fastest one I've seen so far, although at some point the whole thing looks choppy, due to the shadows. I'm interested in the link I posted above though, because the shadow system seems to work, and look somewhat realistic without dropping the speed of the game, but I have no idea what the GM user did to create the shadows.
*


I'm actually making my final lighting engine right now. It will have:
Lighting
Shadows
And normal maps (thank GearGOD to for this one too)
In this will you will able to choose you will able to choose quality and speed at your choice.
Quality + Speed = 1
I also hope to make shadows even faster in this one and fix up many glitches.
Well that's about all the info I can give you on it now.
Until then look for it in the next week. It will be under the name:
Radiance Edt: It seems that name has been taken so it will now be named: Eclipse

- Eric
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.