2

what I'm searching for is a simple way to get the world space coordinates for each pixel running throu the pixel shader.

I've found this topic: Pixel World Position

and it seems to work, but I'm yet not that used to shader language that I completly understand it.

Isn't there an easy way to solve this?

Kind regards, Marius

Community
  • 1
  • 1
marius
  • 1,118
  • 1
  • 18
  • 38

1 Answers1

3

Easiest way is to pass it from the vertex shader as texture coordinate, like this (dx9 sample easy to convert to 10 if you use it):

cbuffer cbmat : register( b0 )
{
    float4x4 tW; //World transform
    float4x4 tWVP: //World * View * Projection
};



struct vs2ps
{
    float4 Pos : POSITION;
    float4 TexCd : TEXCOORD0;
    float3 PosW : TEXCOORD1;
};

vs2ps VS(float4 Pos : POSITION,float4 TexCd : TEXCOORD0)
{
    vs2ps Out;
    Out.Pos = mul(Pos, tWVP);
    Out.TexCd = TexCd;
    Out.PosW = mul(Pos, tW);
    return Out;
}

float4 PS(vs2ps In): COLOR
{
    return float4(In.PosW,1.0f);
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
mrvux
  • 8,523
  • 1
  • 27
  • 61
  • Can't get this to running. float4x4 tVP: //World * View * Projection should actually be tWVP as you only use this lower. and Out.TexCd = mul(TexCd, tTex); tTex is not defined. Anyway thx. – marius Nov 12 '12 at 13:48
  • @marius updated answer to remove the tTex, and changes tVP by tWVP. now should work. – mrvux Nov 12 '12 at 18:05
  • Thx works, but is this really the world position for each pixel? I think it's the position of the vertex right? Outputting it on the pixelshader has no debugging-value ^^. Btw I use SM2.0 / DX9 and thx for quick response! – marius Nov 13 '12 at 12:33
  • 1
    World position will be interpolated across the triangle between vertex shader/pixel shader, so it will be pixel position when in pixel shader. – mrvux Nov 13 '12 at 17:03
  • Are you sure? Gonna test it asap, but that sounds to easy. Thx for helping out =) – marius Nov 14 '12 at 08:02
  • Thank you very much for this! For those in the future who want to understand this code, you essentially just pass the vertex position after being rotated and scaled (but before multiplying it by the Orientation (World * View * Projection) matrix) to the pixel shader and let D3D automatically interpolate it per pixel for you. In the pixel shader, then, In.PosW is the pixel's position in the world. – Andrew Jul 28 '16 at 02:43