jmasterx
jmasterx

Reputation: 54123

Getting the color of a vertex in HLSL?

I have the following vertex and pixel shaders:

    struct VS_INPUT
   {
      float4 Position  : POSITION0;
      float2 TexCoord  : TEXCOORD0;
      float4 Color     : TEXCOORD1;
   };
   struct VS_OUTPUT
   {
      float4 Position  : POSITION0;
      float4 Color     : COLOR0;
      float2 TexCoord  : TEXCOORD0;
   };

   float4x4 projview_matrix;

   VS_OUTPUT vs_main(VS_INPUT Input)
   {
      VS_OUTPUT Output;
      Output.Position = mul(Input.Position, projview_matrix);
      Output.Color = Input.Color;
      Output.TexCoord = Input.TexCoord;
      return Output;
   }

px

   texture tex;
   sampler2D s = sampler_state {
      texture = <tex>;
   };
   float4 ps_main(VS_OUTPUT Input) : COLOR0
   {
      float4 pixel = tex2D(s, Input.TexCoord.xy);
      return pixel;
   }

This is for a 2d game. The vertices of the quads contain tinting colors that I want to use to tint the bitmap. How can I obtain the color of the current vertex so I can multiply it in the pixel shader by the current pixel color?

Thanks

Upvotes: 0

Views: 4826

Answers (1)

Caleb Joseph
Caleb Joseph

Reputation: 438

In your pixel shader, do:

float4 pixel = tex2D(s, Input.TexCoord.xy) * Input.Color;

The Input.Color value will be linearly interpreted across your plane for you, just like Input.TexCoord is. To blend two color vectors together, you simply multiply them together. It may also be advisable to do:

float4 pixel = tex2D(s, Input.TexCoord.xy) * Input.Color;
pixel = saturate(pixel);

The saturate() function will clip each RGB value in your color in the range of 0.0 to 1.0, which may avoid any possible display artifacts.

Upvotes: 0

Related Questions