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?
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!