Sign in to follow this  
Nokame

Very Basic 2D Lighting

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.

[CODE]
void DrawLightmap()
{
this.GraphicsDevice.SetRenderTarget(0, this.map.lightMap);

//clear to some small ambient light
this.GraphicsDevice.Clear(this.map.ambience);

//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
light.Draw(this.spriteBatch);
}
else
{
// Draw the light shape
light.Draw(this.spriteBatch);
}
}
this.spriteBatch.End();

this.GraphicsDevice.SetRenderTarget(0, null);
}
[/CODE]

[CODE]
// 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);

spriteBatch.End();
[/CODE]



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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this