Coding 3 D Shapes
Coding 3 D Shapes
1
Coding 3D Shapes
Example 1
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(20, 150, 130);
rotateY(millis() / 2000);
sphere(70);
}
2
Coding 3D Shapes
Understanding the Code
To create 3D shapes we will need to add WEBGL after setting the screen width (400)
and height (400) of the canvas. WEBGL enables 3D render by introducing the third
dimension: Z.
function setup() {
createCanvas(400, 400, WEBGL);
}
Under function draw(){ , type “background ()”. This function will set the background
color. I used background(20, 150, 130), but you can choose a different color.
function draw() {
sphere(70);
}
By typing “sphere(70)” the program will draw a sphere at the center of the canvas with a
radius of 70 pixels.
For 3D shapes, the parameters (the numbers following a shape) will set the dimensions
of that shape, either: radius, width, height, or depth.
Note: All of the examples in this workbook will remain in the center of the canvas.
3
Coding 3D Shapes
Looking Back at 2D Shapes
The first two numbers following square (0,0) set the location of the square’s top left
corner. The third number (200) sets the size of each side. This means our square will
start in the upper left corner of our canvas at (0,0) and its size will be 200 pixels
by 200 pixels, halfway across the x-axis and halfway across the y-axis.
4
Coding 3D Shapes
Create other 3D shapes.
https://p5js.org/reference/#/p5/sphere
https://p5js.org/reference/#/p5/box
https://p5js.org/reference/#/p5/torus
https://p5js.org/reference/#/p5/cone
5
Coding 3D Shapes
Rotating Shapes
In Example 1, the sphere rotates around the Y-axis using this line of code:
rotateY(millis() / 2000);
Experiment 1: rotateX()
Add the following line of code to Example 1: rotateX(millis() / 2000)
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(20, 150, 130);
rotateX(millis() / 2000);
rotateY(millis() / 2000);
sphere(70);
}
6
Coding 3D Shapes
Experiment 2: rotateY()
Add another line of code: rotateZ(millis() / 2000);
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(20, 150, 130);
rotateX(millis() / 2000);
rotateY(millis() / 2000);
rotateZ(millis() / 2000);
sphere(70);
}
https://p5js.org/reference/#/p5/rotate
https://p5js.org/reference/#/p5/rotateX
https://p5js.org/reference/#/p5/rotateY
7
Coding 3D Shapes
Experimenting with millis ()
What happened? Experiment by changing the parameters for rotateX and rotateZ.
frameCount () The system variable frameCount contains the number of frames that
have been displayed since the program started. Inside setup() the value is 0, after the
first iteration of draw it is 1, etc.
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(120, 50, 80);
rotateY(frameCount * 0.01);
rotateZ(frameCount * 0.01);
rotateX(frameCount * 0.01);
torus(70,30);
}
https://p5js.org/reference/#/p5/millis
https://p5js.org/reference/#/p5/frameCount
8
Coding 3D Shapes
Two 3D Shapes
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(120, 50, 80);
rotateX(frameCount * 0.01);
rotateY(frameCount * 0.01);
rotateZ(frameCount * 0.01);
torus(80,20);
rotateY(frameCount * 0.02);
torus(30,10);
}
9
Coding 3D Shapes
Three 3D Shapes
function setup() {
createCanvas(400, 400, WEBGL);
}
function draw() {
background(120, 50, 80);
rotateY(frameCount * 0.01);
rotateZ(frameCount * 0.01);
rotateX(frameCount * 0.01);
torus(100,10);
rotateZ(frameCount * 0.02);
torus(150, 5);
rotateX(frameCount * 0.01);
sphere(60);
}
https://p5js.org/examples/3d-geometries.html
https://p5js.org/reference/#/p5/translate
https://p5js.org/reference/#/p5/push
https://p5js.org/reference/#/p5/pop
10