Here's the code for light calculation and reconstruction of position from depth:
texture normal;
texture depth;
texture ambient;
float3 EyeVec;
float3 lightPos;
float4x4 InvertedProjectionMat;
sampler normSamp : register (s1) = sampler_state
{
texture = <normal>;
};
sampler depSamp : register (s2) = sampler_state
{
texture = <depth>;
};
sampler ambSamp : register (s3) = sampler_state
{
texture = <ambient>;
};
float3 getPosFromDepth(float2 texCoord)
{
float z = length(tex2D(depSamp, texCoord).rgb);
float x = texCoord.x * 2 - 1;
float y = (1 - texCoord.y) * 2 - 1;
float4 vProjectedPos = float4(x, y, z, 1.0f);
float4 vPositionVS = mul(vProjectedPos, InvertedProjectionMat);
return vPositionVS.xyz / tex2D(depSamp, texCoord).w;
}
struct PixelShaderInput
{
float2 texCoord : TEXCOORD0;
};
float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
float4 Color;
Color = tex2D(ambSamp, input.texCoord);
if(length(Color) == 0)
return float4(0.2f, 0.4f, 0.9f, 1.0f);;
float4 SpecularColor = float4(1.0f, 1.0f, 0.0f, 1.0f);
float specularLVL;
float3 Position = getPosFromDepth( input.texCoord);
float3 Normal = normalize(tex2D(normSamp, input.texCoord)).rgb;
float3 LightVector = normalize( lightPos.xyz - Position.xyz );
float NdL = dot(Normal, LightVector);
float3 Eye = normalize(EyeVec );
float3 halfVec = reflect(LightVector, Normal);
specularLVL = pow(dot(halfVec, Eye), 100);
return Color * NdL;// + (0.7f * specularLVL * SpecularColor);
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
Some mess inside the code is caused by me loosing my mind and start typing random stuff Also as I am posting something on forums (which I try to do only in an act of desperation
Anyway I will appreciate any help and thank You in advice






