-
-
Save Jaybob/aed50b388e173257e9a5 to your computer and use it in GitHub Desktop.
Mesh Lab 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
--# Main | |
-- Main -- | |
function setup() | |
displayMode(FULLSCREEN) | |
-- parameter.action("LookAtCenter",function()cam:lookAt(vec3(0,0,0))end) | |
parameter.number("CamDist",0,2000,600) | |
vert = 1 | |
obj = 1 | |
touches = {} | |
cam = Camera() | |
ui = UI() | |
ls = Stick(10) | |
vPoint = Point() | |
modelMesh = mesh() | |
points = {vec3(-100,0,0),vec3(0,100,0),vec3(100,0,0)} | |
cset = {color(255, 255, 255, 255),color(255, 255, 255, 255),color(255, 255, 255, 255)} | |
snap = 50 | |
end | |
function flrCeil(n,v) | |
local lo = math.floor(n/v)*v | |
local hi = math.ceil(n/v)*v | |
if n <= lo+(v/2) then | |
return lo | |
else | |
return hi | |
end | |
end | |
function saveProject() | |
local s = "\n".. | |
"savedVerts = {".. | |
"\n" | |
local n = 0 | |
for k,v in ipairs(points) do | |
s = s .. "vec3("..v.x..","..v.y..","..v.z..")," | |
n = n + 1 | |
if n == 3 then s = s.."\n" n = 0 end | |
end | |
n = 0 | |
s = s.."} \n savedColors = { \n" | |
for k,v in ipairs(cset) do | |
s = s .. "color"..tostring(v).."," | |
n=n+1 | |
if n == 3 then s = s.."\n" n = 0 end | |
end | |
s = s .. "} \n --]]" | |
saveProjectTab("SaveData",s) | |
end | |
function addObject() | |
local a = vec3(flrCeil(cam.lat.x-100,snap),flrCeil(cam.lat.y,snap),flrCeil(cam.lat.z,snap)) | |
local b = vec3(flrCeil(cam.lat.x,snap),flrCeil(cam.lat.y+100,snap),flrCeil(cam.lat.z,snap)) | |
local c = vec3(flrCeil(cam.lat.x+100,snap),flrCeil(cam.lat.y,snap),flrCeil(cam.lat.z,snap)) | |
table.insert(points,1,a) | |
table.insert(points,1,b) | |
table.insert(points,1,c) | |
local col = color(math.random(100,255)) | |
table.insert(cset,1,col) | |
table.insert(cset,1,col) | |
table.insert(cset,1,col) | |
end | |
function delObject() | |
if #points > 3 then | |
local i = obj*3-2 | |
table.remove(points,i+2) | |
table.remove(points,i+1) | |
table.remove(points,i) | |
table.remove(cset,i+2) | |
table.remove(cset,i+1) | |
table.remove(cset,i) | |
end | |
end | |
function draw() | |
background(40, 40, 50) | |
perspective() | |
cam:draw() | |
cam.dist = CamDist | |
if not vPoint.touch then | |
cam:movement(ls.y,ls.x) | |
end | |
stroke(255, 255, 255, 255)strokeWidth(2) | |
pushMatrix() | |
-- translate(0,-200,0) | |
rotate(90,1,0,0) | |
for i = -5,5 do | |
line(-500,i*100,500,i*100)line(i*100,-500,i*100,500) | |
end | |
popMatrix() | |
modelMesh.vertices = points | |
modelMesh.colors = cset | |
modelMesh:draw() | |
vPoint:draw() | |
ortho() | |
viewMatrix(matrix()) | |
ui:draw() | |
ls:draw() | |
end | |
function touched(touch) | |
if touch.state == ENDED then | |
touches[touch.id] = nil | |
else | |
touches[touch.id] = touch | |
end | |
if not vPoint.touch and touch.y > 200 and touch.x < WIDTH-30 then | |
cam.angH = cam.angH + touch.deltaX | |
cam.angV = cam.angV + touch.deltaY | |
end | |
if touch.x > WIDTH-30 and touch.y > 200 then | |
cam.eye.y = cam.eye.y + touch.deltaY | |
cam.lat.y = cam.lat.y + touch.deltaY | |
end | |
vPoint:touches() | |
end | |
--# Camera | |
-- Camera -- | |
Camera = class() | |
function Camera:init(eX,eY,eZ,lX,lY,lZ) | |
self.eye = vec3(eX or 0, eY or 0, eZ or 400) | |
self.lat = vec3(lX or 0, lY or 0, lZ or 0) | |
self.dist = self.lat:dist(self.eye) | |
self.angH = 90 | |
self.angV = -135 | |
self.fix = true | |
self.crosshair = false | |
end | |
function Camera:draw() | |
self.angV = math.min(-1,math.max(-179,self.angV)) | |
if self.angH < -90 then self.angH = 270 end | |
if self.angH > 270 then self.angH = -90 end | |
if self.fix then | |
self.eye = -self:rotatePoint() + self.lat | |
else | |
self.lat = self:rotatePoint() + self.eye | |
end | |
camera(self.eye.x, self.eye.y, self.eye.z,self.lat.x, self.lat.y, self.lat.z) | |
-- self:drawCrosshair() | |
self. mat = modelMatrix()*viewMatrix()*projectionMatrix() | |
end | |
function Camera:screenPos(p) | |
local m = self.mat | |
m = m:translate(p.x, p.y, p.z) | |
local X, Y = (m[13]/m[16]+1)*WIDTH/2, (m[14]/m[16]+1)*HEIGHT/2 | |
return vec2(X,Y) | |
end | |
function Camera:movement(z,x) | |
if z and z ~= 0 then | |
local zVel = (self.lat-self.eye):normalize() * z | |
self.eye = self.eye + zVel | |
self.lat = self.lat + zVel | |
end | |
if x and x ~= 0 then | |
local xVel = (self.lat-self.eye):cross(vec3(0,1,0)):normalize() * x | |
self.eye = self.eye + xVel | |
self.lat = self.lat + xVel | |
end | |
end | |
function Camera:rotatePoint() | |
-- calculate y and z from angV at set distance | |
local y = math.cos(math.rad(self.angV))*self.dist | |
local O = math.sin(math.rad(self.angV))*self.dist | |
-- calculate x and z from angH using O as the set distance | |
local x = math.cos(math.rad(self.angH))*O | |
local z = math.sin(math.rad(self.angH))*O | |
return vec3(x,y,z) | |
end | |
function Camera:updateAngles() | |
self.angH = math.deg(math.atan2(self.lat.z-self.eye.z,self.lat.x-self.eye.x))+180 | |
self.angV = -math.deg(math.acos((self.lat.y-self.eye.y)/self.dist)) | |
end | |
function Camera:lookAt(target) | |
self.dist = self.eye:dist(target) | |
self.lat = target | |
self:updateAngles() | |
end | |
function Camera:drawCrosshair(w) | |
local w = w or 50 | |
pushMatrix()pushStyle() | |
translate(self.lat.x,self.lat.y, self.lat.z) | |
strokeWidth(2) | |
stroke(38, 255, 0, 255) | |
line(-w/2,0,w/2,0) | |
stroke(255, 187, 0, 255) | |
line(0,-w/2,0,w/2) rotate(90,1,0,0) | |
stroke(73, 0, 255, 255) | |
line(0,-w/2,0,w/2) | |
popMatrix()popStyle() | |
end | |
--# Point | |
Point = class() | |
function Point:init() | |
-- you can accept and set parameters here | |
self.pos = vec3(0,0,0) | |
local xc,yc,zc = color(0, 82, 255, 255),color(255, 233, 0, 255),color(255, 24, 0, 255) | |
local l,w,s = 100,2,10 | |
self.lines = {cube(l,w,w,nil,xc),cube(w,l,w,nil,yc),cube(w,w,l,nil,zc)} | |
self.points = { | |
{t=vec3(-l/2,0,0),msh=cube(s,s,s,nil,xc),v=vec3(1,0,0),d="x"}, | |
{t=vec3(l/2,0,0),msh=cube(s,s,s,nil,xc),v=vec3(1,0,0),d="x"}, | |
{t=vec3(0,l/2,0),msh=cube(s,s,s,nil,yc),v=vec3(0,1,0),d="y"}, | |
{t=vec3(0,-l/2,0),msh=cube(s,s,s,nil,yc),v=vec3(0,1,0),d="y"}, | |
{t=vec3(0,0,l/2),msh=cube(s,s,s,nil,zc),v=vec3(0,0,1),d="z"}, | |
{t=vec3(0,0,-l/2),msh=cube(s,s,s,nil,zc),v=vec3(0,0,1),d="z"} | |
} | |
self.vertMark = cube(s/2,s/2,s/2,nil, | |
color(11, 255, 0, 255)) | |
end | |
function Point:draw() | |
pushMatrix() | |
translate(self.pos.x,self.pos.y,self.pos.z) | |
for k,v in pairs(self.lines) do | |
v:draw() | |
end | |
for k,v in pairs(self.points) do | |
pushMatrix() | |
translate(v.t.x,v.t.y,v.t.z) | |
v.msh:draw() | |
if self.touch == nil then | |
v.touchpos = cam:screenPos(self.pos+v.t) | |
for id,t in pairs(touches) do | |
if v.touchpos:dist(vec2(t.x,t.y)) < 50 and t.state == BEGAN then | |
self.touch = id | |
self.vec = v.v | |
self.dir = v.d | |
end | |
end | |
end | |
popMatrix() | |
end | |
popMatrix() | |
for i = obj*3-2,obj*3 do | |
local v = points[i] | |
pushMatrix() | |
translate(v.x,v.y,v.z) | |
self.vertMark:draw() | |
popMatrix() | |
end | |
if touches[self.touch] == nil then | |
self.pos = points[(obj*3)-3+vert] | |
self.last = self.pos | |
else | |
points[obj*3-3+vert] = self.pos | |
strokeWidth(2)stroke(255, 255, 255, 96) | |
pushMatrix() | |
translate(self.last.x,self.last.y,self.last.z) | |
if self.dir == "x" then | |
if cam.angV < -135 or cam.angV > -45 then rotate(90,1,0,0) end | |
elseif self.dir == "y" then | |
if cam.angH < 45 and cam.angH >-45 or cam.angH > 135 and cam.angH < 225 then | |
rotate(90,0,1,0) end | |
elseif self.dir == "z" then | |
if cam.angV < -135 or cam.angV > -45 then rotate(90,1,0,0) else rotate(90,0,1,0) | |
end | |
end | |
for i = -5,5 do | |
line(-snap*5,i*snap,snap*5,i*snap) | |
line(i*snap,-snap*5,i*snap,snap*5) | |
end | |
popMatrix() | |
end | |
end | |
function Point:touches(touch) | |
if touches[self.touch] == nil then | |
self.touch = nil | |
self.pos.x = flrCeil(self.pos.x,snap) | |
self.pos.y = flrCeil(self.pos.y,snap) | |
self.pos.z = flrCeil(self.pos.z,snap) | |
else | |
local i = 1 | |
local d = touches[self.touch].deltaX/2 | |
if self.dir == "y" then | |
d = touches[self.touch].deltaY/2 | |
elseif self.dir == "x" then | |
if cam.angH < 0 or cam.angH > 180 then i = -1 end | |
elseif self.dir == "z" then | |
if cam.angH < 90 or cam.angH > 270 then i = -1 end | |
end | |
self.pos = self.pos + (self.vec*(d*i)) | |
end | |
end | |
--# UI | |
UI = class() | |
function UI:init(x) | |
-- you can accept and set parameters here | |
self.colTray = ColorPallet(WIDTH- 305,5,150) | |
rrect = roundRect(100,50,5) | |
tstate = 0 | |
buttons = { | |
{x = WIDTH - 350,y = 130, w = 110, h = 50, t = "SetVertCol", cb = function() | |
cset[(obj*3)-3+vert] = self.colTray.output end}, | |
{x = WIDTH - 350,y = 70, w = 110, h = 50, t = "SetTriCol", cb = function() | |
cset[(obj*3)-3+1] = self.colTray.output | |
cset[(obj*3)-3+2] = self.colTray.output | |
cset[(obj*3)-3+3] = self.colTray.output | |
end}, | |
{x = WIDTH - 500,y = 130, w = 80, h = 50, t = "Vertices"}, | |
{x = WIDTH - 415,y = 130, w = 50, h = 50, t = "+", cb = function() | |
vert = vert + 1 end}, | |
{x = WIDTH - 555,y = 130, w = 50, h = 50, t = "-", cb = function() | |
vert = vert - 1 end}, | |
{x = WIDTH - 500,y = 70, w = 80, h = 50, t = "Object"}, | |
{x = WIDTH - 415,y = 70, w = 50, h = 50, t = "+", cb = function() | |
obj = obj + 1 end}, | |
{x = WIDTH - 555,y = 70, w = 50, h = 50, t = "-", cb = function() | |
obj = obj - 1 end}, | |
{x = WIDTH - 500,y = 10, w = 80, h = 50, t = "GridSnap"}, | |
{x = WIDTH - 415,y = 10, w = 50, h = 50, t = "+", cb = function() | |
snap = snap + 10 end}, | |
{x = WIDTH - 555,y = 10, w = 50, h = 50, t = "-", cb = function() | |
snap = snap - 10 end}, | |
{x = WIDTH - 680,y = 130, w = 110, h = 50, t = "AddObj", cb = function() | |
addObject() end}, | |
{x = WIDTH - 680,y = 70, w = 110, h = 50, t = "DelObj", cb = function() | |
delObject() end}, | |
{x = WIDTH - 810,y = 70, w = 110, h = 50, t = "SaveProj", cb = function() | |
saveProject() end}, | |
{x = WIDTH - 810,y = 10, w = 110, h = 50, t = "LoadProj", cb = function() | |
if savedVerts then | |
points = savedVerts | |
cset = savedColors | |
end | |
end}, | |
{x = WIDTH - 810,y = 130, w = 110, h = 50, t = "FixCam", cb = function() | |
if cam.fix then cam.fix = false else cam.fix = true end | |
end}, | |
} | |
end | |
function UI:draw() | |
spriteMode(CORNER) | |
if ct.state == ENDED then | |
if tstate == 1 then tstate = 2 | |
elseif tstate >= 1 then tstate = 0 | |
end | |
elseif not tstate or tstate == 0 then | |
tstate = 1 | |
end | |
rect(-1,-1,WIDTH+2,200) | |
rect(WIDTH-30,200,30,HEIGHT-200) | |
self.colTray:draw() | |
textMode(CENTER) | |
stroke(0, 0, 0, 255) | |
for k,v in pairs(buttons) do | |
fill(127, 127, 127, 255) | |
rect(v.x,v.y,v.w,v.h) | |
if press(v.x,v.y,v.w,v.h) then | |
if v.cb then | |
v.cb() | |
end | |
end | |
fill(178, 178, 178, 255) | |
text(v.t,v.w/2+v.x,v.h/2+v.y) | |
end | |
text(snap,WIDTH-620,35) | |
text(tostring(self.colTray.output),WIDTH-290,35) | |
snap = math.max(10,math.min(500,snap)) | |
if vert > 3 then vert = 1 end | |
if vert < 1 then vert = 3 end | |
if obj > #points/3 then obj = 1 end | |
if obj < 1 then obj = #points/3 end | |
end | |
function press(x,y,w,h) | |
local t = ct | |
if tstate == 2 and not vPoint.touch and | |
t.x > x and t.x < x + w and t.y > y and t.y < y + h then | |
return true | |
end | |
return false | |
end | |
--# ColorTray | |
ColorPallet = class() | |
function ColorPallet:init(x,y,w) | |
ct = CurrentTouch | |
self.mem = {} | |
self.tray = vec2(2,5) | |
for i = 1,self.tray.y + 1 do self.mem[i] = {} | |
for j = 1,self.tray.x do self.mem[i][j] = color(47, 47, 47, 255) end end | |
self.x = x | |
self.y = y | |
self.w = w | |
self._ = self.w / 18 | |
------ | |
self.tx = x +80 | |
self.ty = self.y | |
self.tw = self.w / self.tray.y | |
self.th = self.tw * self.tray.x | |
self.sy = self.w + self._ + 4 | |
self.sh = self.w / 15 | |
self.ay = self.sy + self.sh + self._ | |
self.ah = self.w / 15 | |
self.px = self.x+self.w | |
self.py = self.y | |
self.cy = self.py + self.w + self._/2 | |
self.cw = self.tw*2 | |
self.ch = self.tw | |
self.cx = self.x + (self.w - self.cw)/2 | |
self.h = self.cy + self.ch + self._/2 - self.y | |
------- | |
local C = {color(255,0,0),color(255,255,0),color(0,255,0), | |
color(0,255,255),color(0,0,255),color(255,0,255), | |
color(255,0,0),color(255,255,255),color(255,255,255,0)} | |
gMesh = mesh() | |
gMesh.vertices = {vec2(0,0),vec2(255,255),vec2(0,255),vec2(0,0),vec2(255,255),vec2(255,0)} | |
gMesh.colors = {C[9],C[8],C[9],C[9],C[8],C[8]} | |
gImg = image(255,255) | |
setContext(gImg) | |
gMesh:draw() | |
setContext() | |
sImg = image(300,25) | |
pushStyle() | |
setContext(sImg) | |
fill(0, 0, 0, 255)rect(0,0,300,25)blendMode(ADDITIVE) | |
for i = 1,7 do | |
tint(C[i]) | |
sprite(gImg,-75+(i*50),25,50,50) | |
sprite(gImg,-25+(i*50),25,-50,50) | |
end | |
setContext() | |
popStyle() | |
self.touched = { | |
{id="tray" ,x=self.tx, y=self.ty, w=self.tx+(self.tw*2), h=self.ty+(self.tw*5)}, | |
{id="slider",x=self.px, y=self.sy, w=self.px+self.w, h=self.sy+self.sh}, | |
{id="pallet",x=self.px, y=self.py, w=self.px+self.w, h=self.py+self.w}, | |
{id="color" ,x=self.tx,y=self.cy, w=self.tx+(self.tw*2),h=self.cy+(self.tw*2)}, | |
{id="alpha" ,x=self.px ,y=self.ay, w=self.px+self.w ,h=self.ay+self.ah}} | |
self.move = nil | |
ttime = 0 | |
end | |
function ColorPallet:draw() | |
pushStyle() | |
noStroke() | |
pushMatrix() | |
spriteMode(CORNER) | |
self:userinput() | |
T = nil -- set to nil if no tile is touched | |
for i = 1,#self.mem - 1 do | |
for j = 1,#self.mem[i] do | |
pushStyle() | |
local x,y,w,c = self.tw*(j-1)+self.tx,self.tw*(i-1)+self.ty,self.tw,self.mem[i][j] | |
if ct.x > x and ct.x < x + w and ct.y > y and ct.y < y + w then | |
T = vec2(j,i) -- get index of current touched tray tile | |
if self.active == "tray" and ttime == 20 then | |
move = c -- if touch is held store color | |
end | |
if move ~= nil then strokeWidth(2)stroke(255, 255, 255, 255) end | |
end | |
fill(c)rect(x,y,w,w) | |
popStyle() | |
end | |
end | |
if self.active == "color" and ttime == 20 then -- if selection color is held store color | |
move = selection | |
end | |
strokeWidth(2) | |
stroke(255, 255, 255, 255) | |
-- draw the hue slider | |
sprite(sImg,self.px + 1,self.sy,self.w - 2,self.sh) | |
fill(hueCol) | |
ellipse(self.px+pos.x,self.sy+self.sh/2,self.sh*1.5) | |
-- draw the alpha slider | |
sprite(gImg,self.px + 1,self.ay,self.w - 2,self.ah) | |
fill(rgb.r,rgb.g,rgb.b,alp) | |
ellipse(self.px+pos.w,self.ay+self.ah/2,self.ah*1.5) | |
-- draw the pallet | |
pushMatrix() | |
translate(self.px,self.py)fill(127, 127, 127, 255)rect(0,0,self.w,self.w) | |
rotate( 180)tint(127, 127, 127, 255)sprite(gImg,-self.w,-self.w,self.w) | |
rotate( -90)tint(255, 255, 255, 255)sprite(gImg,0,-self.w,self.w) | |
rotate( -90)tint(hueCol)sprite(gImg,0,0,self.w) | |
rotate( -90)tint(0, 0, 0, 255)sprite(gImg,-self.w,0,self.w) | |
popMatrix() | |
fill(rgb) | |
ellipse(self.px+pos.y,self.py+pos.z,self.ah*1.5) | |
fill(selection) | |
rect(self.tx,self.cy,self.cw,self.ch) | |
if move ~= nil then | |
stroke(0, 0, 0, 255) fill(move) | |
rect(ct.x - self.tw/2,ct.y - self.tw/2,self.tw,self.tw) | |
end | |
self.input = nil | |
self.output = selection | |
popMatrix() | |
popStyle() | |
end | |
function ColorPallet:userinput() | |
alp = nil | |
if self.input == nil then | |
selection = self.mem[#self.mem][1] | |
else | |
selection = self.input | |
end | |
if ct.state == ENDED then | |
-- if there is a tray at currentTouch | |
if self.active == "tray" or self.active == "color" then | |
if T ~= nil then | |
-- if a color has been grabbed | |
if move ~= nil then | |
-- update memory accordingly | |
self.mem[T.y][T.x] = move | |
-- if no color has been grabbed | |
elseif ttime > 0 then | |
-- set selection color of touched tray | |
selection = self.mem[T.y][T.x] | |
end | |
end | |
end | |
-- reset all counters | |
self.active,move,ttime = nil,nil,0 | |
elseif ct.state == BEGAN then | |
ttime = ttime + 1 | |
if self.active == nil then | |
for i = 1,#self.touched do | |
local t = self.touched[i] | |
if ct.x > t.x and ct.x < t.w and ct.y > t.y and ct.y < t.h then | |
self.active = t.id | |
end | |
end | |
end | |
end | |
if hue == nil then hue = 0 end | |
hsv = self:rgbtohsv(selection,hue/255) -- convert selection color to hsv | |
hue,sat,val = hsv.x, hsv.y, hsv.z -- split hsv to simplify future code | |
alp = selection.a | |
if self.active == "slider" then | |
print("kkk") | |
hue = ((ct.x - self.px) / self.w) * 1530 | |
if hue > 1529 then hue = 1529 elseif hue < 0 then hue = 0 end | |
elseif self.active == "pallet" then | |
sat = ((ct.x - self.px) / self.w) * 255 | |
if sat > 0255 then sat = 0255 elseif sat < 0 then sat = 0 end | |
val = ((ct.y - self.py) / self.w) * 255 | |
if val > 0255 then val = 0255 elseif val < 0 then val = 0 | |
end | |
elseif self.active == "alpha" then | |
alp = ((ct.x - self.px) / self.w) * 255 | |
if alp > 255 then alp = 255 elseif alp < 0 then alp = 0 end | |
end | |
pos = vec4((hue/1530)*self.w,(sat/255)*self.w,(val/255)*self.w,(alp/255)*self.w) | |
hueCol = valuetohue(hue) -- convert hue to get base color | |
rgb = self:hsvtorgb(vec3(hue,sat,val)) -- get then new color | |
self.mem[#self.mem][1] = color(rgb.r,rgb.g,rgb.b,alp) | |
end | |
function roundRect(w,h,r) | |
local img = image(w,h) | |
setContext(img) | |
fill(255, 255, 255, 255) | |
ellipse(0+r,0+r,r*2) | |
ellipse(0+w-r,0+r,r*2) | |
ellipse(0+w-r,0+h-r,r*2) | |
ellipse(0+r,0+h-r,r*2) | |
rect(0+r,0,w-r*2,h) | |
rect(0,0+r,w,h-r*2) | |
setContext() | |
return img | |
end | |
-- | |
function ColorPallet:rgbtohsv(c,oldhue) | |
local r,g,b,h = c.r,c.g,c.b | |
local min,max = math.min(r,g,b),math.max(r,g,b) | |
local v,delta = max,max - min | |
if max ~= 0 then s = delta / max else s,h = 0,0 end | |
if r == max then h = 0 + (g - b) / delta | |
elseif g == max then h = 2 + (b - r) / delta | |
elseif b == max then h = 4 + (r - g) / delta | |
end | |
if h < 0 then h = h + 6 end | |
if s == 0 then | |
h = oldhue | |
end | |
return vec3(h*255,s*255,v) | |
end | |
function ColorPallet:hsvtorgb(V) | |
local h,s,v = V.x/255,V.y/255,V.z | |
if s == 0 then return color(v,v,v) end | |
i = math.floor(h) | |
f = h - i | |
p = v * (1 - s) | |
q = v * (1 - s * f) | |
t = v * (1 - s * (1 - f)) | |
if i == 0 then r,g,b = v,t,p | |
elseif i == 1 then r,g,b = q,v,p | |
elseif i == 2 then r,g,b = p,v,t | |
elseif i == 3 then r,g,b = p,q,v | |
elseif i == 4 then r,g,b = t,p,v | |
elseif i == 5 then r,g,b = v,p,q | |
end | |
return color(r,g,b) | |
end | |
function valuetohue(v) | |
add = -v + 255 + math.floor( v / 255 ) * 255 | |
sub = v - math.floor( v / 255 ) * 255 | |
if v >= 1275 then r,g,b = 255,000,-v+1530 | |
elseif v >= 1020 then r,g,b = sub,000,255 | |
elseif v >= 0765 then r,g,b = 000,add,255 | |
elseif v >= 0510 then r,g,b = 000,255,sub | |
elseif v >= 0255 then r,g,b = add,255,000 | |
elseif v >= 0000 then r,g,b = 255,sub,000 | |
end | |
return color(r,g,b) | |
end | |
function huetovalue(h) | |
r,g,b = h.r,h.g,h.b max = math.max(r,g,b) | |
if r == max then | |
if b == 0 then v = g | |
elseif g == 0 then v = -b + 1530 | |
end | |
elseif g == max then | |
if r == 0 then v = b + 510 | |
elseif b == 0 then v = -r + 510 | |
end | |
elseif b == max then | |
if g == 0 then v = r + 1020 | |
elseif r == 0 then v = -g + 1020 | |
end | |
end | |
return v | |
end | |
--# Stick | |
-- Stick -- | |
Stick = class() | |
function Stick:init(ratio,x,y) | |
self.ratio = ratio or 1 | |
self.i = vec2(x or 100,y or 100)-- initial pos | |
self.v = vec2(0,0) -- stick pos relative to initial pos | |
self.b = b or 150 -- base diameter | |
self.s = s or 100 -- stick diameter | |
self.d = d or 50 -- dead zone diameter | |
self.a = 0 -- stick angle relative to initial pos | |
self.touchId = nil | |
self.x,self.y = 0,0 | |
end | |
function Stick:draw() | |
if touches[self.touchId] == nil then | |
for i,t in pairs(touches) do | |
if vec2(t.x,t.y):dist(self.i) < self.b/2 then self.touchId = i end | |
end | |
self.v = vec2(0,0) | |
else | |
self.v = vec2(touches[self.touchId].x,touches[self.touchId].y) - self.i | |
self.a = math.deg(math.atan2(self.v.y,self.v.x)) | |
end | |
self.t = math.min(self.b/2,self.v:len()) | |
if self.t >= self.b/2 then | |
self.v = vec2(math.cos(math.rad(self.a))*self.b/2,math.sin(math.rad(self.a))*self.b/2) | |
end | |
fill(127, 127, 127, 150) | |
ellipse(self.i.x,self.i.y,self.b) | |
ellipse(self.i.x+self.v.x,self.i.y+self.v.y,self.s) | |
self.v = self.v/(self.b/2)*self.ratio | |
self.t = self.t/(self.b/2)*self.ratio | |
self.x,self.y = self.v.x,self.v.y | |
end | |
--# Cube | |
-- Cube -- | |
function cube(w,h,z,t,col,texture) | |
local w,h,z,t = w/2,h/2,z/2,t or vec3(0,0,0) | |
local m = mesh() | |
local A,B,C,D,a,b,c,d = | |
vec3(-w, h, z)+t,vec3( w, h, z)+t,vec3(-w,-h, z)+t,vec3( w,-h, z)+t, | |
vec3(-w, h,-z)+t,vec3( w, h,-z)+t,vec3(-w,-h,-z)+t,vec3( w,-h,-z)+t | |
m.vertices = {A,a,b,A,B,b,c,C,D,c,d,D,c,a,A,c,C,A,D,d,b,D,B,b,c,d,b,c,a,b,A,B,D,A,C,D} | |
local a,b,c,d =vec2(0,1),vec2(1,1),vec2(0,0),vec2(1,0) | |
m.texCoords = {c,a,b,c,d,b,c,a,b,c,d,b,c,a,b,c,d,b,c,d,b,c,a,b,d,c,a,d,b,a,a,b,d,a,c,d} | |
m.texture = texture | |
if col then | |
m:setColors(col) | |
else | |
m:setColors(255,255,255,255) | |
end | |
return m | |
end | |
--# SaveData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment