0

I have searched a lot on Google and most of examples achieving gradient using texture coordinates. But I don't have texture coordinates with me. I am working on 3D text on which I want to apply gradient color. Is it possible? If yes, how? Is it necessary to have texture coordinates for obtaining color gradient?

Following is the part of my hlsl shader file :

struct VS_INPUT
{
    float3 Pos : POSITION;
    float3 Norm : NORMAL;
};

struct PS_INPUT
{
    float4 Pos : SV_POSITION;
    float3 WorldNorm : TEXCOORD0;
    float3 CameraPos : TEXCOORD1;
    float3 WorldPos : TEXCOORD2;
};


//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------

PS_INPUT VS( VS_INPUT input )
{
    PS_INPUT output = (PS_INPUT)0;
    float4 worldPos = mul( float4(input.Pos,1), World );
    float4 cameraPos = mul( worldPos, View );

    output.WorldPos = worldPos;
    output.WorldNorm = normalize(mul( input.Norm, (float3x3)World ));
    output.CameraPos = cameraPos;
    output.Pos = mul( cameraPos, Projection );

    return output;
}

//--------------------------------------------------------------------------------------
// Pixel Shader Without Light
//--------------------------------------------------------------------------------------

float4 PS( PS_INPUT input) : SV_Target
{    
    float4 finalColor = {1.0f, 0.0f, 0.0f, 1.0f};

    return finalColor;
}


//--------------------------------------------------------------------------------------
technique10 Render
{
    pass P0
    {
        SetVertexShader( CompileShader( vs_4_0_level_9_1, VS() ) );
        SetGeometryShader( NULL );
        SetPixelShader( CompileShader( ps_4_0_level_9_1, PS() ) );
    }
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59

1 Answers1

0

You don't need texture coordinates, because they are only one possible method (the most flexible) to save needed information about how your gradient should look like, as such origin, direction and length.

To have a gradient from left to right of your 3D-Text, you need to know where is left and right in your shader to take the appropriate color. I assume that your text is changing and such dynamically, so you need to transport this information into the shader, which either can be placed into the vertices directly by texture coordinates or with a constant buffer. Last method would only work if you draw at most one text per drawcall, because the gradient data is persistent over the whole drawing of all triangles in your drawcall.

If your situation is more special, as like your text is axis-aligned, you could take this axis and the worldposition in your pixelshader to determine the relative position for your gradient, but this method makes many assumptions as you still need the left and right maximum of your text.

Gnietschow
  • 3,070
  • 1
  • 18
  • 28