# calculate eye-space position from depth

This topic is 3105 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't understand the following function:

// calculate eye-space position from depth
float3 posEye = uvToEye(texCoord, depth);
// calculate differences
float3 ddx = getEyePos(depthTex, texCoord + float2(texelSize, 0)) -posEye;
float3 ddx2 = posEye -getEyePos(depthTex, texCoord + vec2(-texelSize, 0));
if (abs(ddx.z) > abs(ddx2.z)) {
ddx = ddx2;
}

how to get eye-space position from depth?

##### Share on other sites
Are you asking how uvToEye works in that snippet? Assuming texCoord is the screen-space coordinate of the current pixel, then the function could work like this:

// fetch depth value for current pixel
float depth = tex2D( depthTex, texCoord ).r;

// convert texture coordinate to homogeneous space
float2 xyPos = texCoord * 2.0 - 1.0;

// construct clip-space position
float4 clipPos = float4( xyPos, depth, 1.0 );

// transform from clip space to view (eye) space
// NOTE: this assumes that you've precomputed the
// inverse of the view->clip transform matrix and
// provided it to the shader as a constant.
float4 viewPos = mul( clipPos, c_invViewClipTransform );

// Make the position homogeneous and return
return viewPos.xyz / viewPos.w;

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015493
×