Hi,
I wanted to control a property in pixel shader based on world position passed from vertex shader and got stuck there. For the sake of simplicity, let's say I want to draw stripes on the object, when the view changes they'd stay where they were on the object (as if planar mapped to the object), when the object moves, they'd stay on their original coords, moving across its surface.
Now, here's what I tried:
float4x4 WorldITXf : WorldInverseTranspose;
float4x4 WorldViewProjXf : WorldViewProjection;
float4x4 WorldXf : World;
float4x4 ViewIXf : ViewInverse;
void mainVS(inout float4 Position : POSITION,
out float3 WorldPos : TEXCOORD0)
{
Position = mul(Position, WorldViewProjXf);
WorldPos = mul(Position, WorldXf).xyz;
}
float4 mainPS(in float3 WorldPos : TEXCOORD0) : COLOR {
float strokeS = frac(WorldPos.x / 5);
return (strokeS > 0.5) ? float4(1,1,1,1) : float4(0,0,0,0);
}
technique Default {
pass p0 {
VertexShader = compile vs_3_0 mainVS();
PixelShader = compile ps_3_0 mainPS();
}
}
I'd expect WorldPos to contain absolute world coords so the pixel shader value on the same world x-coord would always be the same. However, when I rotate the view, these values change, leading me to believe I got something wrong in the vertex shader. I'm completely new to shaders, tried searching for a solution both in literature and online, yet it seems this is the right way to do it. I would be very grateful if anyone could point me in the right direction.