# 2D Deferred lighting in XNA

## Recommended Posts

FantasyVII    1074

Hello everyone,

So I have been trying to learn HLSL and I think I got the hang of it. I found this awesome tutorial that show how to implement deferred lighting. and I think I understand it. However there is just one thing that I would like to know. How can I change the light shape from point light to cone shape like light?

Here is a video of my implementation of deferred lighting.

http://www.youtube.com/watch?v=Fp8dCFCqACc

and here is the code

struct Light
{
float2 position;
float4 color;
float power;
};

texture colortexture;

int numberOfLights;
Light lights[4];

float ambient;
float4 ambientColor;

float screenWidth;
float screenHeight;

sampler ColorMap = sampler_state
{
Texture = <colortexture>;
};

float4 CalculateLight(Light light, float4 base, float3 pixelPosition)
{
float2 direction = light.position - pixelPosition;
float distance = 1 / length(light.position - pixelPosition) * light.power;
float amount = max(dot(base, normalize(distance)), 0);

return base * distance * light.color * ambient;
}

float4 Deferredlight(float2 texCoords : TEXCOORD0) : COLOR
{
float4 base = tex2D(ColorMap, texCoords);

float3 pixelPosition = float3(screenWidth * texCoords.x, screenHeight * texCoords.y, 0);

float4 finalColor = (base * ambientColor * ambient);
for(int i=0; numberOfLights; i++)
{
finalColor += CalculateLight(lights[i], base, pixelPosition);
}

return finalColor;
}

technique Deferred
{
pass Pass1
{
PixelShader = compile ps_2_0 Deferredlight();
}
}

Edited by FantasyVII

#### Share this post

##### Share on other sites
VladR    722

Start experimenting with transformation of the distance in either / both of (x,y) directions - you will get different shapes this way. If you double the x-coord, the light cone will be twice as wide (and so on).

Play with it and see what shape you like.

Alternatively, you should look into GI - namely Radiosity and check the Area Lighting, where if you take into account the ypos of the light (e.g. how high above ground it is), the shape of the cone will, obviously, change due to physical properties of a light distribution.

A Radiosity gives you shapes like this:

#### Share this post

##### Share on other sites
kauna    2922

You'll need a spot light shader

With deferred rendering you'll need also a mesh to contain the light, a full screen quad will do fine for beginning but more optimized version will use something more of a cone shape.

Practically a spot light has just a bit more parameters than omni light ie. cone direction, inner and outer cone angles.

Cheers!

#### Share this post

##### Share on other sites
AmzBee    485

I just had a poke around on that tutorial you are using, it looks like the author unfortunately doesn't understand what differed means. The technique he's using to me looks more like the way BasicEffect implements it's 3 lights with added normal mapping. There's nothing wrong with that, but I thought I'd point it out as you are learning as I am and it's I'm sure you'd prefer to understand it.

To further explain what deferred lighting means, check out this Wikipedia article; http://en.wikipedia.org/wiki/Deferred_lighting

There are many ways to create lighting in a 2D game, and the tutorial you are following is definitely one of those, if you were after shadowing you could even do something like Catalin Zima explains on his website: http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/ however it's important to note that these methods usually seldom go together very easily.

Although it does not offer shadows nor normal mapping out the box, I have written an example on my developer blog to demonstrate how to accomplish straight forward light mapping: http://xpod-games.com/2013/04/2d-light-mapping-in-xna-4-0/

I hope this helps.

Aimee

#### Share this post

##### Share on other sites
FantasyVII    1074

I just had a poke around on that tutorial you are using, it looks like the author unfortunately doesn't understand what differed means. The technique he's using to me looks more like the way BasicEffect implements it's 3 lights with added normal mapping. There's nothing wrong with that, but I thought I'd point it out as you are learning as I am and it's I'm sure you'd prefer to understand it.

To further explain what deferred lighting means, check out this Wikipedia article; http://en.wikipedia.org/wiki/Deferred_lighting

There are many ways to create lighting in a 2D game, and the tutorial you are following is definitely one of those, if you were after shadowing you could even do something like Catalin Zima explains on his website: http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/ however it's important to note that these methods usually seldom go together very easily.

Although it does not offer shadows nor normal mapping out the box, I have written an example on my developer blog to demonstrate how to accomplish straight forward light mapping: http://xpod-games.com/2013/04/2d-light-mapping-in-xna-4-0/

I hope this helps.

