-
-
Save peteroyle/1848637 to your computer and use it in GitHub Desktop.
Rotating Sun In Codea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- CircleMask class courtesy of Simeon (http://twolivesleft.com/Codea/Talk/profile/3/Simeon) | |
-- Use this function to perform your initial setup | |
function setup() | |
print("Hello World!") | |
-- params: width, sides in mesh, texture detail, image resolution, rotation speed | |
sun = Sun(WIDTH*0.5, 128, 580, 52, 0.4) | |
end | |
-- This function gets called once every frame | |
function draw() | |
-- This sets a dark background color | |
background(40, 40, 50) | |
-- Do your drawing here | |
sun:draw() | |
end | |
---- | |
CircleMask = class() | |
function CircleMask:init(rad, sides) | |
-- you can accept and set parameters here | |
self.mesh = mesh() | |
local verts = {} | |
local tex = {} | |
for i=1,sides do | |
local r1 = (i-1)/sides * math.pi * 2 | |
local r2 = i/sides * math.pi * 2 | |
local p1 = vec2( math.cos(r1), math.sin(r1) ) | |
local p2 = vec2( math.cos(r2), math.sin(r2) ) | |
-- Verts | |
table.insert(verts, p1 * rad) | |
table.insert(verts, p2 * rad) | |
table.insert(verts, vec2(0,0)) -- center | |
-- Tex Coords | |
table.insert(tex, (p1 + vec2(1,1)) * 0.5) | |
table.insert(tex, (p2 + vec2(1,1)) * 0.5) | |
table.insert(tex, vec2(0.5,0.5)) | |
end | |
self.mesh.vertices = verts | |
self.mesh.texCoords = tex | |
self.mesh:setColors( 255,255,255 ) | |
end | |
function CircleMask:setDrawFunction(func, w, h) | |
self.mesh.texture = image(w,h) | |
self.drawFunction = func | |
end | |
function CircleMask:setTexture(tex) | |
self.mesh.texture = tex | |
self.drawFunction = nil | |
end | |
function CircleMask:draw() | |
if self.drawFunction ~= nil then | |
setContext( self.mesh.texture ) | |
pushMatrix() | |
resetMatrix() | |
self.drawFunction() | |
popMatrix() | |
setContext() | |
end | |
self.mesh:draw() | |
end | |
---- | |
Sun = class() | |
function Sun:init(diameter, sides, spotDetail, resolution, speed) | |
self.initialHeight = diameter | |
self.sunMask = CircleMask(self.initialHeight/2, sides) | |
self.numSpots = spotDetail | |
self.sunImgSize = resolution -- image resolution. Also impacts rotation speed | |
self.sunRot = 0 -- initial rotation | |
self.sunRotSpeed = speed -- rotations speed, pixels per frame | |
self.maxSunRot = self.initialHeight | |
self.sunMask:setDrawFunction(function() | |
self:drawRotatingTexture(self.sunImgSize) | |
end, self.sunImgSize, self.sunImgSize) | |
end | |
function Sun:drawMasked() | |
pushStyle() | |
pushMatrix() | |
resetMatrix() | |
translate(WIDTH/2, HEIGHT/2) | |
col = randomSunColour() | |
tint(col) | |
self.sunMask:draw() | |
popMatrix() | |
popStyle() | |
end | |
function Sun:draw() | |
self:drawMasked() | |
end | |
function randomSunColour() | |
red = math.random(10) + 240 | |
green = math.random(10) + 160 | |
col = color(red, green, 0, 255) | |
return col | |
end | |
function randomGray() | |
lum = math.random(30) + 220 | |
col = color(lum, lum, lum, 128) | |
return col | |
end | |
function Sun:drawRotatingTexture(size) | |
self:createSunImage(size*2, size) | |
x = (size/2) - self.sunRot | |
y = size/2 | |
w = size * 2 | |
h = size | |
self.sunRot = (self.sunRot + self.sunRotSpeed) % w | |
clip(0,0,size,size) | |
sprite(self.circleImg, x, y, w ,h) | |
sprite(self.circleImg, x + w - 1, y, w, h) | |
clip() | |
end | |
function Sun:drawSunTexture(w, h) | |
pushStyle() | |
noStroke() | |
fill(32, 32, 32, 255) -- fill white, we can tint() the image | |
ellipseMode(CORNERS) | |
rect(0,0,w,h) | |
for i=0, self.numSpots do | |
x = math.random(w) | |
y = math.random(h) | |
ew = math.random(h/2) | |
eh = math.random(h/2) | |
fill(randomGray()) | |
ellipseMode(CENTER) | |
ellipse(x, y, ew, eh) | |
-- make repeatable horizontally | |
ellipse(x+w, y, ew, eh) | |
ellipse(x-w, y, ew, eh) | |
end | |
popStyle() | |
end | |
function Sun:createSunImage( w, h ) | |
if self.circleImg ~= nil then | |
return | |
end | |
local i = image(w, h) | |
setContext(i) | |
self:drawSunTexture(w, h) | |
setContext() | |
self.circleImg = i | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment