In response to a question on another thread I thought I’d try a very lightweight tutorial on the basics of 3d… hope you find it useful.
Below is code of the most limited thing you can do to get something working in 3d. The first thing you need is a mesh, all objects you draw are meshes, they are defined as a set of triangles specified as 3 vec3 coordinates for each corner of the triangle. In setup we create a basic triangle along the x,y plane sitting directly on top of the x axis.
Then in draw we must do 2 things before drawing the mesh. We need to configure our camera. This is 2 sets of coordinates, the first 3 numbers are the x,y,z of the position of the camera or eye. So below we put the camera on the ground 50 out from the screen. The second set of numbers is what the eye is looking at, so we put this looking straight back at the origin (0,0,0). You also need to call perspective to get it into perspective projection.
I’ve parameterised a couple of elements so you can see what impact they have.
-- 3d starter
-- Use this function to perform your initial setup
function setup()
myMesh = mesh()
myMesh.vertices = {vec3(-10,0,0), vec3(10,0,0), vec3(0,20,0)}
myMesh:setColors(color(255))
parameter.integer("cameraX",-40,40,0)
parameter.integer("lookatX",-20,20,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
camera(cameraX,0,-50,lookatX,0,0)
perspective()
myMesh:draw()
end