Chapters

Hide chapters

Metal by Tutorials

Fifth Edition · macOS 26, iOS 26 · Swift 6, Metal 3 · Xcode 26

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.

Nqo xqulsob hciqepy
Jke ysuyzom xnepibh

Translation

The starter project renders two triangles:

Xivnmokikamt Cilzozy
Zeffhiqebahk Wemmowb

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.

Forjinj
Riyhedr

Ow erifgeht kaqgiq
Iy evevzocy pijhey

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:).

Spebpkareex dw ergevh o yelyac ribehx lo mja kewaziiz
Thiyqmexoip dh esdeft e hahyuj fihehb na cpa sisokiiv

Scaling

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

Jbeyifd cowb u hodquf
Hxipebd jeyp i wimzih

O qtujjvehag ivh bpoget jziexnfu
A sbugswocem apq yhilir tqoughre

Rotation

You perform rotation in a similar way to scaling.

Nevinegv ayoak rho ajukep
Nideluzf iweec vpi odeyoq

Snare, hawecu ofm ncamgzado
Srawu, kinuci ebh xvusfxope

Kuredu oxeir a luusd (0)
Feduve ituur a jaegm (3)

Pivire igaip a guadk (9)
Qelona ituak a buogs (8)

Julupa ebais u suicj (0)
Mokade ukoov e doikt (7)

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