What a 4×4 Matrix Actually Does to a Point
Back in June, the screen post wrote the view-projection multiply out by hand, _vp[0]*_x + _vp[4]*_y + ..., called it “a 4×4 matrix times a 4-component vector” and moved on. I owed an explanation of those lines, and this is it. You need to know what a point with three coordinates is and how to multiply two numbers. Every number below was printed by a script that asked three.js itself (version 0.187.0), because I did the arithmetic by hand first and wanted the library to check me rather than the other way round.
A matrix is a recipe with one column per input
Take a 2×2 matrix that turns a flat point (x, y) into a new point. Write its four numbers as two columns, one for each axis. Multiplying the matrix by the point gives:
output = x × (column 1) + y × (column 2)
That is the whole rule, and it reads as “each column is where one input axis goes”. Column 1 says where the x axis ends up, column 2 where the y axis ends up. Put x = 1, y = 0 in and you get column 1 back. Put in (2, 3) and you get twice column 1 plus three times column 2.
A 4×4 matrix works the same way with four columns. The output is
x × (column 1) + y × (column 2) + z × (column 3) + 1 × (column 4)
The multiplier on the last column is 1, not a coordinate. That is the first thing the GML’s indices tell us. Three.js stores the sixteen numbers in one flat array, elements, column by column. Its documentation says so, and the printed array confirms it: the first four numbers are column 1, the next four column 2, and so on. So clip x, the first row of the output, is built from elements[0], elements[4], elements[8] and elements[12]. Those are the indices in _vp[0]*_x + _vp[4]*_y + _vp[8]*_z + _vp[12]. The GML is not picking numbers at random: it is reading the matrix column by column.
Why a point needs a fourth number
Try a 3×3 matrix with the same rule. The output is x × col1 + y × col2 + z × col3. Put in the point (0, 0, 0), the origin, and every term is zero. So the origin always stays at the origin. A 3×3 matrix can turn a shape, stretch it or shear it, but it can never slide it. Moving a point needs an offset added on, and an offset is not a multiple of any of the three columns.
The trick is to give every point a fourth number, which we call w, and set it to 1. The point (x, y, z) becomes (x, y, z, 1). Now the fourth column is multiplied by 1 and simply added: that is the translation. The rule x × col1 + y × col2 + z × col3 + 1 × col4 is the same rule as before, with a fourth column doing the sliding.
Directions use w = 0 instead. A direction is a difference between two points, and sliding a difference makes no sense, so the fourth column is multiplied by 0 and vanishes. One matrix can therefore move points and turn directions correctly, and the w number tells it which is which.
The view matrix: moving the world to the camera
The viewer’s camera lives in src/viewer/threeCamera.ts. placeCamera puts the camera at the orbit’s eye with camera.position.set and points it at the tree with camera.lookAt. Three.js turns that position and aim into a matrix. The camera keeps the inverse of its own world matrix in matrixWorldInverse (see Camera.js in three’s source), and that inverse is the view matrix. It moves the whole world so that the camera sits at the origin.
After that move, the camera looks down the negative z axis, as three cameras do by default. So a point in front of the camera has a negative z, and a point behind it has a positive one. That is why projectToScreen in src/viewer/projection.ts gives up when the point’s view-space z is at least zero:
if (point.clone().applyMatrix4(camera.matrixWorldInverse).z >= 0) return undefined;
The viewer also mirrors y first, through toViewerSpace in src/viewer/camera.ts, so the numbers in the next section are already in the viewer’s mirrored space. The worked point is a point 2.5 units in front of the camera:
view space: (0.5, 0.25, −2.5)
The view matrix has already been applied to that point. From here on, only the projection matrix is left.
The projection matrix, worked with the real numbers
For the viewer’s camera, FOV_DEGREES is 60, NEAR is 0.01, FAR is 20, and the viewport is 1600 × 900, all from src/viewer/camera.ts and the test in src/viewer/projection.test.ts. Three builds this projection matrix, printed here as rows:
| Row | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| 1 | 0.9742786 | 0 | 0 | 0 |
| 2 | 0 | 1.7320508 | 0 | 0 |
| 3 | 0 | 0 | −1.0010005 | −0.0200100 |
| 4 | 0 | 0 | −1 | 0 |
The first two rows do the scaling. The number 1.7320508 is f = 1 / tan(30°), where 30° is half the 60° field of view. Row 2 is f times y. Row 1 is f / aspect, where the aspect is 1600 / 900 = 1.7777778, so it is 0.9742786. The horizontal scale is smaller by the aspect ratio, so the vertical field of view stays at 60° and the horizontal one is wider, as a wide screen needs.
Now run the worked point through it. Clip x is 0.9742786 × 0.5 = 0.4871393 and clip y is 1.7320508 × 0.25 = 0.4330127. Those two are just the scaled x and y. The interesting row is the fourth.
Row 4 is (0, 0, −1, 0). It takes the point’s z, multiplies it by −1, and writes the result into w. The point’s z is −2.5, so clip w is 2.5. Clip z comes from row 3, and works out to 2.4824912. So the homogeneous point is:
clip = (0.4871393, 0.4330127, 2.4824912, 2.5)
This is the step I want to point at. The matrix does not divide anything. It writes the division’s denominator into w, and the division comes after. The perspective divide is dividing by w, which is −z, which is the point’s depth in front of the camera along the view axis. It is not the straight-line distance from the eye: a point far off to the side has a larger distance than its depth. The depth is what shrinks things, because an object twice as deep has its x and y halved.
The divide itself is one line for each coordinate:
- x: 0.4871393 / 2.5 = 0.1948557
- y: 0.4330127 / 2.5 = 0.1732051
- z: 2.4824912 / 2.5 = 0.9929965
Those are normalised device coordinates, NDC, which run from −1 to +1 across the screen. projectToScreen maps them to CSS pixels with two lines:
return { x: ((ndc.x + 1) / 2) * width, y: ((1 - ndc.y) / 2) * height };
The first line turns −1…+1 into 0…1 and then into 0…1600 pixels. Here that is ((0.1948557 + 1) / 2) × 1600 = 955.88. The second line does the same for y but flips it, because NDC’s y points up and screen y points down: ((1 − 0.1732051) / 2) × 900 = 372.06. The point lands at about (955.88, 372.06), which is a little right of centre and a little above the middle. The script’s screen formula gives the same two numbers to seven decimals.
Depth is the third row
Row 3 also produces the z that the viewer uses for the far-plane check. Divide clip z by w and you get
NDC z = 1.0010005 − 0.0200100 / d
where d is the depth in front of the camera. This is a curve in 1/d, not a straight line in d. Here are the values from the script, which uses three’s own applyMatrix4 on a point at each depth:
| Depth d | NDC z |
|---|---|
| 0.01 (near plane) | −1.0000000 |
| 0.1 | 0.8009005 |
| 0.8 | 0.9759880 |
| 2.5 (the default camera distance) | 0.9929965 |
| 6 | 0.9976655 |
| 20 (far plane) | 1.0000000 |
My hand value at the near plane is −0.9999995, because I rounded the constants to seven places. Three gives exactly −1. The rest agree to the digits shown.
Look at where the range goes. From the near plane to a depth of 0.1 the value climbs from −1 to 0.80, which is 1.8 of the 2 units. The next 19.9 units of depth, from 0.1 out to the far plane, use only the last 0.2. Most of the depth range is spent right next to the camera. That is what a 1/d curve does, and it explains two of the viewer’s choices.
It explains why the near plane is not zero. At d = 0 the term 0.0200100 / d divides by zero, so the formula has no value there. The near plane is the smallest depth the renderer will draw, here 0.01, and the formula is well behaved everywhere past it.
And it explains the far-plane test. Points past FAR have an NDC z above 1, and projectToScreen returns nothing when ndc.z > 1. The value 1 is exactly the far plane, so the check is a comparison with one fixed number rather than a separate distance test.
What I take from it
A 4×4 matrix is a recipe with one column for each input, and the fourth column, reached through a w of 1, is what makes it a move as well as a turn. The view matrix is three’s inverse of the camera’s pose, and the projection matrix is almost all constants, with the one clever row, (0, 0, −1, 0), that copies the depth into w so that the divide happens in the right place. The perspective divide is dividing by how far in front of the eye the point is, and the depth curve it leaves behind is why most of the depth range belongs to things close to the camera.
The useful habit is the one from the screen post, now at a finer grain. When a point comes out in the wrong place, check which row produced which number, and check the column order before you blame the arithmetic. The GML’s indices were in the right order all along.
Coming next
Storing a camera as three angles: why the orbit camera keeps yaw, pitch and distance instead of an (x, y, z) eye position, how dcos and dsin turn three angles into a point on a sphere, the cos(pitch) shrink that tucks the camera overhead, and the edge cases at the poles. The eye position it produces is the cameraEye in src/viewer/camera.ts that feeds the view matrix above.