2017-02-14 71 views
0

我有以下片段和顶点着色器。线性深度到世界位置

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坐标重建点。

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

回答

0

它看起来不像你正在规范你的深度。试试这个。在你的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

+0

看来你的解决方案不是为了线性深度。我有一个着色器按照你所说的方式工作,但我需要有另一个着色器以线性深度工作。 – nickygs