# 3D question

Hello everyone! I have been working on my next codea project, and I decided to use 3D in it. When I realized how complicated meshes were I decided to use normal shapes, and there I realized there was no cube() function. So to educate myself in 3D and make a useful function I decided to make this myself. I have a problem where it cuts of the faces of my cube, here is my (incomplete) code

``````function setup()

end

function draw()
background(0, 0, 0, 255)
fill(255, 0, 0, 255)
stroke(255, 255, 255, 255)
strokeWidth(2)
rotate(45, 1, 1, 0)
cube(100, 100, 0, 20,20,5)
end

function cube(x, y, z, w, h, d)
translate(x, y, z)
translate(0,0,-d)
rect(0,0,w,h)
translate(w,0,d)
rotate(90, 0, 1, 0)
rect(0,0,d,h)
end
``````

Anyone know how I can fix this?
Jordan

Hey again, I have gone a bit further into this project, and I have swapped out rects for meshes (Not too complicated… I hope). The same bug still applies though, but weirdly it vanished with a size of less than 7 (even if it is scaled). Here is my code:

``````function setup()
--ortho(0,WIDTH,0,HEIGHT,-50,50)
iparameter("R",-360,360, 0)
parameter("x",0,1,1)
parameter("y",0,1,1)
parameter("z",0,1,0)
iparameter("size", 1, 30, 5)
end

function draw()
background(0, 0, 0, 255)
translate(WIDTH/2,HEIGHT/2)
scale(5)
rotate(R,x,y,z)
fill(255, 0, 0, 255)
cube(0,0,0,size,size,size)

end

function cube(x,y,z,w,h,d)
local cube = mesh()
local v = {
vec3(x-w,y-h,z+d),vec3(x-w,y+h,z+d),vec3(x+w,y-h,z+d),vec3(x+w,y+h,z+d),
vec3(x-w,y-h,z-d),vec3(x-w,y+h,z-d),vec3(x+w,y-h,z-d),vec3(x+w,y+h,z-d)
}
local faces = {
v[1],v[2],v[3],v[2],v[3],v[4],
v[2],v[4],v[6],v[4],v[6],v[8],
v[1],v[2],v[5],v[2],v[5],v[6],
v[3],v[4],v[7],v[4],v[7],v[8],
v[1],v[3],v[5],v[3],v[5],v[7],
v[5],v[6],v[7],v[6],v[7],v[8]
}
local c = { color(255, 0, 0, 255), color(0, 255, 0, 255), color(255, 243, 0, 255),
color(0, 0, 255, 255), color(255, 255, 255, 255), color(255, 0, 189, 255)}
local colors = {}
for i = 1, 6 do
for j = 1, 6 do
table.insert(colors, c[i])
end
end
cube.vertices = faces
cube.colors = colors
cube:draw()
end
``````

Thanks
Jordan

@Jordan

I made some minor changes to your code. I think your original problem might have been with the camera function. I’m not sure if there are default camera values if you don’t specify them, but it was acting like your cube was being clipped. In other words, it was moving behind the camera as you were increasing the scale. I’m not sure if that’s what was happening, but the changes I made seems to help.

``````
function setup()
iparameter("R",-360,360, -90)  -- changes starting angle to -90
parameter("x",0,1,1)
parameter("y",0,1,1)
parameter("z",0,1,0)
iparameter("size", 1, 50, 20)  -- changed size values
end

function draw()
background(0, 0, 0, 255)
camera(-200,0,0,0,0,0)   -- added camera and removed scale(5)
rotate(R,x,y,z)
fill(255, 0, 0, 255)
cube(0,0,0,size,size,size)
end

function cube(x,y,z,w,h,d)
local cube = mesh()
v = {
vec3(x-w,y-h,z+d),vec3(x-w,y+h,z+d),vec3(x+w,y-h,z+d),vec3(x+w,y+h,z+d),
vec3(x-w,y-h,z-d),vec3(x-w,y+h,z-d),vec3(x+w,y-h,z-d),vec3(x+w,y+h,z-d)
}

local faces = {
v[1],v[2],v[3],v[2],v[3],v[4],
v[2],v[4],v[6],v[4],v[6],v[8],
v[1],v[2],v[5],v[2],v[5],v[6],
v[3],v[4],v[7],v[4],v[7],v[8],
v[1],v[3],v[5],v[3],v[5],v[7],
v[5],v[6],v[7],v[6],v[7],v[8]
}
local c = { color(255, 0, 0, 255), color(0, 255, 0, 255), color(255, 243, 0, 255),
color(0, 0, 255, 255), color(255, 255, 255, 255), color(255, 0, 189, 255)}
local colors = {}
for i = 1, 6 do
for j = 1, 6 do
table.insert(colors, c[i])
end
end
cube.vertices = faces
cube.colors = colors
cube:draw()
end

``````

Thanks @Dave1707 ! I just have one question. I looked at the reference of the camera() function, and I it never really clicked for me. I thought that if you were to move the camera without affecting the 3D you should move it in the Z axis. Is there a reason why you moved it in the X axis instead? And what are the default values for the Eye/X/Y/Z and CameraX/Y/Z

@Jordan
Basically I just set the values so that they worked. I haven’t found anything yet that gave a good explanation of the 9 values for the camera. What I think so far is the “eye values” are the x,y,z coordinates of the camera position. The “center values” are the x,y,z coordinates of where the camera is pointed. The “up values” are what combination of axis are pointed up. I changed a program I had so that I could alter the 9 camera values to see what would happen. The above information is what I think from playing around, but I also got some crazy results I haven’t figured out yet. As for default values, I haven’t seen anything other than the 0,1,0 up values.

Here is an example I put together to demo the different values for rotate and camera. The eyeXYZ values set the camera position. The cXYZ values set where the camera is pointed. The upXYZ values set which axis is pointing up and which axis the rotation goes around. One or more of the upXYZ values can be set to rotate about a combination of the axis. I also show the rotate and camera functions with their values. Hope this helps explain how the rotate and camera functions work. After the program starts, pull the output pane all the way down to show all the parameters.

``````
-- 3d example

function setup()
iparameter("rot",-180,180,70)
iparameter("size", 0, 20, 20)
iparameter("eyeX",-200,200,90)
iparameter("eyeY",-200,200,50)
iparameter("eyeZ",-200,200,70)
iparameter("cX",-100,100,0)
iparameter("cY",-100,100,0)
iparameter("cZ",-100,100,0)
iparameter("upX",0,1,0)
iparameter("upY",0,1,1)
iparameter("upZ",0,1,0)
end

function draw()
background(0, 0, 0, 255)

textMode(CORNER)
text("3D Example",200,1000)
str=string.format("rotate(%d,%d,%d,%d)",
rot,upX,upY,upZ)
text(str,50,900)
str=string.format("camera(%d,%d,%d, %d,%d,%d, %d,%d,%d)",
eyeX,eyeY,eyeZ,cX,cY,cZ,upX,upY,upZ)
text(str,50,850)

perspective(90)
camera(eyeX,eyeY,eyeZ,cX,cY,cZ,upX,upY,upZ)
rotate(rot,upX,upY,upZ)
strokeWidth(4)

textMode(CENTER)
stroke(255,0,0)
fill(255, 0, 0)
text("-x",-50,0)
text("+x",50,0)
line(-40,0,40,0)

stroke(0,0,255)
fill(0,0,255)
text("-y",0,-50)
text("+y",0,50)
line(0,-40,0,40)

s=size * .1
scale(s,s,s)
create3d()
end

function create3d()
c3d = mesh()

vp = {vec3(0,0,0),vec3(-20,0,0),vec3(0,0,20),
vec3(0,0,0),vec3(0,20,0),vec3(0,0,20),
vec3(0,0,0),vec3(0,-20,0),vec3(20,0,0)}

sides = {
vp[1],vp[2],vp[3],
vp[4],vp[5],vp[6],
vp[7],vp[8],vp[9]
}

c3d.vertices = sides
c3d:setColors(0,0,0)

c3d:color(1,255,0,0)    -- red
c3d:color(2,255,0,0)
c3d:color(3,255,0,0)

c3d:color(4,0,0,255)    -- blue
c3d:color(5,0,0,255)
c3d:color(6,0,0,255)

c3d:color(7,0,255,0)    -- green
c3d:color(8,0,255,0)
c3d:color(9,0,255,0)

c3d:draw()
end

``````