Transformations

Transform Stack

3D shapes like sphere(), box(), and cylinder() do not have parameters to set their position. They are always drawn at the origin (0, 0, 0). If we want to change their position or shape, we need to use the transformation functions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* eslint-disable no-undef, no-unused-vars */

const USE_WEBGL = true;

function setup() {
  if (USE_WEBGL) {
    createCanvas(500, 500, WEBGL);
  } else {
    createCanvas(500, 500);
  }
}

function draw() {
  background("#292f33");

  fill("#eae3ce");

  translate(0, 0, 100);

  translate(-125, -125, 0);
  box(50, 50, 50);

  translate(250, 0, 0);
  sphere(50);

  translate(0, 250, 0);
  cylinder(50, 20);

  translate(-250, 0, 0);
  torus(50, 20);
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

import "p5";

Object.assign(window, { setup, draw, windowResized });
  • translate(), as we’ve already seen in the examples, will move everything drawn by the (x, y, z) offset set in the function.
  • rotateX(), rotateY(), and rotateZ() will reorient everything drawn after the functions are called.
  • scale() will apply a scalar multiplication to everything drawn after the function is called.

These transformations are stacked, meaning that they are added together. For example, calling translate(50, 0, -10); then translate(-10, 30, 15) will add the translations together; it is equivalent to calling translate(40, 30, 5);. The composition of all the transformations together is called the transformation matrix.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/* eslint-disable no-undef, no-unused-vars */

let rotAngle = 0;

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
}

function draw() {
  background("#292f33");
  fill("#eae3ce");

  rotAngle += deltaTime * 0.001;

  const shapeSize = width / 10.0;

  // Draw a box in the top-left of the canvas.
  translate(width * -0.25, height * -0.25);
  rotateX(rotAngle);
  rotateZ(rotAngle);
  box(shapeSize);

  // Draw a sphere in the top-right of the canvas.
  translate(width * 0.5, 0);
  rotateY(rotAngle);
  sphere(shapeSize);

  // Draw a cylinder in the bottom-right of the canvas.
  translate(0, height * 0.5);
  rotateX(rotAngle);
  rotateZ(rotAngle);
  cylinder(shapeSize, shapeSize);

  // Draw a torus in the bottom-left of the canvas.
  translate(width * -0.5, 0);
  rotateZ(rotAngle);
  torus(shapeSize, 15);
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

import "p5";

Object.assign(window, { setup, draw, windowResized });

The sketch above gives us some unexpected results.

  1. We don’t need the half-dimension translation translate(width * -0.5, height * -0.5);. This is only necessary when drawing 2D elements in our 3D WebGL context. We are drawing 3D shapes and are not interacting with the mouse (which is 2D), so we can use the canvas with the centered origin (0, 0, 0) directly.
  2. Since all the transformations are stacked, the rotations are applied to every shape that is drawn after the rotate...(...) calls. This gets hard to manage very quickly.

One solution to the problem is to call negating transformations after the shapes are drawn. This results in the transformation matrix being reset before we draw the next shape.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* eslint-disable no-undef, no-unused-vars */

let rotAngle = 0;

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
}

function draw() {
  background("#292f33");
  fill("#eae3ce");

  rotAngle += deltaTime * 0.001;

  const shapeSize = width / 10.0;

  // Draw a box in the top-left of the canvas.
  translate(width * -0.25, height * -0.25);
  rotateX(rotAngle);
  rotateZ(rotAngle);
  box(shapeSize);
  rotateZ(-rotAngle);
  rotateX(-rotAngle);
  translate(width * 0.25, height * 0.25);

  // Draw a sphere in the top-right of the canvas.
  translate(width * 0.25, height * -0.25);
  rotateY(rotAngle);
  sphere(shapeSize);
  rotateY(-rotAngle);
  translate(width * -0.25, height * 0.25);

  // Draw a cylinder in the bottom-right of the canvas.
  translate(width * 0.25, height * 0.25);
  rotateY(rotAngle);
  rotateZ(rotAngle);
  cylinder(shapeSize, shapeSize);
  rotateZ(-rotAngle);
  rotateY(-rotAngle);
  translate(width * -0.25, height * -0.25);

  // Draw a torus in the bottom-left of the canvas.
  translate(width * -0.25, height * 0.25);
  rotateZ(rotAngle);
  torus(shapeSize, 15);
  rotateZ(-rotAngle);
  translate(width * 0.25, height * -0.25);
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

import "p5";

Object.assign(window, { setup, draw, windowResized });

This can get very unwieldy, adding a lot of code to our sketch. Instead of reverting all transformations manually, we can use the push() and pop() functions.

  • push() is used to save the current state, including the transformation matrix.
  • pop() is used to revert to the last saved state.

By wrapping our transformations inside calls to push() and pop(), we can reset the state with minimal code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* eslint-disable no-undef, no-unused-vars */

let rotAngle = 0;

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
}

function draw() {
  background("#292f33");
  fill("#eae3ce");

  rotAngle += deltaTime * 0.001;

  const shapeSize = width / 10.0;

  // Draw a box in the top-left of the canvas.
  push();
  translate(width * -0.25, height * -0.25);
  rotateX(rotAngle);
  rotateZ(rotAngle);
  box(shapeSize);
  pop();

  // Draw a sphere in the top-right of the canvas.
  push();
  translate(width * 0.25, height * -0.25);
  rotateY(rotAngle);
  sphere(shapeSize);
  pop();

  // Draw a cylinder in the bottom-right of the canvas.
  push();
  translate(width * 0.25, height * 0.25);
  rotateY(rotAngle);
  rotateZ(rotAngle);
  cylinder(shapeSize, shapeSize);
  pop();

  // Draw a torus in the bottom-left of the canvas.
  push();
  translate(width * -0.25, height * 0.25);
  rotateZ(rotAngle);
  torus(shapeSize, 15);
  pop();
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

import "p5";

Object.assign(window, { setup, draw, windowResized });

Exercise

Create a WebGL p5.js sketch, and draw a random 3D primitve as a mouse cursor. For bonus points, change the primitive whenever the mouse is clicked.

You can fork this CodeSandbox template to get started.

Cursor Primitive

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* eslint-disable no-undef, no-unused-vars */

let rotAngle = 0;

let shape = 0;

function setup() {
  createCanvas(windowWidth, windowHeight, WEBGL);
}

function draw() {
  background("#292f33");
  fill("#eae3ce");

  rotAngle += deltaTime * 0.001;

  const shapeSize = width / 10.0;

  // Draw the shape under the mouse position.
  push();
  translate(mouseX + width * -0.5, mouseY + height * -0.5);
  rotateX(rotAngle);
  rotateZ(rotAngle);
  if (shape === 0) {
    box(shapeSize);
  } else if (shape === 1) {
    sphere(shapeSize);
  } else if (shape === 2) {
    cylinder(shapeSize, shapeSize);
  } else {
    torus(shapeSize, 15);
  }

  pop();
}

function mouseClicked() {
  shape = (shape + 1) % 4;
}

function windowResized() {
  resizeCanvas(windowWidth, windowHeight);
}

import "p5";

Object.assign(window, { setup, draw, mouseClicked, windowResized });