Made this cool effect for the start screen of a game I’m working on, liked it so much I thought id share it with you all
EDIT: NOW IN BEAUTIFUL TECHNICOLOR!!!
EDITEDIT:MANY Thanks to @Ignatz for the (grayscale) shader that makes the (uncolored version of the) finalized effect so glorious!
EDITEDITEDIT: Now in a class so it can be used elsewhere, also now without a lot of the bulk it had earlier! Plus you can now switch between grayscale and color in the overlay!
--# Main
displayMode(OVERLAY)
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_ANY)
function setup()
parameter.boolean("Grayscale",true, function() grayscale = Grayscale end)
p=PolarAnim()
m=mesh()
m:addRect(math.min(WIDTH,HEIGHT)/2,math.min(WIDTH,HEIGHT)/2,math.min(WIDTH,HEIGHT),math.min(WIDTH,HEIGHT))
m.texture=p.img
m.shader=shader(GrayScaleShader.v,GrayScaleShader.f)
m.shader.luminance=vec3(0.21,0.72,0.07)
end
function draw()
if grayscale then
p:draw()
m:draw()
else
p:draw(true)
end
end
GrayScaleShader={
v=[[
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;
}
]],
f=[[
precision highp float;
uniform lowp sampler2D texture;
uniform vec3 luminance;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;
float c = col.r*luminance.x + col.g*luminance.y + col.b*luminance.z;
gl_FragColor = vec4(c, c, c, col.a );
}
]]}
--# PolarAnim
PolarAnim = class()
function PolarAnim:init()
self.red = true
pi,sin,cos,tan,asin,acos,atan,log,ln,abs,sqrt =
math.pi,math.sin,math.cos,math.tan,math.asin,
math.acos,math.atan,math.log,math.ln,math.abs,math.sqrt
self.xmi,self.xma,self.rangex,self.ymi,self.yma,
self.rangey,theta,self.blend = 0,0,0,0,0,0,0,0
self:grid(-200,200,-200,200)
local thsteptbl = {77.70001,pi/3.3333,pi/4.4444,pi/11.111,pi/2.2222,pi/1.1111,pi/9.9999;
pi/1.23456,pi/.7777,pi*3.3333,pi*1.1111,pi*4.4444,pi*.757575;
pi*pi,pi*pi*pi*pi*pi+.000003,pi*.5001,thstep = pi*.1111,thstep = pi*.2222}
self.thstep = thsteptbl[math.random(#thsteptbl)]
g = math.random(200)/2
self.func = "200*cos(g*theta)"
self.img = image(math.min(WIDTH,HEIGHT),math.min(WIDTH,HEIGHT))
self.perframe = math.random(10,100)
self.drawcol = true
self.frame=0
self.a = true
end
function PolarAnim:draw(draw)
if self.drawcol == true then
self:colorCycle()
else
if self.a then
self.frame = self.frame + 1
if self.frame == 254 then self.a = false end
else
self.frame = self.frame - 1
if self.frame == 0 then self.a = true end
end
end
background(255)
for i=0,self.perframe do
self:calcpoint()
end
if draw then
pushMatrix()
translate(math.min(WIDTH,HEIGHT)/2,math.min(WIDTH,HEIGHT)/2)
sprite(self.img,0,0)
popMatrix()
end
end
function PolarAnim:calcpoint()
local dist = loadstring("return "..self.func)()
local x = dist*math.cos(theta)
local y = dist*math.sin(theta)
local lastpoi = poi
self:convertRawToScreenCoords(x,y)
setContext(self.img)
smooth()
if self.drawcol == false then
stroke(self.frame%255)
elseif self.drawcol == true then
stroke(self.col)
end
fill(stroke())
strokeWidth(1)
if lastpoi then
line(lastpoi.x,lastpoi.y,poi.x,poi.y)
end
setContext()
theta = theta + self.thstep
end
function PolarAnim:grid(xmin,xmax,ymin,ymax)
self.origin = vec2(0,0)
if xmin<0 and 0<xmax then
self.xmi = xmin
self.xma = xmax
self.rangex = xmax - xmin
local xaxisloc = 0-xmin
self.origin.x = (xaxisloc/self.rangex)*math.min(WIDTH,HEIGHT)
else
self.xmi = xmin
self.xma = xmax
self.rangex = xmax - xmin
local xaxisloc = 0-xmin
self.origin.x = (xaxisloc/self.rangex)*math.min(WIDTH,HEIGHT)
end
if ymin<0 and 0<ymax then
self.ymi = ymin
self.yma = ymax
self.rangey = ymax-ymin
local yaxisloc = 0-ymin
self.origin.y = (yaxisloc/self.rangey)*math.min(WIDTH,HEIGHT)
else
self.ymi = ymin
self.yma = ymax
self.rangey = ymax-ymin
local yaxisloc = 0-ymin
self.origin.y = (yaxisloc/self.rangey)*math.min(WIDTH,HEIGHT)
end
end
function PolarAnim:convertRawToScreenCoords(x,y)
local xp = x-self.xmi
local coord = vec2((xp/self.rangex)*math.min(WIDTH,HEIGHT),0)
local yp = y-self.ymi
coord.y = (yp/self.rangey)*math.min(WIDTH,HEIGHT)
drawpoint = true
poi = coord
return coord
end
function PolarAnim:colorCycle()
self.blend = self.blend + .01
if self.red then
local c2 = color(255, 0, 0, 255)
local c1 = color(255,127.5,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.red = false
self.orange = true
end
elseif self.orange then
local c2 = color(255,127.5,0,255)
local c1 = color(255,255,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.orange = false
self.yellow = true
end
elseif self.yellow then
local c2 = color(255,255,0,255)
local c1 = color(127.5,255,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.yellow = false
self.yellowgreen = true
end
elseif self.yellowgreen then
local c2 = color(127.5,255,0,255)
local c1 = color(0,255,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.yellowgreen = false
self.green = true
end
elseif self.green then
local c2 = color(0,255,0,255)
local c1 = color(0, 255, 127.5, 255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.green = false
self.seafoamgreen = true
end
elseif self.seafoamgreen then
local c2 = color(0,255,127.5,255)
local c1 = color(0, 255, 255, 255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.seafoamgreen = false
self.teal = true
end
elseif self.teal then
local c2 = color(0,255,255,255)
local c1 = color(0,127.5,255,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.teal = false
self.skyblue = true
end
elseif self.skyblue then
local c2 = color(0,127.5,255,255)
local c1 = color(0,0,255,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.skyblue = false
self.blue = true
end
elseif self.blue then
local c2 = color(0,0,255,255)
local c1 = color(127.5,0,255,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.blue = false
self.purple = true
end
elseif self.purple then
local c2 = color(127.5,0,255,255)
local c1 = color(255, 0, 255, 255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.pink = true
self.purple = false
end
elseif self.pink then
local c2 = color(255, 0, 255, 255)
local c1 = color(255, 0, 127.5, 255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.fuscia = true
self.pink = false
end
elseif self.fuscia then
local c2 = color(255,0,127.5,255)
local c1 = color(255,0,0,255)
self.col = c1:mix(c2,self.blend)
if self.blend >= 1 then
self.blend = 0
self.red = true
self.fuscia = false
end
end
end