2017-02-14 11 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;

다음의보기 공간 위치를 추출하는 기능입니다 깊이 텍스처의 특정 픽셀 저는 스크린 정렬 쿼드를 렌더링하고 픽셀 쉐이더에서 위치 추출을 수행하고 있다고 가정합니다. 관련 계산의 수를 줄일 수 있지만, view frustum 화면 정렬 쿼드 렌더링의 특별한 방법을 사용하는 것보다 진보 된 접근 방식에 대한

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

, here를 참조하십시오.

+0

귀하의 솔루션은 선형적인 깊이가 아닌 것 같습니다. 내가 말하는 방식대로 작동하는 셰이더가 있지만 선형 깊이로 작업하는 셰이더가 필요합니다. – nickygs