• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
FantasyVII

2D Deferred lighting in XNA

7 posts in this topic

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?

 

1111fs.png

 

 

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
0

Share this post


Link to post
Share on other sites

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:

ScreenShot004.png

1

Share this post


Link to post
Share on other sites

You'll need a spot light shader

 

Some reference http://ogldev.atspace.co.uk/www/tutorial21/tutorial21.html and http://www.gamedev.net/topic/424058-per-pixel-spotlight-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!

1

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

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

 

Some reference http://ogldev.atspace.co.uk/www/tutorial21/tutorial21.html and http://www.gamedev.net/topic/424058-per-pixel-spotlight-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:

ScreenShot004.png

 

thanks :)

 

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

0

Share this post


Link to post
Share on other sites

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.

1

Share this post


Link to post
Share on other sites

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 :)

0

Share this post


Link to post
Share on other sites

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.

 

light1.png
light2n.png

 

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

 

light3v.png

 

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
0

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  
Followers 0