Graphics Reference
In-Depth Information
Figure 5.8
A view rotated 45 degrees around the Y axis
It doesn't really look like the view has been rotated at all; it looks more like it's been
horizontally squashed using a scale transform. Did we do something wrong?
No, this is actually correct. The view looks narrower because we are looking at it
diagonally, so there is less of it facing the camera. The reason it doesn't look right is
because there is no
perspective.
Perspective Projection
In real life, when things are farther away, they seem to get smaller due to perspective. We
would expect the side of the view that is farther away from us to appear shorter than the
side that is closer, but that isn't happening. What we are looking at currently is an
isometric
projection
of our view, which is a method of 3D drawing that preserves parallel lines, much
like the affine transforms we were using earlier.
In an isometric projection, objects that are farther away appear at the same scale as objects
that are close to us. This kind of projection has its uses (for example, for an architectural
drawing, or a top-down, pseudo-3D videogame), but it's not what we want right now.
To fix this, we need to modify our transform matrix to include a
perspective transform
(sometimes called the
z transform
) in addition to the rotation transform we've already
applied. Core Animation doesn't give us any functions to set up a perspective transform, so
we'll have to modify our matrix values manually. Fortunately, though, this is simple:
The perspective effect of a
CATransform3D
is controlled by a single value in the matrix:
element
m34
. The
m34
value (shown in Figure 5.9) is used in the transform calculation to
scale the X and Y values in proportion to how far away they are from the camera.