Screen show results:
This shadow is very bad ? How too implemant blur shadow mapps ? Please help me. THX for all.
This is code to HLSL VS/PS :
struct SMapVertexToPixel
{
float4 Position : POSITION;
float3 Position2D : TEXCOORD0;
};
struct SMapPixelToFrame
{
float4 Color : COLOR0;
float Depth : DEPTH;
};
struct SSceneVertexToPixel
{
float4 Position : POSITION;
float4 ShadowMapSamplingPos : TEXCOORD0;
float4 RealDistance : TEXCOORD1;
float2 TexCoords : TEXCOORD2;
float3 Normal : TEXCOORD3;
float3 Position3D : TEXCOORD4;
};
struct SScenePixelToFrame
{
float4 Color : COLOR0;
};
struct AmbientVertexToPixel
{
float4 Position : POSITION;
float2 TexCoords : TEXCOORD0;
float4 Position2D : TEXCOORD1;
};
struct AmbientPixelToFrame
{
float4 Color : COLOR0;
};
//------- Constants --------
float4x4 xCameraViewProjection;
float4x4 xLightViewProjection[3];
float4x4 xRotate;
float4x4 xTranslateAndScale;
float4 xLightPos[3];
float4 xCameraPos;
float xLightPower[3];
float xMaxDepth;
float xAmbient;
//------- Texture Samplers --------
Texture xColoredTexture;
sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
Texture xShadowMap;
sampler ShadowMapSampler = sampler_state { texture = <xShadowMap> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = clamp; AddressV = clamp;};
Texture xCarLightTexture;
sampler CarLightSampler = sampler_state { texture = <xCarLightTexture> ; magfilter = LINEAR; minfilter=LINEAR; mipfilter = LINEAR; AddressU = clamp; AddressV = clamp;};
Texture xSpotLightTexture;
sampler SpotLightSampler = sampler_state { texture = <xSpotLightTexture> ; magfilter = LINEAR; minfilter=LINEAR; mipfilter = LINEAR; AddressU = clamp; AddressV = clamp;};
//------- Vertex Shaders --------
SMapVertexToPixel ShadowMapVertexShader( float4 inPos : POSITION, uniform int CurrentLight)
{
SMapVertexToPixel Output = (SMapVertexToPixel)0;
float4x4 preWorld = mul(xRotate, xTranslateAndScale);
float4x4 preLightWorldViewProjection = mul (preWorld, xLightViewProjection[CurrentLight]);
Output.Position = mul(inPos, preLightWorldViewProjection);
Output.Position2D = Output.Position;
return Output;
}
SSceneVertexToPixel ShadowedSceneVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0, float3 inNormal : NORMAL, uniform int CurrentLight)
{
SSceneVertexToPixel Output = (SSceneVertexToPixel)0;
float4x4 preWorld = mul(xRotate, xTranslateAndScale);
float4x4 preCameraWorldViewProjection = mul (preWorld, xCameraViewProjection);
float4x4 preLightWorldViewProjection = mul (preWorld, xLightViewProjection[CurrentLight]);
Output.Position = mul(inPos, preCameraWorldViewProjection);
Output.ShadowMapSamplingPos = mul(inPos, preLightWorldViewProjection);
Output.RealDistance = Output.ShadowMapSamplingPos.z/xMaxDepth;
Output.TexCoords = inTexCoords;
Output.Normal = mul(inNormal, xRotate);
Output.Position3D = inPos;
return Output;
}
AmbientVertexToPixel AmbientVertexShader( float4 inPos : POSITION, float4 inTexCoords : TEXCOORD0)
{
AmbientVertexToPixel Output = (AmbientVertexToPixel)0;
float4x4 preWorld = mul(xRotate, xTranslateAndScale);
float4x4 preCameraWorldViewProjection = mul (preWorld, xCameraViewProjection);
Output.Position = mul(inPos, preCameraWorldViewProjection);
Output.Position2D = Output.Position;
Output.TexCoords = inTexCoords;
return Output;
}
//------- Pixel Shaders --------
float DotProduct(float4 LightPos, float3 Pos3D, float3 Normal)
{
float3 LightDir = normalize(LightPos - Pos3D);
return dot(LightDir, Normal);
}
SMapPixelToFrame ShadowMapPixelShader(SMapVertexToPixel PSIn, uniform int CurrentLight)
{
SMapPixelToFrame Output = (SMapPixelToFrame)0;
Output.Color[CurrentLight] = PSIn.Position2D.z/xMaxDepth;
Output.Depth = Output.Color[CurrentLight]/3.0f + (2-CurrentLight)*0.33f;
return Output;
}
SScenePixelToFrame ShadowedScenePixelShader(SSceneVertexToPixel PSIn, uniform int CurrentLight)
{
SScenePixelToFrame Output = (SScenePixelToFrame)0;
float2 ProjectedTexCoords;
ProjectedTexCoords[0] = PSIn.ShadowMapSamplingPos.x/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f;
ProjectedTexCoords[1] = -PSIn.ShadowMapSamplingPos.y/PSIn.ShadowMapSamplingPos.w/2.0f +0.5f;
if ((saturate(ProjectedTexCoords.x) == ProjectedTexCoords.x) && (saturate(ProjectedTexCoords.y) == ProjectedTexCoords.y))
{
float StoredDepthInShadowMap = tex2D(ShadowMapSampler, ProjectedTexCoords)[CurrentLight];
if ((PSIn.RealDistance.x - 1.0f/100.0f) <= StoredDepthInShadowMap)
{
float LightTextureFactor;
if (CurrentLight == 0)
{
LightTextureFactor = tex2D(CarLightSampler, ProjectedTexCoords).r;
}else{
LightTextureFactor = tex2D(SpotLightSampler, ProjectedTexCoords).r;
}
float DiffuseLightingFactor = DotProduct(xLightPos[CurrentLight], PSIn.Position3D, PSIn.Normal);
float4 ColorComponent = tex2D(ColoredTextureSampler, PSIn.TexCoords);
Output.Color = ColorComponent*LightTextureFactor*DiffuseLightingFactor*xLightPower[CurrentLight];
}
}
return Output;
}
AmbientPixelToFrame AmbientPixelShader(AmbientVertexToPixel PSIn)
{
AmbientPixelToFrame Output = (AmbientPixelToFrame)0;
Output.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords)*xAmbient;
float2 ScreenPos;
ScreenPos[0] = PSIn.Position2D.x/PSIn.Position2D.w/2.0f +0.5f;
ScreenPos[1] = -PSIn.Position2D.y/PSIn.Position2D.w/2.0f +0.5f;
for (int CurrentLight=1; CurrentLight<3; CurrentLight++)
{
float4 Light3DPos = mul(xLightPos[CurrentLight],xCameraViewProjection);
float2 LightScreenPos;
LightScreenPos[0] = Light3DPos.x/Light3DPos.w/2.0f +0.5f;
LightScreenPos[1] = -Light3DPos.y/Light3DPos.w/2.0f +0.5f;
float dist = distance(ScreenPos, LightScreenPos);
float radius = 3.5f/distance(xCameraPos, xLightPos[CurrentLight]);
if (dist < radius)
{
Output.Color.rgb += (radius-dist)*8.0f;
}
}
return Output;
}
//------- Techniques --------
technique ShadowMap
{
pass Pass0
{
cullmode = ccw;
colorwriteenable = red;
VertexShader = compile vs_2_0 ShadowMapVertexShader(0);
PixelShader = compile ps_2_0 ShadowMapPixelShader(0);
}
pass Pass1
{
colorwriteenable = green;
VertexShader = compile vs_2_0 ShadowMapVertexShader(1);
PixelShader = compile ps_2_0 ShadowMapPixelShader(1);
}
pass Pass2
{
colorwriteenable = blue;
VertexShader = compile vs_2_0 ShadowMapVertexShader(2);
PixelShader = compile ps_2_0 ShadowMapPixelShader(2);
}
}
technique ShadowedScene
{
pass Pass0
{
VertexShader = compile vs_2_0 ShadowedSceneVertexShader(0);
PixelShader = compile ps_2_0 ShadowedScenePixelShader(0);
}
pass Pass1
{
SRCBLEND = ONE;
DESTBLEND = ONE;
ALPHABLENDENABLE = true;
VertexShader = compile vs_2_0 ShadowedSceneVertexShader(1);
PixelShader = compile ps_2_0 ShadowedScenePixelShader(1);
}
pass Pass2
{
VertexShader = compile vs_2_0 ShadowedSceneVertexShader(2);
PixelShader = compile ps_2_0 ShadowedScenePixelShader(2);
}
pass Pass3
{
VertexShader = compile vs_2_0 AmbientVertexShader();
PixelShader = compile ps_2_0 AmbientPixelShader();
}
}