Jump to content

  • Log In with Google      Sign In   
  • Create Account

[XnA] 2D Lighting - How is it done?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 rApp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 04:30 AM

Hello!
I've been curious lately on how you make your own "2D Lighting"-engine? (Is it called engine?)
I've tried to find a XnA-tutorial on how to achieve this but my search havent been successful, most finds have been in a 3D enviroment.. :/

What i want to achieve is something like in this game
http://www.youtube.c...h?v=UGo7iMUcxLc

Im completety noob when it comes to this so please be patience! :(
Are there any tutorials for XnA 2D Lighting? (No 3D! :()

Sponsor:

#2 Jon Hellebuyck   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 08:10 AM

I'm an old Direct3D user rather than XnA, but hopefully a lot of this translates to what you're doing. I wrote a 2D engine using textured quads (triangle strips) and added the sort of 2D lighting shown in your video. I did it by altering the vertex colors and intensities to simulate lighting.

First I added a "light" (or several) to a scene, which was really just a 2D x,y coordinate, an RGB color, an intensity, and a range. I made a class called "light" to store that data.

Since 2D textured quads are rendered using the texture colors modulated by the vertex colors, your vertex colors are normally all white (red is 255, green is 255, blue is 255) if you want to see the original texture. If you want to darken the texture, you can darken the vertex colors (something like 127, 127, 127) and your texture will appear darker.

I used this texture modulation feature to do my 2D lighting. Each scene refresh I would set all my scene vertices to an unlit "ambient" level. This could be a little below 255, 255, 255, if I want very subtle lighting or 0, 0, 0, if I want the player to only see what is being lit (like the cavern levels in your video). Then I checked the distance of each light in the scene against each vertex in the scene and, if the light affected the vertex (by that I mean that it was close enough), I would add that light's color and intensity to the vertex's "ambient" color.

Once I'm done accumulating the light effects on each vertex, I use the accumulated color to render the quad, and it appears to be lit. This method does have its limitations, but it will get you simple 2D colored per-vertex lighting. Again, I'm an old-school D3D guy (DX8.1), so maybe they've come up with an easier way to do 2D lighting since then with XnA.
Jon Hellebuyck - Mode13.com

#3 rApp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 08:22 AM

Okey, I think I get what you are saying.
But how would i make everything dark and then get the bright thing to "overrun" that? (Sorry for being novice, but I really wanna know these things! :))

#4 geolycosa   Members   -  Reputation: 217

Like
0Likes
Like

Posted 02 May 2011 - 08:37 AM

For my 2D games, I wrote a light pre-pass renderer as described by Wolfgang Engel here. I wrote it as if I were writing a 3D renderer, but was able to take some geometric shortcuts because I knew the game was a 2D platformer. The image below shows the results (light pre-pass + tone mapping + bloom). The material on the tiles use normal and specular maps in addition to their diffuse color. The characters are rendered without light.

Posted Image

Using this approach, my engine supports hundreds of dynamic lights at very high framerates. Most 2D engines that have lighting aren't this complex though. You can get acceptable results by simply rendering your lights with additive blending after you've rendered the rest of your scene. You should check out a game called The Archer which is being developed over at TIGSource for an example of this.
Will Miller | Game Designer | Big Huge Games

#5 rApp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 08:55 AM

Geolycosa, how are you making the rest of the map black?
Are you placing some kind of texture over the whole scene or are you drawing that with a black color if the "light" can't reach?

And how are you making, sorry for sounding absolutely stupid but, the light go in all directions?
I mean what's making it act like a "ray"?

#6 geolycosa   Members   -  Reputation: 217

Like
0Likes
Like

Posted 02 May 2011 - 09:13 AM

Geolycosa, how are you making the rest of the map black?
Are you placing some kind of texture over the whole scene or are you drawing that with a black color if the "light" can't reach?


I use three maps to compute light at each pixel: a normal map, a specular / diffuse intensity map, and a color map. The specular / diffuse intensity map modulates the specular and diffuse light contribution to each pixel.


I don't compute a "ray" so to speak. The lighting process is detailed on Engel's blog, but essentially it works by rendering in these passes:

Geometry Pass (out: gbuffer) - Encode position and normal vectors into a gbuffer

Light Pass (out: light buffer) - For each light, sampling the gbuffer, render light color * N.L * attenuation in R, G, and B channels, then the specular contribution in the A channel. Render with additive blending.

Material Pass (in: light buffer) - Forward render each object and compute light by sampling the light buffer.

Again, if you're really interested in the nuts and bolts of this, I would encourage you to study Engel's work on the subject.
Will Miller | Game Designer | Big Huge Games

#7 rApp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 09:18 AM


Geolycosa, how are you making the rest of the map black?
Are you placing some kind of texture over the whole scene or are you drawing that with a black color if the "light" can't reach?


I use three maps to compute light at each pixel: a normal map, a specular / diffuse intensity map, and a color map. The specular / diffuse intensity map modulates the specular and diffuse light contribution to each pixel.

Okey, how are these maps made?
I mean like, are there three different textures? (Have they been made by you using a program like photoshop or are they made "on the run"?)
If they are made "on the run", do you have any good tutorials for this?

As far as i've reached now im only printing a 1x1px texture stretching the whole screens resolution, and then just fades this using COLOR in the Draw-method.. This doesnt feel like im doing it right! :P

#8 geolycosa   Members   -  Reputation: 217

Like
0Likes
Like

Posted 02 May 2011 - 09:27 AM



Geolycosa, how are you making the rest of the map black?
Are you placing some kind of texture over the whole scene or are you drawing that with a black color if the "light" can't reach?


I use three maps to compute light at each pixel: a normal map, a specular / diffuse intensity map, and a color map. The specular / diffuse intensity map modulates the specular and diffuse light contribution to each pixel.

Okey, how are these maps made?
I mean like, are there three different textures? (Have they been made by you using a program like photoshop or are they made "on the run"?)
If they are made "on the run", do you have any good tutorials for this?

As far as i've reached now im only printing a 1x1px texture stretching the whole screens resolution, and then just fades this using COLOR in the Draw-method.. This doesnt feel like im doing it right! :P


There are three different textures used per material. I used Photoshop to create the color and light intensity maps. For the intensity maps, I have diffuse intensity in RGB and specular intensity in A. The normal maps were generated in a program called Crazy Bump. Crazy bump is pretty good at generating normal maps from organic looking images (rocks and stuff), but not so good at making maps for non-organic surfaces. For that, I would suggest actually modeling the shapes in a 3D modeling application (Google's SketchUp will suffice) and generating the normal maps that way.



Will Miller | Game Designer | Big Huge Games

#9 rApp   Members   -  Reputation: 100

Like
0Likes
Like

Posted 02 May 2011 - 09:57 AM

I've read some of the different links you have provided me but i still don't really get where I should start... :/

If I wanna start simple, a square that gives you light to a scene.
-Is this "easy" to make?
If yes, where do i start?
I've tried to find tutorials that is 2D and with XnA but i havent found a single one that works.. All the others seems to be working with 3D which i dont wanna get into right now.

Sorry for being a pain :(

#10 geolycosa   Members   -  Reputation: 217

Like
0Likes
Like

Posted 02 May 2011 - 10:12 AM

I've read some of the different links you have provided me but i still don't really get where I should start... :/

If I wanna start simple, a square that gives you light to a scene.
-Is this "easy" to make?
If yes, where do i start?
I've tried to find tutorials that is 2D and with XnA but i havent found a single one that works.. All the others seems to be working with 3D which i dont wanna get into right now.

Sorry for being a pain :(



You're not going to find a tutorial on exactly how to do this in 2D (it's not something that's commonly done), but the 3D implementations you're probably looking at are very close to the 2D ones (the math and setup are exactly the same). If you want an XNA implementation of light pre-pass, check out J. Coluna's excellent post on the subject. If these links don't make sense to you, this lighting implementation might be out of your reach at this point. I would recommend finding some basic XNA tutorials on shader-based per-pixel lighting (even though they are 3D) and figure out how they work. It's simple to take those techniques and use them for 2D.
Will Miller | Game Designer | Big Huge Games




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS