Calculating world space coordinates in the pixel shader

孤人 提交于 2020-03-21 19:25:22

问题


I have a pixel shader and I want to calculate the position of each pixel in terms of my world space coordinates. How would I do this? What would I need?

I have a ps_input structure which has a float4 position : SV_POSITION. I'm assuming this is important, but the values stored inside seems to be kind of funny.

I can't seem to figure out what they relate to. For instance, if a pixel is 2d, how come it has a w component, or a z component for that matter?

I'm using DirectX and the pixel shader is in HLSL. C++ as my programing language on the CPU.

Any help would be appreciated.


回答1:


If i remember this correctly, SV_POSITION is only 0->W 0->H, so it describes where it is on the screen. this site contains more information : http://msdn.microsoft.com/en-us/library/windows/desktop/bb509647(v=vs.85).aspx

To get the pixel to world space, you transform your pixelcordinate back to worlspace. it´s a bit tricky with the math and all ( i think it is! ) but this article should help you out! http://jcoluna.wordpress.com/2011/01/27/reconstructing-view-space-position-from-depth/

else wise search for pixel to viewspace. i know you wanted worldspace, but converting everything to viewspace is probably faster.



来源:https://stackoverflow.com/questions/12794428/calculating-world-space-coordinates-in-the-pixel-shader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!