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)
    x = math.cos(math.rad(ang))*- camDist
    z = math.sin(math.rad(ang))*- camDist
    perspective()
    camera(x,Y,z,0,0,0)
    shapes[CurrentShape]:draw()
end



--# Shape
Shape = class()

function Shape:sphere(r,n,t) -- radius,detail,translate
    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
            local p1 = vec3(math.cos(math.rad(angH))*h1,y1,math.sin(math.rad(angH))*h1)
            local p2 = vec3(math.cos(math.rad(angH+hi))*h1,y1,math.sin(math.rad(angH+hi))*h1)
            local p3 = vec3(math.cos(math.rad(angH))*h2,y2,math.sin(math.rad(angH))*h2)
            local p4 = vec3(math.cos(math.rad(angH+hi))*h2,y2,math.sin(math.rad(angH+hi))*h2)
            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

function Shape:circle(r,n,t) -- radius,detail,tanslate
    local p = {}
    local t = t or vec3(0,0,0)
    local j = 360/n
    for i = 0,360,j do
        p[#p+1] = t
        p[#p+1] = vec3(math.sin(math.rad(i))*(r),math.cos(math.rad(i))*(r),0)+t
        p[#p+1] = vec3(math.sin(math.rad(i+j))*(r),math.cos(math.rad(i+j))*(r),0)+t
    end
    local m = mesh()
    m.vertices = p
    m:setColors(255,255,255,255)
    return m
end

function Shape:circleNofill(r,n,w,t) -- radius,detail,width,translate
    local p = {}
    local t = t or vec3(0,0,0)
    local j = 360/n
    for i = 0,360,j do
        p1 = vec3(math.sin(math.rad(i))*(r-w),math.cos(math.rad(i))*(r-w),0)
        p2 = vec3(math.sin(math.rad(i))*(r),math.cos(math.rad(i))*(r),0)
        p3 = vec3(math.sin(math.rad(i+j))*(r-w),math.cos(math.rad(i+j))*(r-w),0)
        p4 = vec3(math.sin(math.rad(i+j))*(r),math.cos(math.rad(i+j))*(r),0)
        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

function Shape:rCube(w,h,z,r,n,t) --width,height,depth,edgeRadius,detail
    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
            local p1 = vec3(math.cos(math.rad(angH))*h1,y1,math.sin(math.rad(angH))*h1)
            local p2 = vec3(math.cos(math.rad(angH+vi))*h1,y1,math.sin(math.rad(angH+vi))*h1)
            local p3 = vec3(math.cos(math.rad(angH))*h2,y2,math.sin(math.rad(angH))*h2)
            local p4 = vec3(math.cos(math.rad(angH+vi))*h2,y2,math.sin(math.rad(angH+vi))*h2)
            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 p1 = math.cos(math.rad(i))*r --x
        local p2 = math.sin(math.rad(i))*r
        local p3 = math.cos(math.rad(i+ai))*r
        local p4 = math.sin(math.rad(i+ai))*r
        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