Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Very Basic 2D Lighting

This topic is 2761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there,
I've recently implemented the most basic version of 2d lighting I could find, but I can't really tell what is actually going on behind the scenes. I'm assuming it draws an alphablended color (using the line GraphicsDevice.Clear(color);) over the screen, and over images which have already been drawn. The light source is a specified color and image that is drawn on top of this screen color, illuminating the images drawn "beneath" it. I assume it does this because of the GraphicsDevice.RenderState lines in the method (as well as the sortmode):

Also note, i'm using XNA 3.1.

void DrawLightmap()
this.GraphicsDevice.SetRenderTarget(0, this.map.lightMap);

//clear to some small ambient light

//where Alpha is 0, nothing will be written
this.spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

this.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
this.GraphicsDevice.RenderState.SourceBlend = Blend.DestinationAlpha;
this.GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;
this.GraphicsDevice.RenderState.SeparateAlphaBlendEnabled = true;

foreach (LightSource light in this.map.mapLightSourceInstances)
if (light.DistanceActivate)
// Draw the light shape
// Draw the light shape

this.GraphicsDevice.SetRenderTarget(0, null);

// The Draw function has the following code at the end,
// after all other images have already been drawn.

spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);

GraphicsDevice.RenderState.SourceBlend = Blend.Zero;
GraphicsDevice.RenderState.DestinationBlend = Blend.SourceColor;
GraphicsDevice.RenderState.BlendFunction = BlendFunction.Add;

spriteBatch.Draw(map.lightMap.GetTexture(), Vector2.Zero, Color.White);


In the Draw function i call the DrawLightmap() function, which creates a RenderTarget2D object (map.lightMap). It draws this RenderTarget2D object surface AFTER it draws the portion of the map that is to be drawn... the lights illuminate the map. It looks nice and basic, exactly what i want, but It seems to be really slow. It actually runs perfectly for awhile, but then drops in FPS by roughly half after a few minutes of runtime. I was wondering if there was any obvious error in the code, or if there is a way to use this style of lighting more efficiently? HLSL would be my last resort at this point (only because the time i have to work on this project is running out, and to figure out how to implement it would take a good amount of time), but if it's the only way, I'd appreciate knowing that too. I've attached a scaled screenshot of the lighting.

Thank you.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!