1 | /**
|
---|
2 | * @author mrdoob / http://mrdoob.com/
|
---|
3 | * @author greggman / http://games.greggman.com/
|
---|
4 | * @author zz85 / http://www.lab4games.net/zz85/blog
|
---|
5 | */
|
---|
6 |
|
---|
7 | THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
|
---|
8 |
|
---|
9 | THREE.Camera.call( this );
|
---|
10 |
|
---|
11 | this.fov = fov !== undefined ? fov : 50;
|
---|
12 | this.aspect = aspect !== undefined ? aspect : 1;
|
---|
13 | this.near = near !== undefined ? near : 0.1;
|
---|
14 | this.far = far !== undefined ? far : 2000;
|
---|
15 |
|
---|
16 | this.updateProjectionMatrix();
|
---|
17 |
|
---|
18 | };
|
---|
19 |
|
---|
20 | THREE.PerspectiveCamera.prototype = Object.create( THREE.Camera.prototype );
|
---|
21 |
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Uses Focal Length (in mm) to estimate and set FOV
|
---|
25 | * 35mm (fullframe) camera is used if frame size is not specified;
|
---|
26 | * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
|
---|
27 | */
|
---|
28 |
|
---|
29 | THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
|
---|
30 |
|
---|
31 | if ( frameHeight === undefined ) frameHeight = 24;
|
---|
32 |
|
---|
33 | this.fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) );
|
---|
34 | this.updateProjectionMatrix();
|
---|
35 |
|
---|
36 | }
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Sets an offset in a larger frustum. This is useful for multi-window or
|
---|
41 | * multi-monitor/multi-machine setups.
|
---|
42 | *
|
---|
43 | * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
|
---|
44 | * the monitors are in grid like this
|
---|
45 | *
|
---|
46 | * +---+---+---+
|
---|
47 | * | A | B | C |
|
---|
48 | * +---+---+---+
|
---|
49 | * | D | E | F |
|
---|
50 | * +---+---+---+
|
---|
51 | *
|
---|
52 | * then for each monitor you would call it like this
|
---|
53 | *
|
---|
54 | * var w = 1920;
|
---|
55 | * var h = 1080;
|
---|
56 | * var fullWidth = w * 3;
|
---|
57 | * var fullHeight = h * 2;
|
---|
58 | *
|
---|
59 | * --A--
|
---|
60 | * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
|
---|
61 | * --B--
|
---|
62 | * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
|
---|
63 | * --C--
|
---|
64 | * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
|
---|
65 | * --D--
|
---|
66 | * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
|
---|
67 | * --E--
|
---|
68 | * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
|
---|
69 | * --F--
|
---|
70 | * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
|
---|
71 | *
|
---|
72 | * Note there is no reason monitors have to be the same size or in a grid.
|
---|
73 | */
|
---|
74 |
|
---|
75 | THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
|
---|
76 |
|
---|
77 | this.fullWidth = fullWidth;
|
---|
78 | this.fullHeight = fullHeight;
|
---|
79 | this.x = x;
|
---|
80 | this.y = y;
|
---|
81 | this.width = width;
|
---|
82 | this.height = height;
|
---|
83 |
|
---|
84 | this.updateProjectionMatrix();
|
---|
85 |
|
---|
86 | };
|
---|
87 |
|
---|
88 |
|
---|
89 | THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
|
---|
90 |
|
---|
91 | if ( this.fullWidth ) {
|
---|
92 |
|
---|
93 | var aspect = this.fullWidth / this.fullHeight;
|
---|
94 | var top = Math.tan( THREE.Math.degToRad( this.fov * 0.5 ) ) * this.near;
|
---|
95 | var bottom = -top;
|
---|
96 | var left = aspect * bottom;
|
---|
97 | var right = aspect * top;
|
---|
98 | var width = Math.abs( right - left );
|
---|
99 | var height = Math.abs( top - bottom );
|
---|
100 |
|
---|
101 | this.projectionMatrix.makeFrustum(
|
---|
102 | left + this.x * width / this.fullWidth,
|
---|
103 | left + ( this.x + this.width ) * width / this.fullWidth,
|
---|
104 | top - ( this.y + this.height ) * height / this.fullHeight,
|
---|
105 | top - this.y * height / this.fullHeight,
|
---|
106 | this.near,
|
---|
107 | this.far
|
---|
108 | );
|
---|
109 |
|
---|
110 | } else {
|
---|
111 |
|
---|
112 | this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | };
|
---|
117 |
|
---|
118 | THREE.PerspectiveCamera.prototype.clone = function () {
|
---|
119 |
|
---|
120 | var camera = new THREE.PerspectiveCamera();
|
---|
121 |
|
---|
122 | THREE.Camera.prototype.clone.call( this, camera );
|
---|
123 |
|
---|
124 | camera.fov = this.fov;
|
---|
125 | camera.aspect = this.aspect;
|
---|
126 | camera.near = this.near;
|
---|
127 | camera.far = this.far;
|
---|
128 |
|
---|
129 | return camera;
|
---|
130 | };
|
---|