In threejs there are a few cameras to work with, typically in most cases I would use the perspective camera, however there is also the orthographic camera. With this orthographic camera an object size will remain the same regardless of this distance in which the object is from the camera, as compared to the perspective camera which will change the size as the distance from the camera goes up.
I often do use the perspective camera as with most of the projects I work on I want to use a camera that works like that of the human eye, but the main other camera of interest outside of that would be the orthographic camera first and foremost. With that said in this post I will be writing about the orthographic camera, and how it compares to the perspective camera. There might be a few situations in which I might want to use this kind of camera here and there. In any case thought it is also a good idea to work out at least a few examples with a camera option other than the perspective camera just for the sake of getting a little more solid with what there is to work with in threejs.
In most situations the Orthographic Camera may not be the best choice for a project so there is looking into what the other options are with cameras. For most typical projects I would go with the Perspective camera that reproduces the way that the human eye sees.
There is also reading up more on the base camera class in threejs that contains what there is to worth with across all types of cameras. There are properties in the base class like isCamera as well as methods like the get world direction. On top of the base camera class there is also the object3d class that is not only a base class of the camera class, but also a base class of many objects in threejs including mesh objects, groups, and even whole scene objects. So when it comes to doing something like setting the position of a camera, the same can also be done with mesh objects, and anything else that is based off of the object3d class.
The source code examples in this post can be found on my test threejs repository. So if you would like to make a pull request, or just pull down this source code along with the source code for all my other blog posts on threejs that would be worth checking out. This is also the repo where I store the source code examples and additional assets for all my other blog posts on threejs as well.
When I first wrote this post back in May of 2018 I as using revision r91 of three.js. As of this writing I was using r146 of threejs last time I came around to doing a little editing with this post as well. It would seem that not to much has change with cameras over that time, at least not with the camera options that I have been sticking with. However that does not mean that code breaking changes are not made to the library often, as that is indeed the case. If any example here is not working be sure to check what version you are using first.
First off it would be best to just start out with a few simple getting started type examples with the orthographic camera. So in this Section I will be doing just that where the main focus will just be on some very simple hello world type code examples where this type of camera is being used. The main focus here in this section then will just be on the THREE.OrthographicCamera constructor itself then, and with the options that one will need to be aware of when calling it to create an instance of this kind of camera in the first place. After that there is maybe just a few more demos that have to do with various basic features of this kind of camera, and also object3d based objects in general when it comes to things like moving the camera around and so forth.
In this example I am just creating an instance of the orthographic camera with the THREE.OrthographicCamera constructor function, and storing the returned instance of the camera to a variable called camera. When doing so the set of arguments that I pass to the constructor will differ a little from the usual perspective camera constructor as the arguments are used to define a box like area. In other words in place of values that have to do with field of view, aspect ratio and so forth there are values for setting the left, right, top, and bottom values of a box like shape in space rather than that of a pyramid. After that there is just setting the near and far render distance values just like with the perspective camera to define a render distance as always.
Once I have my instance of the orthographic camera I can use camera and object3d base classes and properties as with any other camera. For example I can use the instance of vector3 stored at the position property to set the position of the camera in space, and the look at method as a way to set rotation both of which are object3d features.
After setting up the camera the way I want it I then created a scene object, added a mesh to look at and also set up the renderer that i want to use for the example. I can now use the render method of the renderer with a scene object, and my camera object to draw the contents of the scene using the orthographic camera.
I have wrote a main blog post on the subject of moving a camera around, however what this really about is the position property of object3d class based objects in general. It would be best to read those posts if you really want to learn a thing or two about moving cameras, and object3d class based objects in general around. However I will of course write about a basic example of this sort of thing here.
The position property of the camera stores a an instance of the vector3 class as the value, and the state of this vector3 objects is very much the current position. There are a whole lot of methods in the vector3 class that are useful for updating the position of the camera over time. However there is also just working our some expressions for a given axis value like I am doing here.
In this example I am just using the Orthographic Camera alone to look at a random cube stack module instance thing that I made for this post alone. This cube stack thing resembles a small cityscape or something to that effect, but it is really just to have something that is composed mainly of a bunch of cubes that are all the same uniform size.
In order to appreciate the differences between the orthographic, and perspective cameras in three.js I will first need some kind of scene that will help express that well. So that being said I made a model that I think will help make a scene that can do that very well.
So I have this saved as cube_stack.js, and I link to it in my html after three.js, and before the rest of my demo where I will be using this. I could get into this in detail, and yes it is far from perfect, but that would be off topic. For now if three.js is loaded in the browser, and then this is loaded I will have a constructor that I can use in a three.js demo like this:
I could have it be a lot more that what it is but for the purpose of the subject of this post by doing this it will just add a group that contains a bunch of Mesh Object instances positioned in a way that might resemble a city. The reason why this is of interest comes when switching between the kinds of cameras used to view something like this.
After that I can use set method of the Vector3 instance of the position property of the camera to set the position, and I also like to use the lookAt method as a way to set the orientation of the camera. If I set a static value like the zoom value of the camera I will need to call the update projection matrix method which is also the case with the perspective camera that I am used to. In this example I have chose to add a light source relative to the camera in the form of a point light, as such in order for that light source to work in a scene I am going to want to add the camera object as a whole to the scene.
I then created and added to the scene an instance of this cube stack model that I made. After that is set and done I set up and append to the html an instance of a WebGl renderer. I am then going to want to have a main animation loop in which I will be changing the position of the camera and make it so that the camera will orbit around this cube stack module.
This cube stack modules that I made is kind of cool, but there is a lot more that I would like to add to it and fix. I will also want to make a demo that will show the difference between the two most common camera types by comparing what is rendered using an orthographic camera to that of a perspective camera. To do this I will want to use more than one camera in my demo by having an array of cameras rather than just one camera and switch between the two as part of an update loop.
In this new cube stack module I made a whole bunch of little fixes and changes when it comes to arguments and how the position of cubes is adjusted. However one of the coolest changes that I have made is to make use of data textures as a way to add at least a little texture to the mesh objects that I am adding to each stack of cubes.
When all goes well this will result in a rotating scene that looks like a bunch of buildings. The example will switch between camera 0 (perspective), and camera 1 (orthographic). If you take the time to reproduce this you will notice that it is easier to tell what the size is of things.
Just like that of the perspective camera there is also an update projection matrix method of the camera that I will want to call when changing any of the values that are set when calling the constructor for the first time. So if I want to change the left, right, top, bottom, near, or far values in an update loop I will want to call this function after making the changes or else what I have done will not take effect.
In just about all threejs projects I am typicality going to want to go with the perspective camera actually when it comes to features of threejs that I am actually using most of the time. Still if I am going to use a camera other that the perspective camera I would say that the orthographic camera is at the top if the list. It does result in a cool kind of visual look that I think will also prove to be useful for certain games, and animations that I might choose to make from time to time.
However in any case I think the coolest thing that I made while working on this post was this cube stack module that I might want to come back to again and again every once in a while. In fact I think it might be a good idea to crate another threejs project example in which I am creating and working with a grid of these with different settings when it comes to the count of cube in each cube stack.