Reputation: 33
How can I draw a gradient rect (a color hue spectrum actually) like this:
I thought of drawing it pixel by pixel but it takes a lot of time (memory). I thought of drawing 4 different gradient rects with vertex buffers, and it should be good, but is there another way to do that?
Upvotes: 1
Views: 2336
Reputation: 13003
Such color effects is very easy to implement in pixel shader procedurally.
Here is a code from my GLSL shader, adopted for HLSL (renamed vec3 to float3 and clamp to saturate etc.). Note, that it has not been tested as HLSL.
struct PSInput
{
float2 texcoord;
};
float3 HueToRGB(in float h)
{
float3 rgb = 0.0f;
rgb.r = abs(h * 6.0f - 3.0f) - 1.0f;
rgb.g = 2.0f - abs(h * 6.0f - 2.0f);
rgb.b = 2.0f - abs(h * 6.0f - 4.0f);
rgb = saturate(rgb);
return rgb;
}
void main() : SV_Target
{
float3 colorRGB = HueToRGB(in.texcoord.x);
return float4(colorRGB, 1.0f);
}
For more control over colors, you can:
Happy coding!
Upvotes: 0
Reputation: 4291
For every distinct color add a a vertex pair containing the color as vertex data. In the vertex shader, forward that color to the pixel shader, and let the pixel shader simply output it. As the attribute will be interpolated from the vertex shader -> pixel shader you'll get the gradient for free.
Upvotes: 1