- Home /
shader: gradient with three colours
Hello,
Using shaders in Unity I want to achieve something like this:
The colour transition between the colours should be smooth.
Does somebody has any idea?
Thanks in advance!
three-colour-gradient.jpg
(31.0 kB)
Comment
Best Answer
Answer by PhilippL · Sep 22, 2015 at 09:04 AM
I solved this problem with the specific colors: color1 = grey, color2 = black and color3 = white. I wrote this vertex shader and attached this to the corresponding mesh.
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord0 : TEXCOORD0;
};
struct fragmentInput{
float4 position : SV_POSITION;
float4 texcoord0 : TEXCOORD0;
};
fragmentInput vert(vertexInput i){
fragmentInput o;
o.position = mul (UNITY_MATRIX_MVP, i.vertex);
o.texcoord0 = i.texcoord0;
return o;
}
fixed4 frag(fragmentInput i) : SV_Target {
float x = i.texcoord0.x;
float y = i.texcoord0.y;
float value = 1-(0.5*(1-x)+x*y); // function for gradient behaviour
return fixed4(value,value, value, 1-value);
}
The key is the function f(x, y)=1-(0.5*(1-x)+x*y) which outputs the variable value. This function handles the continuous color transition of the corresponding colors. I think this code can be easily customized to work with other colors or another contrast of the colors.
I hope this answer will help you.
Your answer
![](https://koobas.hobune.stream/wayback/20220612033328im_/https://answers.unity.com/themes/thub/images/avi.jpg)