Aimee

I have looked at your code and it was really good. I have implemented something like that in the past and I was very pleased with it. However the light felt dull. it didn't feel quite right. But with the shader I used the light felt alive. It has that glow feel to it.

I used this tutoril before.

http://blog.josack.com/2011/07/xna-2d-dynamic-lighting.html

thanks :)

You'll need a spot light shader

With deferred rendering you'll need also a mesh to contain the light, a full screen quad will do fine for beginning but more optimized version will use something more of a cone shape.

Practically a spot light has just a bit more parameters than omni light ie. cone direction, inner and outer cone angles.

Cheers!

thanks :)

just one thing. Why draw full screen quad? how will it help?

Start experimenting with transformation of the distance in either / both of (x,y) directions - you will get different shapes this way. If you double the x-coord, the light cone will be twice as wide (and so on).

Play with it and see what shape you like.

Alternatively, you should look into GI - namely Radiosity and check the Area Lighting, where if you take into account the ypos of the light (e.g. how high above ground it is), the shape of the cone will, obviously, change due to physical properties of a light distribution.

A Radiosity gives you shapes like this:

thanks :)

I tried that but I couldn't get it to work :(

#### Share this post

##### Share on other sites
phil_t    8084

just one thing. Why draw full screen quad? how will it help?

That advice was for when people thought you were talking about deferred rendering. With deferred rendering, you draw lights on during the final pass. The "dumb"/easy way to do it is with full screen quads. The more optimized way is to render geometry that just encloses the light volume, so you don't end up drawing invisible pixels that don't contribute to the final scene.

#### Share this post

##### Share on other sites
FantasyVII    1074

just one thing. Why draw full screen quad? how will it help?

That advice was for when people thought you were talking about deferred rendering. With deferred rendering, you draw lights on during the final pass. The "dumb"/easy way to do it is with full screen quads. The more optimized way is to render geometry that just encloses the light volume, so you don't end up drawing invisible pixels that don't contribute to the final scene.

I see.

thanks :)

#### Share this post

##### Share on other sites
FantasyVII    1074

ok so this is what I have so far. I made two types of point lights. they are perfect for what i need. What is left the cone shape light. Can someone please look at my code and see if I'm going in the right path or not.

Here are the images of both lights that workes perfectly.

and here is the image of the cone light that I have a problem with.

Clearly it looks nothing like a cone.

The function that i use to create the cone shape light is "CalculateConeLight".

struct Light
{
float2 Position;
float4 Color;
float Radius;
float Intensity;
};

float4x4 MatrixTransform;
texture ColorTexture;

int numberOfLights;
Light lights[15];

float AmbientIntensity;
float4 AmbientColor;

float ScreenWidth;
float ScreenHeight;

sampler ColorMap = sampler_state
{
Texture = <ColorTexture>;
};

float4 CalculateLight(Light light, float4 TextureMap, float3 PixelPosition)
{
float2 Direction = light.Position - PixelPosition;
float Distance = saturate(1 / length(Direction) * light.Radius);

return TextureMap * Distance * light.Color * light.Intensity;
}

float4 CalculatePointLight(Light light, float4 TextureMap, float3 PixelPosition)
{
float2 Direction = light.Position - PixelPosition;
float Distance = 1 - length(Direction) / light.Radius;

return TextureMap * Distance * light.Color * light.Intensity;
}

float4 CalculateConeLight(Light light, float4 TextureMap, float3 PixelPosition)
{
float2 Direction = light.Position - PixelPosition;
float rho = dot(normalize(light.Position), -Direction);
float phi = cos(45 * 0.5);
float coneAttenuation  = saturate(1 - max(0, rho) / phi);

return TextureMap * coneAttenuation  * light.Color * light.Intensity;
}

float4 Deferredlight(float2 TexCoords : TEXCOORD0) : COLOR
{
float4 TextureMap = tex2D(ColorMap, TexCoords);

float3 PixelPosition = float3(ScreenWidth * TexCoords.x, ScreenHeight * TexCoords.y, 0);

float4 FinalColor = (TextureMap * AmbientColor * AmbientIntensity);

for(int i = 0; i <= numberOfLights; i++)
FinalColor += CalculateLight(lights[i], TextureMap, PixelPosition);

return FinalColor;
}

void VertexShaderFunction(inout float4 color    : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
}

technique Deferred
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 Deferredlight();
}
}

Edited by FantasyVII

## 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