简体   繁体   English

到世界位置的线性深度

[英]Linear Depth to World Position

I have the following fragment and vertex shaders. 我有以下片段和顶点着色器。

HLSL code ` // Vertex shader //----------------------------------------------------------------------------------- HLSL代码`//顶点着色器// ----------------------------------------- ------------------------------------------

void mainVP( 
   float4 position     : POSITION, 
   out float4 outPos   : POSITION, 
   out float2 outDepth : TEXCOORD0, 
   uniform float4x4 worldViewProj,
   uniform float4 texelOffsets,
   uniform float4 depthRange)   //Passed as float4(minDepth, maxDepth,depthRange,1 / depthRange)
{ 
    outPos = mul(worldViewProj, position);
    outPos.xy += texelOffsets.zw * outPos.w;
    outDepth.x = (outPos.z - depthRange.x)*depthRange.w;//value [0..1]
    outDepth.y = outPos.w; 
} 

// Fragment shader  
void mainFP( float2 depth: TEXCOORD0, out float4 result : COLOR) { 
    float finalDepth = depth.x;
    result = float4(finalDepth, finalDepth, finalDepth, 1);
}

` `

This shader produces a depth map. 该着色器生成深度图。

This depth map must then be used to reconstruct the world positions for the depth values. 然后必须使用该深度图来重建深度值的世界位置。 I have searched other posts but none of them seem to store the depth using the same formula I am using. 我搜索了其他帖子,但似乎都没有使用与我相同的公式来存储深度。 The only similar post is the following Reconstructing world position from linear depth 唯一类似的帖子是以下从线性深度重构世界位置

Therefore, I am having a hard time reconstructing the point using the x and y coordinates from the depth map and the corresponding depth. 因此,我很难使用深度图和相应深度的x和y坐标来重建点。

I need some help in constructing the shader to get the world view position for a depth at particular texture coordinates. 在构造着色器时需要一些帮助,以获取特定纹理坐标处的深度的世界视图位置。

It doesn't look like you're normalizing your depth. 看起来您不是在规范深度。 Try this instead. 试试这个吧。 In your VS, do: 在您的VS中,执行以下操作:

outDepth.xy = outPos.zw;

And in your PS to render the depth, you can do: 在您的PS中渲染深度,您可以执行以下操作:

float finalDepth = depth.x / depth.y;

Here is a function to then extract the view-space position of a particular pixel from your depth texture. 然后是一个从深度纹理中提取特定像素的视图空间位置的功能。 I'm assuming you're rendering screen aligned quad and performing your position-extraction in the pixel shader. 我假设您要渲染屏幕对齐的四边形并在像素着色器中执行位置提取。

// Function for converting depth to view-space position
// in deferred pixel shader pass.  vTexCoord is a texture
// coordinate for a full-screen quad, such that x=0 is the
// left of the screen, and y=0 is the top of the screen.
float3 VSPositionFromDepth(float2 vTexCoord)
{
    // Get the depth value for this pixel
    float z = tex2D(DepthSampler, vTexCoord);  
    // Get x/w and y/w from the viewport position
    float x = vTexCoord.x * 2 - 1;
    float y = (1 - vTexCoord.y) * 2 - 1;
    float4 vProjectedPos = float4(x, y, z, 1.0f);
    // Transform by the inverse projection matrix
    float4 vPositionVS = mul(vProjectedPos, g_matInvProjection);  
    // Divide by w to get the view-space position
    return vPositionVS.xyz / vPositionVS.w;  
}

For a more advanced approach that reduces the number of calculation involved but involves using the view frustum and a special way of rendering the screen-aligned quad, see here . 要获得一种更高级的方法,该方法可以减少涉及的计算数量,但需要使用视锥台视图和呈现与屏幕对齐的四边形的特殊方法,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM