yeah Hornsj3 ok what is the point of using perspective divide as it's always 1 ??
here is the code :
float4x4 WorldViewProjection;
float4x4 InViewProjection;
texture2D DepthTexture;
texture2D NormalTexture;
sampler2D depthSampler = sampler_state
{
texture = <DepthTexture>;
minfilter=point;
magfilter=point;
mipfilter=point;
};
sampler2D normalSampler = sampler_state
{
texture = <DepthTexture>;
minfilter=point;
magfilter=point;
mipfilter=point;
};
float3 LightColor;
float3 LightPosition;
float3 LightAttenuation;
// Include shared funtions
#include "PPShared.vsi"
struct VertexShaderInput
{
float4 Position : POSITION0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 LightPosition :TEXCOORD0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = mul(input.Position, WorldViewProjection);
output.LightPosition=output.Position;
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
// Find the Pixel Coordinates of the input position in Depth,Normal Textures
float2 texCoord = postProjectionToScreen(input.LightPosition) + halfPixel();
// extract the depth for this pixel from depth map that I created before
float4 depth = tex2D(depthSampler,texCoord);
// Recreate the position with the UV coordinates and depth value
float4 position;
position.x = texCoord.x * 2 -1;
position.y = (1- texCoord.y) * 2 - 1;
position.z = depth.r;
position.w = 1.0f;
// Transform position from screen space to world space
position = mul(position,InViewProjection);
position.xyz /= position.w;
// Extract the normal from normal map and move from
// 0 to 1 range to -1 to 1 range
float4 normal=(tex2D(normalSampler, texCoord)-0.5) * 2;
// Perform the lighting calculations for a point light
float3 lightDirection = normalize(LightPosition - position);
float lighting = clamp(dot(normal,lightDirection) , 0 , 1);
// Attenuate the light to simulate a point light
float d = distance(LightPosition,position);
float att = 1-pow(d / LightAttenuation , 6);
float3 output=LightColor * lighting * att ;
return (output,1 );
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
I had previously store the depth and normal values in special textures and use them in this effect and i use shared functions to map 3d to 2d for extracting values from DepthTexture and NormalTextures .
here is the effect file that used to store depth and normal :
// TODO: add effect parameters here.
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Nomral : NORMAL0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Depth : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4x4 viewProjection=mul(View,Projection);
float4x4 worldViewProjection=mul(World,viewProjection);
output.Position=mul(input.Position,worldViewProjection);
output.Normal=mul(input.Nomral,World);
//Position's z and w components corresponds to the distance
//from camera and distance of the far plane respectivily
output.Depth.xy=output.Position.zw;
return output;
}
// I render to render targets simultaneously,so I can't
// return a float4 from PixelShaderFunction
struct PixelShaderOutput
{
float4 Normal:COLOR0;
float4 Depth :COLOR1;
};
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input)
{
PixelShaderOutput output;
// Depth is stored as distance from camera / far plane distance
// to get value between 1 and 0
output.Depth=input.Depth.x/input.Depth.y;
// Normal map simply stores x, y and z components of normals
// shifted from -1 to 1 range to 0-1 range
output.Normal.xyz=(normalize(input.Normal).xyz/2)+ .5;
// other components must be intialized to compile
output.Depth.a=1;
output.Normal.a=1;
return output;
}
technique Technique1
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_1_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
I Know what is done in both files but I don't understand the following in the second file code:
Edited by mrmohadnan, 11 July 2012 - 04:48 AM.