# 3D Shapes

``````
--# Main
-- ShapeLibrary --
function setup()
parameter.integer("CurrentShape",1,5,1)
parameter.number("camDist",0,2000,1000)
parameter.number("Y",-1000,1000,200)
parameter.number("ang",-360,360,-70)
shapes = {
Shape:circleNofill(200,30,4),
Shape:circle(200,30),
Shape:sphere(200,50),
Shape:cube(400,400,400),
Shape:rCube(400,400,400,100,20)}
for k,v in pairs(shapes) do
for j = 1,#v.vertices do
v:color(j,math.random(50,255),math.random(50,255),math.random(50,255))
end
end
end

function draw()
background(24, 24, 24, 255)
perspective()
camera(x,Y,z,0,0,0)
shapes[CurrentShape]:draw()
end

--# Shape
Shape = class()

local t = t or vec3(0,0,0)
local p = {}
local vi = 180/n
local hi = 360/n
for angV = -90,90-vi,vi do
local h1 = math.cos(math.rad(angV)) * r
local h2 = math.cos(math.rad(angV+vi)) * r
local y1 = math.sin(math.rad(angV)) * r
local y2 = math.sin(math.rad(angV+vi)) * r
for angH = -180,180-hi,hi do
p[#p+1] = p1 + t
p[#p+1] = p2 + t
p[#p+1] = p4 + t
----------------
p[#p+1] = p1 + t
p[#p+1] = p3 + t
p[#p+1] = p4 + t
end
end
local m = mesh()
m.vertices = p
m:setColors(255,255,255,255)
return m
end

function Shape:cube(w,h,z,t) -- width,height,depth,translate
local w,h,z,t = w/2,h/2,z/2,t or vec3(0,0,0)
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
local m = mesh()
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}
m:setColors(255,255,255,255)
return m
end

local p = {}
local t = t or vec3(0,0,0)
local j = 360/n
for i = 0,360,j do
p[#p+1] = t
end
local m = mesh()
m.vertices = p
m:setColors(255,255,255,255)
return m
end

local p = {}
local t = t or vec3(0,0,0)
local j = 360/n
for i = 0,360,j do
p[#p+1] = p1 + t
p[#p+1] = p2 + t
p[#p+1] = p3 + t
------------
p[#p+1] = p2 + t
p[#p+1] = p3 + t
p[#p+1] = p4 + t
end
local m = mesh()
m.vertices = p
m:setColors(255,255,255,255)
return m
end

local w,h,z = w/2,h/2,z/2
local t = t or vec3(0,0,0)
local p = {
vec3(-w+r,-h+r,-z),vec3(-w+r,h-r,-z),vec3(w-r,h-r,-z),
vec3(-w+r,-h+r,-z),vec3(w-r,-h+r,-z),vec3(w-r,h-r,-z),--front
vec3(-w+r,-h+r,z),vec3(-w+r,h-r,z),vec3(w-r,h-r,z),
vec3(-w+r,-h+r,z),vec3(w-r,-h+r,z),vec3(w-r,h-r,z),--back
vec3(-w,-h+r,-z+r),vec3(-w,h-r,-z+r),vec3(-w,h-r,z-r),
vec3(-w,-h+r,-z+r),vec3(-w,-h+r,z-r),vec3(-w,h-r,z-r),--left
vec3(w,-h+r,-z+r),vec3(w,h-r,-z+r),vec3(w,h-r,z-r),
vec3(w,-h+r,-z+r),vec3(w,-h+r,z-r),vec3(w,h-r,z-r),--right
vec3(-w+r,h,-z+r),vec3(-w+r,h,z-r),vec3(w-r,h,z-r),
vec3(-w+r,h,-z+r),vec3(w-r,h,-z+r),vec3(w-r,h,z-r),--top
vec3(-w+r,-h,-z+r),vec3(-w+r,-h,z-r),vec3(w-r,-h,z-r),
vec3(-w+r,-h,-z+r),vec3(w-r,-h,-z+r),vec3(w-r,-h,z-r)}--top
self:cubeEdge(p,n,r,0,h,vec3(w-r,0,z-r))
self:cubeEdge(p,n,r,-90,h,vec3(w-r,0,-z+r))
self:cubeEdge(p,n,r,90,h,vec3(-w+r,0,z-r))
self:cubeEdge(p,n,r,180,h,vec3(-w+r,0,-z+r))
self:cubeEdge(p,n,r,0,w,vec3(0,h-r,z-r),1)
self:cubeEdge(p,n,r,-90,w,vec3(0,h-r,-z+r),1)
self:cubeEdge(p,n,r,90,w,vec3(0,-h+r,z-r),1)
self:cubeEdge(p,n,r,180,w,vec3(0,-h+r,-z+r),1)
self:cubeEdge(p,n,r,0,z,vec3(w-r,h-r,0),2)
self:cubeEdge(p,n,r,90,z,vec3(-w+r,h-r,0),2)
self:cubeEdge(p,n,r,-90,z,vec3(w-r,-h+r,0),2)
self:cubeEdge(p,n,r,180,z,vec3(-w+r,-h+r,0),2)
self:cubeCorner(p,n,r,0,0,vec3(w-r,h-r,z-r))
self:cubeCorner(p,n,r,0,-90,vec3(w-r,h-r,-z+r))
self:cubeCorner(p,n,r,0,90,vec3(-w+r,h-r,z-r))
self:cubeCorner(p,n,r,0,180,vec3(-w+r,h-r,-z+r))
self:cubeCorner(p,n,r,-90,0,vec3(w-r,-h+r,z-r))
self:cubeCorner(p,n,r,-90,-90,vec3(w-r,-h+r,-z+r))
self:cubeCorner(p,n,r,-90,90,vec3(-w+r,-h+r,z-r))
self:cubeCorner(p,n,r,-90,180,vec3(-w+r,-h+r,-z+r))
local m = mesh()
for k,v in pairs(p) do v = v + t end
m.vertices = p
m:setColors(255,255,255,255)
return m
end

function Shape:cubeCorner(p,n,r,v,h,t)
local vi = 90/n
for angV = v,v+90-vi,vi do
local h1 = math.cos(math.rad(angV)) * r
local h2 = math.cos(math.rad(angV+vi)) * r
local y1 = math.sin(math.rad(angV)) * r
local y2 = math.sin(math.rad(angV+vi)) * r
for angH = h,h+90-vi,vi do
p[#p+1] = p1 + t
p[#p+1] = p2 + t
p[#p+1] = p4 + t
------------
p[#p+1] = p1 + t
p[#p+1] = p3 + t
p[#p+1] = p4 + t
end
end
end

function Shape:cubeEdge(p,n,r,a,l,tr,flip)
local ai = 90/n
for i = a,a+90-ai,ai do
local x1,y1,z1,x2,y2,z2 = p1,-l+r,p2,p3,l-r,p4
if not flip then
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x2,y1,z2)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
---------------------------
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x1,y2,z1)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
end
if flip == 1 then
x1,y1,z1,x2,y2,z2 = -l+r,p1,p2,l-r,p3,p4
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x2,y1,z1)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
---------------------------
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x1,y2,z2)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
end
if flip == 2 then
x1,y1,z1,x2,y2,z2 = p1,p2,-l+r,p3,p4,l-r
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x1,y1,z2)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
---------------------------
p[#p+1] = vec3(x1,y1,z1)+tr
p[#p+1] = vec3(x2,y2,z1)+tr
p[#p+1] = vec3(x2,y2,z2)+tr
end
end
end

``````

@Jaybob Nice job, I like the rounded cube.

@dave1707 Thanks, was a real pain to make!

@Jaybob I’m sure it was, but I bet you enjoyed every minute creating it.

great?like gem or jewel