I have the following code for my HLSL pixel shader. Modified from another post here on GameDev (Link), but I have a few problems with it:
// calculate UV and get texture and normal.
float2 UV = Input.position.xy / Input.ScreenSize.xy;
float4 DiffuseColor = ColorTexture.Sample( SampleType, UV );
float4 NormalColor = NormalTexture.Sample( SampleType, UV );
float4 normal = 2.0f * NormalColor - 1.0f;
// calculate distance.
float3 LightDir = LightPos - Input.position.xyz;
float distance = 1 - length( LightDir ) / LightRadius;
// calculate dot of normal and light direction.
float NdL = max( 0, dot( normal.xyz, LightDir ) );
// get final color.
float4 finalColor = ( DiffuseColor * ambientIntensity ) + DiffuseColor * distance * LightIntensity * LightColor * NdL;
return float4( finalColor.rgb, DiffuseColor.a );
LightIntensity are currently 1.0f to simplify things.
LightRadius depends on the size of the geometry, but is in pixels. ~80 for testing.
DiffuseColor is a texture resulting from applying self-illumination and darkening the base image.
NormalColor is the normal map for the image.
This shader needs to run for every light on the screen, which isn't that many.
For my tests I use the images from the blog post here. It's a little old, but it is pretty much what I need to do, which I can't.
Now; I did enough testing to know that all input and cbuffer parameters are valid but the results I get are not what I hoped for.
Here are my problems:
Problem 1: My knowledge of math is limited and I don't understand exactly what the dot product does, why it is needed in the light calculation or why the distance is calculated as it is. Is there a resource online that would explain lighting in term of HLSL or at least in a way meant for a programmer and not using big equations meant for a mathematician? If I don't understand the basics, I'm afraid I wont be able to improve and I'll be stuck every time I need to add new features (ie: shadows), but I need to get this things running asap.
Problem 2: The results I have are not good. When I move the light around, I can actually see where the geometry cuts off, and it is as if the light does not trail off properly near the edges of the geometry. Are the calculations wrong?
Problem 3: I built a 2D shape editor that I use to design the shape of the lighting geometry, but I don't know how to edit the shaders or the geometry's UV to have the custom shapes (fans, circles, etc) shaded properly. The light I render is always shaded as if it was a circle.
I spent the last two weeks working on this and whenever I search on Google, I keep getting pages I have already visited, so I'm quite lost right now. Any suggestions would be highly appreciated.
My game is a tile-based 2D game, thought I would mention that.