UNPKG

9.34 kBTypeScriptView Raw
1import { JSONMeta, Object3DJSON, Object3DJSONObject } from "../core/Object3D.js";
2import { Vector2 } from "../math/Vector2.js";
3import { Camera } from "./Camera.js";
4
5export interface PerspectiveCameraJSONObject extends Object3DJSONObject {
6 fov: number;
7 zoom: number;
8
9 near: number;
10 far: number;
11 focus: number;
12
13 aspect: number;
14
15 view?: {
16 enabled: boolean;
17 fullWidth: number;
18 fullHeight: number;
19 offsetX: number;
20 offsetY: number;
21 width: number;
22 height: number;
23 };
24
25 filmGauge: number;
26 filmOffset: number;
27}
28
29export interface PerspectiveCameraJSON extends Object3DJSON {
30 object: PerspectiveCameraJSONObject;
31}
32
33/**
34 * Camera that uses {@link https://en.wikipedia.org/wiki/Perspective_(graphical) | perspective projection}.
35 * This projection mode is designed to mimic the way the human eye sees
36 * @remarks
37 * It is the most common projection mode used for rendering a 3D scene.
38 * @example
39 * ```typescript
40 * const camera = new THREE.PerspectiveCamera(45, width / height, 1, 1000);
41 * scene.add(camera);
42 * ```
43 * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_blending | animation / skinning / blending }
44 * @see Example: {@link https://threejs.org/examples/#webgl_animation_skinning_morph | animation / skinning / morph }
45 * @see Example: {@link https://threejs.org/examples/#webgl_effects_stereo | effects / stereo }
46 * @see Example: {@link https://threejs.org/examples/#webgl_interactive_cubes | interactive / cubes }
47 * @see Example: {@link https://threejs.org/examples/#webgl_loader_collada_skinning | loader / collada / skinning }
48 * @see {@link https://threejs.org/docs/index.html#api/en/cameras/PerspectiveCamera | Official Documentation}
49 * @see {@link https://github.com/mrdoob/three.js/blob/master/src/cameras/PerspectiveCamera.js | Source}
50 */
51export class PerspectiveCamera extends Camera {
52 /**
53 * Creates a new {@link PerspectiveCamera}.
54 * @remarks Together these define the camera's {@link https://en.wikipedia.org/wiki/Viewing_frustum | viewing frustum}.
55 * @param fov Camera frustum vertical field of view. Default `50`.
56 * @param aspect Camera frustum aspect ratio. Default `1`.
57 * @param near Camera frustum near plane. Default `0.1`.
58 * @param far Camera frustum far plane. Default `2000`.
59 */
60 constructor(fov?: number, aspect?: number, near?: number, far?: number);
61
62 /**
63 * Read-only flag to check if a given object is of type {@link Camera}.
64 * @remarks This is a _constant_ value
65 * @defaultValue `true`
66 */
67 readonly isPerspectiveCamera: true;
68
69 /**
70 * @override
71 * @defaultValue `PerspectiveCamera`
72 */
73 override readonly type: string | "PerspectiveCamera";
74
75 /**
76 * Gets or sets the zoom factor of the camera.
77 * @defaultValue `1`
78 */
79 zoom: number;
80
81 /**
82 * Camera frustum vertical field of view, from bottom to top of view, in degrees.
83 * @remarks Expects a `Float`
84 * @defaultValue `50`
85 */
86 fov: number;
87
88 /**
89 * Camera frustum aspect ratio, usually the canvas width / canvas height.
90 * @remarks Expects a `Float`
91 * @defaultValue `1`, _(square canvas)_.
92 */
93 aspect: number;
94
95 /**
96 * Camera frustum near plane.
97 * @remarks The valid range is greater than `0` and less than the current value of the {@link far | .far} plane.
98 * @remarks Note that, unlike for the {@link THREE.OrthographicCamera | OrthographicCamera}, `0` is **not** a valid value for a {@link PerspectiveCamera |PerspectiveCamera's}. near plane.
99 * @defaultValue `0.1`
100 * @remarks Expects a `Float`
101 */
102 near: number;
103
104 /**
105 * Camera frustum far plane.
106 * @remarks Must be greater than the current value of {@link near | .near} plane.
107 * @remarks Expects a `Float`
108 * @defaultValue `2000`
109 */
110 far: number;
111
112 /**
113 * Object distance used for stereoscopy and depth-of-field effects.
114 * @remarks This parameter does not influence the projection matrix unless a {@link THREE.StereoCamera | StereoCamera} is being used.
115 * @remarks Expects a `Float`
116 * @defaultValue `10`
117 */
118 focus: number;
119
120 /**
121 * Frustum window specification or null.
122 * This is set using the {@link setViewOffset | .setViewOffset} method and cleared using {@link clearViewOffset | .clearViewOffset}.
123 * @defaultValue `null`
124 */
125 view: null | {
126 enabled: boolean;
127 fullWidth: number;
128 fullHeight: number;
129 offsetX: number;
130 offsetY: number;
131 width: number;
132 height: number;
133 };
134
135 /**
136 * Film size used for the larger axis.
137 * This parameter does not influence the projection matrix unless {@link filmOffset | .filmOffset} is set to a nonzero value.
138 * @remarks Expects a `Float`
139 * @defaultValue `35`, _millimeters_.
140 */
141 filmGauge: number;
142
143 /**
144 * Horizontal off-center offset in the same unit as {@link filmGauge | .filmGauge}.
145 * @remarks Expects a `Float`
146 * @defaultValue `0`
147 */
148 filmOffset: number;
149
150 /**
151 * Returns the focal length of the current {@link .fov | fov} in respect to {@link filmGauge | .filmGauge}.
152 */
153 getFocalLength(): number;
154
155 /**
156 * Sets the FOV by focal length in respect to the current {@link filmGauge | .filmGauge}.
157 * @remarks By default, the focal length is specified for a `35mm` (full frame) camera.
158 * @param focalLength Expects a `Float`
159 */
160 setFocalLength(focalLength: number): void;
161
162 /**
163 * Returns the current vertical field of view angle in degrees considering {@link zoom | .zoom}.
164 */
165 getEffectiveFOV(): number;
166
167 /**
168 * Returns the width of the image on the film
169 * @remarks
170 * If {@link aspect | .aspect}. is greater than or equal to one (landscape format), the result equals {@link filmGauge | .filmGauge}.
171 */
172 getFilmWidth(): number;
173
174 /**
175 * Returns the height of the image on the film
176 * @remarks
177 * If {@link aspect | .aspect}. is less than or equal to one (portrait format), the result equals {@link filmGauge | .filmGauge}.
178 */
179 getFilmHeight(): number;
180
181 /**
182 * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction.
183 * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle.
184 */
185 getViewBounds(distance: number, minTarget: Vector2, maxTarget: Vector2): void;
186
187 /**
188 * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction.
189 * Copies the result into the target Vector2, where x is width and y is height.
190 */
191 getViewSize(distance: number, target: Vector2): Vector2;
192
193 /**
194 * Sets an offset in a larger frustum.
195 * @remarks
196 * This is useful for multi-window or multi-monitor/multi-machine setups.
197 *
198 * For example, if you have 3x2 monitors and each monitor is _1920x1080_ and
199 * the monitors are in grid like this
200 * ```
201 * ┌───┬───┬───┐
202 * │ A │ B │ C │
203 * ├───┼───┼───┤
204 * │ D │ E │ F │
205 * └───┴───┴───┘
206 * ```
207 * then for each monitor you would call it like this
208 * ```typescript
209 * const w = 1920;
210 * const h = 1080;
211 * const fullWidth = w * 3;
212 * const fullHeight = h * 2;
213 *
214 * // Monitor - A
215 * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
216 * // Monitor - B
217 * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
218 * // Monitor - C
219 * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
220 * // Monitor - D
221 * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
222 * // Monitor - E
223 * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
224 * // Monitor - F
225 * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
226 * ```
227 * Note there is no reason monitors have to be the same size or in a grid.
228 * @param fullWidth Full width of multiview setup Expects a `Float`.
229 * @param fullHeight Full height of multiview setup Expects a `Float`.
230 * @param x Horizontal offset of subcamera Expects a `Float`.
231 * @param y Vertical offset of subcamera Expects a `Float`.
232 * @param width Width of subcamera Expects a `Float`.
233 * @param height Height of subcamera Expects a `Float`.
234 */
235 setViewOffset(fullWidth: number, fullHeight: number, x: number, y: number, width: number, height: number): void;
236
237 /**
238 * Removes any offset set by the {@link setViewOffset | .setViewOffset} method.
239 */
240 clearViewOffset(): void;
241
242 /**
243 * Updates the camera projection matrix
244 * @remarks Must be called after any change of parameters.
245 */
246 updateProjectionMatrix(): void;
247
248 /**
249 * @deprecated Use {@link PerspectiveCamera.setFocalLength | .setFocalLength()} and {@link PerspectiveCamera.filmGauge | .filmGauge} instead.
250 */
251 setLens(focalLength: number, frameHeight?: number): void;
252
253 toJSON(meta?: JSONMeta): PerspectiveCameraJSON;
254}