0
votes

Trying to colour vertices using their Y position for terrain but I get a really weird result.

struct InputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float3 normal : NORMAL;
    float4 sunlightPos : TEXCOORD1;
};
float4 main(InputType input) : SV_TARGET
{
    float4 textureColour = float4(0.0, 0.0, 0.0, 1.0);

    if (input.position.y > 0.0f) //r
    {
        textureColour.r = 1;
        textureColour.g = 0;
        textureColour.b = 0;
    }
    if (input.position.y > 20.f) //g
    {
        textureColour.r = 0;
        textureColour.g = 1;
        textureColour.b = 0;
    }
    if (input.position.y > 40.f) //b
    {
        textureColour.r = 0;
        textureColour.g = 0;
        textureColour.b = 1;
    }
    return textureColour;
}

But in the result all vertices are blue, and there are two stripes of red and green that follow my camera in the same place relative to my screen within the terrain, and as I change the angle different vertices are changing their colour, but always in the same location relative to the screen. This is not at all how I understand shaders should work. Is there something obvious I'm doing wrong?

View of the terrain from the top (perspective)

I used to apply a height map to a flat plane, I'd colour vertices the same way using the colour value of the texture, and change the height in the vertex shader using the colour value of the texture, so the position output in the vertex shader should be correct.

1

1 Answers

0
votes

When used in a shader, SV_Position describes the pixel location. Available in all shaders to get the pixel center with a 0.5 offset.

SV_POSITION is the pixel position in screen space(for pixel shaders), more info on that in the answer to this question.

Afaik there's no pixel shader semantic for the vertex position so you have to manually pass it through from your vertex shader by removing the semantic identifier and writing it from within your vertex shader.