Lesson 21 - Orthographic Projections

Introduction

This lesson is about orthographic projections. Orthographic projection is a means of representing a three-dimensional object in two dimensions. It is a form of parallel projection, where all the projection lines are orthogonal to the projection plane, resulting in every plane of the scene appearing in an affine transformation on the viewing surface.

There are many types of projections, including orthographic, perspective, isometric, oblique as well as the complex geographic projections that map a curved surface onto a 2D surface. Three.js supports both the common perspective transformation (which we have been using throughout these lessons up to now) as well as orthographic.

Orthographic projections have the important quality that the objects in the projection all have the same dimensions, no matter the distance from the obsever. Consider this diagram:

Note that in both "scenes" the red and yellow balls are actually the same size, but in the perspective projection, the red ball is distinctly smaller. This seems natural to us, as our vision is essentially a perspective projection onto the back of our eyeball. Things in the distance are smaller, are they not? But note that in the right hand figure, the red ball is still the same size as the yellow ball.

Orthographic projection is widely used in the engineering industry, as depicted in this diagram:

Note that the left-hand 3D representation is also an orthographic projection. Most often, an orthographic projection is in 2D (e.g. architectural drawings, etc.) but orthographic projections can also be in 3D, which is the subject of this lesson. So let's look at the lesson.

Perspective and Orthographic Projections

Most of this lesson is actually about changes made in GFXScene to support both perspective and orthographic projections. Let's first review perspective projections.

Where fov is the field of view (the angle of the view, as shown in the diagram above). The default in GFXScene is 45 degrees. The next parameter is the aspect ratio of the view. and the last two parameters are the near and far clipping planes. Pretty straighforward (though there is a lot of gnarly matrix math in three.js hidden under the covers).

Where orthoSize is the height of the viewport in user-units, i.e. in the units we want to draw in, as opposed to screen coordinates. So we're just setting up a rectangle in user-units that the projection will be mapped into. Again, pretty straightforward (ignoring all that gnarly matrix math :-). Let's see how it is used in practice.

The keys here are that we pass in perspective:false, telling the scene that we an orthographic camera. Set the orthoSize to the size of the "floor" we are setting up. We set the camera position to a reasonable value to the right and back from the floor. We set some reasonable values for the (checkerboard) floor and that's it for instantiation.

The first block checks if the user passed in parameters (as do above in the keyhandler) and parses the parameters out and stores them in the GFXScene object. If perspective is true (the default) we set up the camera as we have done before. But if the camera is orthographic we have to calculate the bounds of the sides of the camera, which is just orthoSize adjusted for the aspect ratio of the window. We then telll three.js to update the projection matrix, then set the camera's position and what it is looking at. Last but not least, note that we have to update the orbitControls since we have changed the camera.

Handling Window Resizing

Finally, it's worth noting that handling of the resizing of the window has to take into account what type of projection is being used:

Note that the use of _self is because an anonymous function like this doesn't have access to GFXScene's this. Instead, this in the anonymous function would mean the window's this, which is definitely not what we want. Otherwise, this code mirrors the code that sets up the cameras in the first place.

And that's it! Click on this link to see the actual rendered demo in all it's orthographic glory!