• Create Account

## 2D Deferred lighting in XNA

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.

7 replies to this topic

### #1FantasyVII  Members

1003
Like
0Likes
Like

Posted 11 April 2013 - 05:34 AM

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.

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, 11 April 2013 - 07:38 AM.

722
Like
1Likes
Like

Posted 11 April 2013 - 08:46 AM

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:

VladR    My 3rd person action RPG on GreenLight:    http://steamcommunity.com/sharedfiles/filedetails/?id=92951596

### #3kauna  Members

2918
Like
1Likes
Like

Posted 11 April 2013 - 09:58 AM

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!

### #4Tarika  Members

485
Like
1Likes
Like

Posted 11 April 2013 - 10:35 AM

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

We are now on Tumblr, so please come and check out our site!

### #5FantasyVII  Members

1003
Like
0Likes
Like

Posted 11 April 2013 - 11:22 AM

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

### #6phil_t  Members

7649
Like
1Likes
Like

Posted 11 April 2013 - 12:00 PM

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.

### #7FantasyVII  Members

1003
Like
0Likes
Like

Posted 11 April 2013 - 12:24 PM

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

### #8FantasyVII  Members

1003
Like
0Likes
Like

Posted 13 April 2013 - 11:06 AM

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 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
{
PixelShader = compile ps_3_0 Deferredlight();
}
}


Edited by FantasyVII, 13 April 2013 - 11:07 AM.

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.