Sets a perspective projection for the camera.
In a perspective projection, shapes that are further from the camera appear smaller than shapes that are near the camera. This technique, called foreshortening, creates realistic 3D scenes. It’s applied by default in new p5.Camera
objects.
myCamera.perspective()
changes the camera’s perspective by changing its viewing frustum. The frustum is the volume of space that’s visible to the camera. The frustum’s shape is a pyramid with its top cut off. The camera is placed where the top of the pyramid should be and points towards the base of the pyramid. It views everything within the frustum.
The first parameter, fovy
, is the camera’s vertical field of view. It’s an angle that describes how tall or narrow a view the camera has. For example, calling myCamera.perspective(0.5)
sets the camera’s vertical field of view to 0.5 radians. By default, fovy
is calculated based on the sketch’s height and the camera’s default z-coordinate, which is 800. The formula for the default fovy
is 2 * atan(height / 2 / 800)
.
The second parameter, aspect
, is the camera’s aspect ratio. It’s a number that describes the ratio of the top plane’s width to its height. For example, calling myCamera.perspective(0.5, 1.5)
sets the camera’s field of view to 0.5 radians and aspect ratio to 1.5, which would make shapes appear thinner on a square canvas. By default, aspect
is set to width / height
.
The third parameter, near
, is the distance from the camera to the near plane. For example, calling myCamera.perspective(0.5, 1.5, 100)
sets the camera’s field of view to 0.5 radians, its aspect ratio to 1.5, and places the near plane 100 pixels from the camera. Any shapes drawn less than 100 pixels from the camera won’t be visible. By default, near
is set to 0.1 * 800
, which is 1/10th the default distance between the camera and the origin.
The fourth parameter, far
, is the distance from the camera to the far plane. For example, calling myCamera.perspective(0.5, 1.5, 100, 10000)
sets the camera’s field of view to 0.5 radians, its aspect ratio to 1.5, places the near plane 100 pixels from the camera, and places the far plane 10,000 pixels from the camera. Any shapes drawn more than 10,000 pixels from the camera won’t be visible. By default, far
is set to 10 * 800
, which is 10 times the default distance between the camera and the origin.
Examples
Syntax
perspective([fovy], [aspect], [near], [far])
Parameters
camera frustum vertical field of view. Defaults to 2 * atan(height / 2 / 800)
.
camera frustum aspect ratio. Defaults to width / height
.
distance from the camera to the near clipping plane. Defaults to 0.1 * 800
.
distance from the camera to the far clipping plane. Defaults to 10 * 800
.