Today I saw this post from 3 months ago by @xThomas. He was asking how to make a rainbow circle shader, and I wanted to give it a shot. I wasn’t sure if I should comment on his old post or if I should make a new one. The shader is free for use and easy to use. Here is an example project using the shader.
--# Main
function setup()
width,height = HEIGHT,HEIGHT
circleM = mesh()
circleM:addRect(WIDTH/2,HEIGHT/2,width,height) -- add a square to the mesh
circleM.shader = shader(RBCircle.vertexShader,RBCircle.fragmentShader)
end
function draw()
background(0)
circleM:draw()
end
function touched(t)
if t.state ~= ENDED then
width,height = width + t.deltaX*2, height + t.deltaY*2
circleM:setRect(1,WIDTH/2,HEIGHT/2,width,height)
end
end
RBCircle = {
vertexShader = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
vColor = color;
vTexCoord = texCoord;
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
precision highp float;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
vec4 mixCol(vec4 C1, vec4 C2, float i) //To make the mixing easier, We'll have this function
{ // C1 is the 1st colour, C2 is the 2nd colour, i is a float for interpolation
return C1 * (1.0 - i) + C2 * i;
}
void main()
{
highp vec4 col;
lowp float dis = distance(vTexCoord,vec2(0.5,0.5)); //get the distance from the texCoord to the center
if (dis < 0.5) { //0.5 is the size of the circle. It's set to take up the whole rectangle
//To make a gradient that transitions through each colour of the rainbow, we'll actually need to make
//5 gradients:
//red -> yellow, yellow -> green, green -> turqoise, turquoise -> blue, blue -> purple
if (vTexCoord.y < 0.2) col = mixCol(vec4(1,0,0,1),vec4(1,1,0,1),vTexCoord.y/0.2); //red to yellow
else if (vTexCoord.y < 0.4) col = mixCol(vec4(1,1,0,1),vec4(0,1,0,1),(vTexCoord.y - 0.2)/0.2);//yel to gre
else if (vTexCoord.y < 0.6) col = mixCol(vec4(0,1,0,1),vec4(0,1,1,1),(vTexCoord.y - 0.4)/0.2);//gre to tur
else if (vTexCoord.y < 0.8) col = mixCol(vec4(0,1,1,1),vec4(0,0,1,1),(vTexCoord.y - 0.6)/0.2);//tur to blu
else col = mixCol(vec4(0,0,1,1),vec4(1,0,1,1),(vTexCoord.y - 0.8)/0.2); // blue to purple
}
else {col = vec4(0,0,0,0);}
gl_FragColor = col;
}
]]
}