Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save galek/056ed6b19e5495199e88fd39e4b20e8b to your computer and use it in GitHub Desktop.
Save galek/056ed6b19e5495199e88fd39e4b20e8b to your computer and use it in GitHub Desktop.
Reconstructing Position From Depth Buffer
float3 vFrustumCornersWS[8] =
{
float3(-1.0, 1.0, 0.0), // near top left
float3( 1.0, 1.0, 0.0), // near top right
float3(-1.0, -1.0, 0.0), // near bottom left
float3( 1.0, -1.0, 0.0), // near bottom right
float3(-1.0, 1.0, 1.0), // far top left
float3( 1.0, 1.0, 1.0), // far top right
float3(-1.0, -1.0, 1.0), // far bottom left
float3( 1.0, -1.0, 1.0) // far bottom right
};
for(int i = 0; i < 8; ++i)
{
float4 vCornerWS = mul(mViewProjInv, float4(vFrustumCornersWS[i].xyz, 1.0));
vFrustumCornersWS[i].xyz = vCornerWS.xyz / vCornerWS.w;
}
for(int i = 0; i < 4; ++i)
{
vFrustumCornersWS[i + 4].xyz -= vFrustumCornersWS[i].xyz;
}
// Passes to the pixel shader - there are two ways to do this:
// 1. Pass the corner with the vertex of your quad as part of the input stage.
// 2. If you're quad is created dynamically with SV_VertexID, as in this example, just pick one from a constant array based on the position.
float3 vCamVecLerpT = (OUT.vPosition.x>0) ? vFrustumCornersWS[5].xyz : vFrustumCornersWS[4].xyz;
float3 vCamVecLerpB = (OUT.vPosition.x>0) ? vFrustumCornersWS[7].xyz : vFrustumCornersWS[6].xyz;
///PIXEL:
float3 vWorldPos = fLinearDepth * IN.vCamVec.xyz + vCameraPos;
From:
http://www.gamedev.net/topic/659279-reconstructing-position-from-depth-buffer/#entry5170000
OUT.vCamVec.xyz = (OUT.vPosition.y<0) ? vCamVecLerpB.xyz : vCamVecLerpT.xyz;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment