PerspectiveCamera.js 5.4 KB
Newer Older
M
Mr.doob 已提交
1
/**
M
Mr.doob 已提交
2
 * @author mrdoob / http://mrdoob.com/
M
Mr.doob 已提交
3
 * @author greggman / http://games.greggman.com/
4
 * @author zz85 / http://www.lab4games.net/zz85/blog
T
tschw 已提交
5
 * @author tschw
M
Mr.doob 已提交
6 7
 */

T
tschw 已提交
8
THREE.PerspectiveCamera = function( fov, aspect, near, far ) {
M
Mr.doob 已提交
9 10 11

	THREE.Camera.call( this );

12 13
	this.type = 'PerspectiveCamera';

T
tschw 已提交
14
	this.fov = fov !== undefined ? fov : 50;
15 16
	this.zoom = 1;

17 18
	this.near = near !== undefined ? near : 0.1;
	this.far = far !== undefined ? far : 2000;
T
tschw 已提交
19 20 21 22 23 24 25
	this.focus = 10;

	this.aspect = aspect !== undefined ? aspect : 1;
	this.view = null;

	this.filmGauge = 35;	// width of the film (default in millimeters)
	this.filmOffset = 0;	// horizontal film offset (same unit as gauge)
M
Mr.doob 已提交
26 27 28 29 30

	this.updateProjectionMatrix();

};

T
tschw 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
THREE.PerspectiveCamera._assignProps = function( dest, source ) {

	dest.fov = source.fov;
	dest.zoom = source.zoom;

	dest.near = source.near;
	dest.far = source.far;
	dest.focs = source.focus;

	dest.aspect = source.aspect;
	dest.view = ! source.view ? null : Object.assign( {}, source.view );

	dest.filmGauge = source.filmGauge;
	dest.filmOffset = source.filmOffset;

};

48
THREE.PerspectiveCamera.prototype = Object.create( THREE.Camera.prototype );
49
THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
M
Mr.doob 已提交
50 51


52
/**
T
tschw 已提交
53 54 55
 * Sets the FOV by focal length (DEPRECATED).
 *
 * Optionally also sets .filmGauge, otherwise uses it. See .setFocalLength.
56
 */
T
tschw 已提交
57 58 59 60
THREE.PerspectiveCamera.prototype.setLens = function( focalLength, filmGauge ) {

	console.warn( "THREE.PerspectiveCamera.setLens is deprecated. " +
			"Use .setFocalLength and .filmGauge for a photographic setup." );
M
Mr.doob 已提交
61

T
tschw 已提交
62 63
	if ( filmGauge !== undefined ) this.filmGauge = filmGauge;
	this.setFocalLength( focalLength );
M
Mr.doob 已提交
64

T
tschw 已提交
65
};
M
Mr.doob 已提交
66

T
tschw 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * Sets the FOV by focal length in respect to the current .filmGauge.
 *
 * The default film gauge is 35, so that the focal length can be specified for
 * a 35mm (full frame) camera.
 *
 * Values for focal length and film gauge must have the same unit.
 */
THREE.PerspectiveCamera.prototype.setFocalLength = function( focalLength ) {

	// see http://www.bobatkins.com/photography/technical/field_of_view.html
	var vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;

	this.fov = THREE.Math.RAD2DEG * 2 * Math.atan( vExtentSlope );
81
	this.updateProjectionMatrix();
M
Mr.doob 已提交
82

B
brason 已提交
83
};
M
Mr.doob 已提交
84 85


T
tschw 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
/**
 * Calculates the focal length from the current .fov and .filmGauge.
 */
THREE.PerspectiveCamera.prototype.getFocalLength = function() {

	var vExtentSlope = Math.tan( THREE.Math.DEG2RAD * 0.5 * this.fov );

	return 0.5 * this.getFilmHeight() / vExtentSlope;

};

THREE.PerspectiveCamera.prototype.getEffectiveFOV = function() {

	return THREE.Math.RAD2DEG * 2 * Math.atan(
			Math.tan( THREE.Math.DEG2RAD * 0.5 * this.fov ) / this.zoom );

};

THREE.PerspectiveCamera.prototype.getFilmWidth = function() {

	// film not completely covered in portrait format (aspect < 1)
	return this.filmGauge * Math.min( this.aspect, 1 );

};

THREE.PerspectiveCamera.prototype.getFilmHeight = function() {

	// film not completely covered in landscape format (aspect > 1)
	return this.filmGauge / Math.max( this.aspect, 1 );

};



M
Mr.doob 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132
/**
 * Sets an offset in a larger frustum. This is useful for multi-window or
 * multi-monitor/multi-machine setups.
 *
 * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
 * the monitors are in grid like this
 *
 *   +---+---+---+
 *   | A | B | C |
 *   +---+---+---+
 *   | D | E | F |
 *   +---+---+---+
 *
133
 * then for each monitor you would call it like this
M
Mr.doob 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
 *
 *   var w = 1920;
 *   var h = 1080;
 *   var fullWidth = w * 3;
 *   var fullHeight = h * 2;
 *
 *   --A--
 *   camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
 *   --B--
 *   camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
 *   --C--
 *   camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
 *   --D--
 *   camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
 *   --E--
 *   camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
 *   --F--
 *   camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
 *
 *   Note there is no reason monitors have to be the same size or in a grid.
 */
T
tschw 已提交
155
THREE.PerspectiveCamera.prototype.setViewOffset = function( fullWidth, fullHeight, x, y, width, height ) {
M
Mr.doob 已提交
156

T
tschw 已提交
157
	this.aspect = fullWidth / fullHeight;
M
Mr.doob 已提交
158

T
tschw 已提交
159 160 161 162 163 164 165 166
	this.view = {
		fullWidth: fullWidth,
		fullHeight: fullHeight,
		offsetX: x,
		offsetY: y,
		width: width,
		height: height
	};
M
Mr.doob 已提交
167 168 169 170

	this.updateProjectionMatrix();

};
171

T
tschw 已提交
172
THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function() {
173

T
tschw 已提交
174 175 176 177 178 179 180
	var near = this.near,
		top = near * Math.tan(
				THREE.Math.DEG2RAD * 0.5 * this.fov ) / this.zoom,
		height = 2 * top,
		width = this.aspect * height,
		left = -0.5 * width,
		view = this.view;
181

T
tschw 已提交
182
	if ( view !== null ) {
183

T
tschw 已提交
184 185
		var fullWidth = view.fullWidth,
			fullHeight = view.fullHeight;
186

T
tschw 已提交
187 188 189 190
		left += view.offsetX * width / fullWidth;
		top -= view.offsetY * height / fullHeight;
		width *= view.width / fullWidth;
		height *= view.height / fullHeight;
191

T
tschw 已提交
192
	}
193

T
tschw 已提交
194 195
	var skew = this.filmOffset;
	if ( skew !== 0 ) left += near * skew / this.getFilmWidth();
196

T
tschw 已提交
197 198
	this.projectionMatrix.makeFrustum(
			left, left + width, top - height, top, near, this.far );
199 200

};
S
Slayvin 已提交
201

T
tschw 已提交
202
THREE.PerspectiveCamera.prototype.copy = function( source ) {
M
Mr.doob 已提交
203

204
	THREE.Camera.prototype.copy.call( this, source );
T
tschw 已提交
205
	THREE.PerspectiveCamera._assignProps( this, source );
206
	return this;
M
Mr.doob 已提交
207

S
Slayvin 已提交
208
};
209

T
tschw 已提交
210
THREE.PerspectiveCamera.prototype.toJSON = function( meta ) {
211 212

	var data = THREE.Object3D.prototype.toJSON.call( this, meta );
T
tschw 已提交
213
	THREE.PerspectiveCamera._assignProps( data.object, this );
214 215
	return data;

216
};