Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to make "light" in a 2D game ?

Started by AntonioR Jun 13, 2011 at 3:05 PM 14 replies 4k views
Original Post
AntonioR
AntonioR
Hi,

I was wondering how to make "light" in a 2D game ?
It doesn't need to be dynamic and cast shadows, which I know is a different topic. Static light would be enough.

Something like this:
[media]
[/media]

Or static lights here (the ones on the celing):
[media]
[/media]

Here is my method that is pretty basic and it doesn't quite work, I get some bugs when there are more "lights".
I made this up myself so I don't know is this even a good start.
I am using DirectX 9 (D3DXSprite for drawing textures) and pixel shaders (v.2).

1. I render the scene on the texture.
2. To make "light" I take a part of the texture where I want to apply light and I use my light shader on it, and then I draw it on the same place from where I took it.
3. I render the scene/texture to backbuffer.


float4 ps_light( float2 Tex : TEXCOORD0 ) : COLOR0
{
float4 Color;

//circular light
float d = sqrt((Tex.x*SCREENW-pos_x*SCREENW)*(Tex.x*SCREENW-pos_x*SCREENW)+(Tex.y*SCREENH-pos_y*SCREENH)*(Tex.y*SCREENH-pos_y*SCREENH));


if (d<size) Color = tex2D( g_samSrcColor, Tex.xy)*(1+(size-d)/size);
else
{
Color = tex2D( g_samSrcColor, Tex.xy);
}

return Color;
}


These are the results. You can see multiple "lights".
As you can see I just increase the brightness of the pixels on some area:

- you can see that bug above the character where multiple lights overlap,
- the parts that are already bright are just brighter, and dark areas are not bright enough.



unledxrq.jpg
yewbie
yewbie
I ended up using a lighting system similar to yours, what I had to do was this:
After all of my lighting calculations were applied to each vertex based on the light source I would then loop through each tile and calculate its light value based on its neighbors smoothing out the light over all.
I would go from top left to right, and from top to bottom.

After the whole loop was run the lighting turned out very smooth.

Also a little more information I was using 4 vertex squares for my tiles the top left vertex would check to the top, left, and top left, add all 4 lighting values together and divide by 4 giving me a average light to apply back to that vertex.
I would do this for each vertex on each file, I would only do this once and not for every lighting change as they would be very costly.

In game if a new light was added or created I would only recalculate a few tiles out from the max range of the light source.

Hope this helps you!
AntonioR
AntonioR
Thanks yewbie for your reply, but I didn't understand a thing. I don't think this is really a similar method to mine.

My "lights" are drawn on the whole scene/texture in real time.
You basically take a rectangle area from the rendered scene, and then draw that same area on the same place with a shader applied to it.
yewbie
yewbie

My "lights" are drawn on the whole scene/texture in real time.
You basically take a rectangle area from the rendered scene, and then draw that same area on the same place with a shader applied to it.


Ah sorry about that, I thought you were applying lighting to your individual vertex's, My approach will not work as a shader I don't think, sorry about that.
AntonioR
AntonioR
OK, I have another idea, but this requires me to render to a texture, and make it black. I know how to do that. Then I want to render it over the scene so it is actually darkness.

I want to make parts of it transparent so the area beneath it will look lighter so it will be like light.

But how do I make areas on the texture(on which I render the darkness) transparent ?
I tried with pixel shaders, and tried to set Color.a so areas should be transparent but it doesn't work.
Maybe the texture needs to be in another format or something ?
mancubit
mancubit
normally you must enable alpha blending manually to create transparent textures. check your API documentation about "alpha blending" maybe this helps
RDragon1
RDragon1
You may as well just do it the same way it's done in 3D. The approach of a deferred renderer would work very well:

1) Render your scene, but only render the unlit base texture without doing any lighting. This is your "albedo buffer".

2) Initialize a new "light buffer" render target to black

3) For each light:
- additively blend just the light's color (do the light attenuation here)

4) Multiply the albedo buffer by the light buffer to apply the lighting to the scene
AntonioR
AntonioR
Thanks, but I actually managed to make my own method, it was so simple actually.
I even recorded a cool video showing it.

Silent Hill anyone?

[media]
[/media]

silentcrafthill.jpg
yewbie
yewbie
That looks cool, nice job!
MarkFarrell
MarkFarrell
http://registry.gimp.org/node/69
I remember watching that link-dead video and him saying he has a normal map or something for each texture in the game. Do you think this tool for gimp might help?
AntonioR
AntonioR

http://registry.gimp.org/node/69
I remember watching that link-dead video and him saying he has a normal map or something for each texture in the game. Do you think this tool for gimp might help?


You can use gimp to make normal maps, but that is easy ,it is different story to know how to use them when rendering stuff.
MarkFarrell
MarkFarrell
If you've looked into it at all, could you give me any information that you've found? I'm obviously kind of a beginner but I'm just trying to exchange any information I can as I search around. Sorry if it wasn't that helpful.
AntonioR
AntonioR

If you've looked into it at all, could you give me any information that you've found? I'm obviously kind of a beginner but I'm just trying to exchange any information I can as I search around. Sorry if it wasn't that helpful.


Why -1 if I don't know how to use them ?
I am a beginner myself, working with normal maps is too advanced for me for now. I only know that NM images can be made in GIMP, but I never did it myself.
MarkFarrell
MarkFarrell
I like your second response better . In all honesty I felt like "but that is easy" kind of came across as a confrontational tone of voice for the "for beginners" section of the forum. I didn't understand at the time that you were saying that you didn't know how to use them yet. I think I realize now that it was bad reason to decrease reputation though, sorry.
AntonioR
AntonioR

I like your second response better . In all honesty I felt like "but that is easy" kind of came across as a confrontational tone of voice for the "for beginners" section of the forum. I didn't understand at the time that you were saying that you didn't know how to use them yet. I think I realize now that it was bad reason to decrease reputation though, sorry.


I'm just a noob looking for some help

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.