从深度计算世界排名

时间:2019-07-14 08:47:04

标签: hlsl directx-12

我正在尝试根据深度值计算世界位置。我目前将所有像素位置存储在世界pos缓冲区中,并且正在使用它进行比较。一旦这项新技术生效,我将其从Bugger布局中删除并节省1/3的空间。

因此,如果我正确理解这一点,我的第一步就是获取深度值,并在GeometryPass中执行以下操作 VS:

PixelShaderInput output;

float4 pos = mul(float4(input.posL, 1.0f), model);
output.posW = pos; // currently used for my worldPossBuffer for comparison
// Transform the vertex position into view space.
pos = mul(pos, view);
// Transform the vertex position into projected space.
pos = mul(pos, projection);
output.posH = pos;
output.depth = pos.zw;

return output;

PS:

float depth = input.depth.x / input.depth.y;

然后将其输入到我的Light Pass中,仅出于调试目的,请执行以下操作

float z = gTangentMap.Sample(s1, input.tex).w ;
z = MapToRange(z, 0.99, 1.0f, 0.0f, 1.0f);
return float4(z, z, z,1.0f);

debugging depth values

所以现在这是我要根据像素着色器中的深度值计算世界位置的地方。我找到了本教程(https://mynameismjp.wordpress.com/2009/03/10/reconstructing-position-from-depth/),该教程显示了从深处获取VS位置,我希望可以做到这一点,然后将invViewMatrix应用于该位置,以回到世界位置。

LightPass PS:

float z = gTangentMap.Sample(s1, input.tex).w;
float x = input.tex.x * 2.0f - 1.0f;
float y = (1.0f - input.tex.y) * 2.0f - 1.0f;
float4 vProjectedPos = float4(x, y, z, 1.0f);
float4 vPositionVS = mul(vProjectedPos, invwProjection);
float3 finalPosVS = vPositionVS.xyz / vPositionVS.w;

float3 worldPos = mul(float4(finalPosVS, 1.0f), invView);

刚刚渲染的结果给了我以下内容 Result of failed attempt

左上角是我的渲染调试,显示了原始的世界pos缓冲区,这是我期望得到的结果

expected kind of result

关于任何错误的任何建议或暗示,将非常感谢,因为我想优化缓冲区,这是第一个主意。

谢谢

0 个答案:

没有答案