I was looking for a simple way to do circle gradients, and I saw a few discussions on here but all of them were not solved or the solutions weren’t great. Here’s how I solved it: rather than making a mesh with 360 vertices, I just did a standard 6 vertice mesh, and to make it a circle, I set the texture to a plain white ellipse.
--# Main
-- CircleGradient
function setup()
grad1 = CircleGradient(WIDTH / 3, color(255, 0, 0), color(0, 0, 255))
grad2 = CircleGradient(WIDTH / 3, color(0, 0, 255), color(0, 255, 0))
end
function draw()
background(255)
pushMatrix()
translate(WIDTH / 4, HEIGHT / 4)
grad1:draw()
popMatrix()
pushMatrix()
translate(WIDTH * 3/4, HEIGHT * 3/4)
grad2:draw()
popMatrix()
end
--# CircleGradient
CircleGradient = class()
-- Size is diameter of circle, bcol is color on bottom, tcol is color on top
function CircleGradient:init(size, bcol, tcol)
local rad = size / 2
-- Create blank circle texture
local texture = image(size, size) setContext(texture) fill(255) noStroke() ellipse(rad, rad, size) setContext()
-- Setup mesh
self.mesh = mesh()
self.mesh.texture = texture
self.mesh.vertices = { vec2(-rad, -rad), vec2(rad, -rad), vec2(rad, rad), vec2(rad, rad), vec2(-rad, rad), vec2(-rad, -rad) }
self.mesh.texCoords = { vec2(0, 0), vec2(1, 0), vec2(1,1), vec2(1,1), vec2(0, 1), vec2(0,0) }
self.mesh.colors = {bcol, bcol, tcol, tcol, tcol, bcol }
end
function CircleGradient:setColors(bcol, tcol)
self.mesh.colors = {bcol, bcol, tcol, tcol, tcol, bcol }
end
function CircleGradient:draw()
self.mesh:draw()
end