【问题标题】:Linear Depth to World Position世界位置的线性深度
【发布时间】:2017-07-02 03:03:10
【问题描述】:

我有以下片段和顶点着色器。

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);
}

`

此着色器生成深度图。

然后必须使用此深度图来重建深度值的世界位置。我搜索了其他帖子,但似乎没有一个帖子使用我使用的相同公式来存储深度。唯一类似的帖子如下 Reconstructing world position from linear depth

因此,我很难使用深度图中的 x 和 y 坐标以及相应的深度来重建该点。

我需要一些帮助来构建着色器以获取特定纹理坐标处深度的世界视图位置。

【问题讨论】:

    标签: directx shader hlsl ogre3d


    【解决方案1】:

    看起来你并没有使你的深度正常化。试试这个。在你的 VS 中,做:

    outDepth.xy = outPos.zw;

    而在你的PS中渲染深度,你可以这样做:

    float finalDepth = depth.x / depth.y;

    这是一个从深度纹理中提取特定像素的视图空间位置的函数。我假设您正在渲染屏幕对齐的四边形并在像素着色器中执行位置提取。

    // 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;  
    }
    

    有关减少所涉及的计算数量但涉及使用view frustum 和渲染屏幕对齐四边形的特殊方法的更高级方法,请参阅here

    【讨论】:

    • 您的解决方案似乎不适用于线性深度。我有一个着色器按照你说的方式工作,但我需要另一个着色器处理线性深度。
    猜你喜欢
    • 2015-03-19
    • 2013-04-21
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2014-12-28
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    相关资源
    最近更新 更多