Sign in to follow this  

Problem with 2d textures and normal mapping

This topic is 3486 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, I'm new in XNA, and I'm making a game like Monkey Island but I want to make some pretty light effects in the 2d textures with normals. To do that, I have downloaded a example from http://ziggyware.com/readarticle.php?article_id=137. The problem for my is that this example is a spot light and not a point light. Here is the fx file for the effect: sampler ColorSampler : register(s0); sampler NormalSampler : register(s1); float3 LightPosition; float4 LightColor = float4(1, 0, 0, 1); float4 AmbientColor = 1; float ScreenWidth; float ScreenHeight; float4 Deferred2DNormalMappingPS(float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0 { float4 tex = tex2D(ColorSampler, texCoord); float3 normal = tex2D(NormalSampler, texCoord); float3 pixelPosition = float3(ScreenWidth * texCoord.x, ScreenHeight * texCoord.y, 0); float3 lightDir = LightPosition - pixelPosition; float lightAmount = max(dot(normal, normalize(lightDir)), 0); return (tex * AmbientColor) + (color * lightAmount * LightColor * AmbientColor); } technique Deferred2DNormalMapping { pass Pass0 { PixelShader = compile ps_3_0 Deferred2DNormalMappingPS(); } } Thanks (Sorry but I'm from Spain and my english is quite bad...)

Share this post


Link to post
Share on other sites
The shader works well, but it does not work like I want. It works like a spot light always facing to the same position, the upper left corner of the screen. In the right side of the screen the only color I see is black. Here is a link to an image that shows what is happening:

http://img511.imageshack.us/my.php?image=testky0.jpg

Share this post


Link to post
Share on other sites
As you're new, lets make sure you know what the shader does!

1. It's meant as a post-processing pass for deferred shading, not during the rendering of a model.

2. It *is* doing a point light calculation, but, oddly, is completely ignoring Z. So it's like the light is an enormous tube going along viewspace Z. Probably meant for a 2D tile engine.

3. It's input is going to be a fullscreen quad, with texcoords going from 0 to 1. You'll need to set ScreenWidth and ScreenHeight to some value to represent your screen. These don't have to be pixels. You could use ScreenWidth and Height of 1 if you want.

4. The light position should be set to the screen-space position, not it's worldspace position. The light's position range should be the same as 0..ScreenWidth and 0..ScreenHeight.

Share this post


Link to post
Share on other sites

This topic is 3486 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.

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