So I'm having this problem with my shadow mapping where the shadows only appear if the light is super close to the objects. Here is some pictures along with my shader code. Does anyone know how to fix this?
Far: https://gyazo.com/1222f5e38b38e0da0f36d85ab32e9458
Near: https://gyazo.com/339409593fbd1bbb05fd4790a11291f0
Besides for the fact that even in the near image it is completely off.
[spoiler]float4x4 World;
float4x4 View;
float4x4 Projection;
float4x4 LightWVP;
float3 LightPos;
texture ColorMap;
sampler ColorSampler = sampler_state
{
Texture = ColorMap;
AddressU = Mirror;
AddressV = Clamp;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
};
texture ShadowMap;
sampler ShadowSampler = sampler_state
{
Texture = ShadowMap;
AddressU = Mirror;
AddressV = Mirror;
MinFilter = Point;
MipFilter = Point;
MagFilter = Point;
};
texture PositionMap;
sampler PositionSampler = sampler_state
{
Texture = PositionMap;
AddressU = Clamp;
AddressV = Clamp;
MinFilter = Point;
MipFilter = Point;
MagFilter = Point;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL;
float2 TexCoords : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoords : TEXCOORD0;
float4 Pos2D : TEXCOORD1;
float4 Pos3D : TEXCOORD2;
float3 Normal : TEXCOORD3;
};
float DotProduct(float3 lightPos, float3 pos3D, float3 normal)
{
float3 lightDir = normalize(pos3D - lightPos);
return dot(-lightDir, normal);
}
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.Pos2D = mul(input.Position, mul(World, LightWVP));
output.Normal = normalize(mul(input.Normal, (float3x3)World));
output.Pos3D = mul(input.Position, World);
output.TexCoords = input.TexCoords;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float2 PCoords;
PCoords[0] = (input.Pos2D.x / input.Pos2D.w) / 2.0 + 0.5;
PCoords[1] = (-input.Pos2D.y / input.Pos2D.w) / 2.0 + 0.5;
float Factor = 0;
if ((saturate(PCoords).x == PCoords.x) && (saturate(PCoords).y == PCoords.y))
{
float Depth = tex2D(ShadowSampler, PCoords).r;
float Distance = input.Pos2D.z / input.Pos2D.w;
if ((Distance - 1.0 / 100.0) <= Depth)
{
Factor = DotProduct(LightPos, input.Pos3D, input.Normal);
Factor = saturate(Factor);
}
}
return Factor;
}
technique Technique1
{
pass Pass1
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PixelShaderFunction();
}
}
[/spoiler]