问题
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