Chapters

Hide chapters

Metal by Tutorials

Fourth Edition · macOS 14, iOS 17 · Swift 5.9 · Xcode 15

Section I: Beginning Metal

Section 1: 10 chapters
Show chapters Hide chapters

Section II: Intermediate Metal

Section 2: 8 chapters
Show chapters Hide chapters

Section III: Advanced Metal

Section 3: 8 chapters
Show chapters Hide chapters

5. 3D Transformations
Written by Marius Horga & Caroline Begbie

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous chapter, you translated vertices and moved objects around the screen by calculating the position data in the vertex function. But there’s a lot more you’ll want to do when working in 3D space, such as rotating and scaling your objects. You’ll also want to have an in-scene camera so that you can move around your scene.

To move, scale and rotate a triangle, you’ll use matrices — and once you’ve mastered one triangle, it’s a cinch to rotate a model with thousands of triangles at once!

For those of us who aren’t math geniuses, vectors and matrices can be a bit scary. Fortunately, you don’t always have to know what’s under the hood when using math. To help, this chapter focuses not on the math, but the matrices. As you work through this chapter, you’ll gradually extend your linear algebra knowledge as you learn what matrices can do for you and how to manipulate them.

Transformations

Look at the following picture.

Affine Transformations
Affine Transformations

Using the vector image editor, Affinity Designer, you can scale and rotate a cat through a series of affine transformations. Instead of individually calculating each position, Affinity Designer creates a transformation matrix that holds the combination of the transformations. It then applies the transformation to each element.

Note: Affine means that after you’ve done the transformation, all parallel lines remain parallel.

Of course, no one wants to translate, scale and rotate a cat since they’ll probably bite. So instead, you’ll translate, scale and rotate a triangle.

The Starter Project & Setup

➤ Open and run the starter project located in the starter folder for this chapter.

Pci mfoxbaj rfovejf
Yso bxivnul chiselp

Translation

The starter project renders two triangles:

Cizywekuxems Lerdald
Zotykeduzekh Sijsahk

Vectors & Matrices

You can better describe position as a displacement vector of [0.3, -0.4, 0]. You move each vertex 0.3 units in the x-direction, and -0.4 in the y-direction from its starting position.

Diwgexh
Qokgant

Ey egewkozz xoytod
Uc osaxroft dewgix

The Magic of Matrices

When you multiply matrices, you combine them into one matrix. You can then multiply a vector by this matrix to transform the vector. For example, you can set up a rotation matrix and a translation matrix. You can then calculate the transformed position with the following line of code:

Creating a Matrix

➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).

Lhelxsuqiod kp umhojx e ralsoh nibudk ci vgu dipozait
Fvarvmadeaf xl oblamp a kavjes weriys gu pte yaxozoig

Scaling

➤ Open Renderer.swift, and in draw(in:), locate where you set matrix in the second red triangle.

Sjegojx jiqb u cegfuw
Xcurikk binh o sosnow

U hsasxyitig ixs hyuwap qgiawyxa
I vjowvdenoz ujw qsacuy jtaostmo

Rotation

You perform rotation in a similar way to scaling.

Hucuzatk ecauz fna axogec
Racisokv ayuis vci idavay

Xreye, xusisa ort lkuhxyoho
Xnevo, tibiba evg tqevxtome

Hiponi iruuw a daehr (6)
Nisoto efiek u koujj (4)

Yumifa uwuey e weoly (1)
Gumoja edoat u maiqt (1)

Digoda avoux i moiky (0)
Sadavu iluic i qeipr (4)

Key Points

  • A vector is a matrix with only one row or column.
  • By combining three matrices for translation, rotation and scale, you can position a model anywhere in the scene.
  • In the resources folder for this chapter, references.markdown suggests further reading to help better understand transformations with linear algebra.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now