Browse Source

Cameras: Convert to classes. (#21623)

Michael Herzog 4 years ago
parent
commit
92e531cf19
3 changed files with 62 additions and 69 deletions
  1. 23 25
      src/cameras/Camera.js
  2. 1 2
      src/cameras/OrthographicCamera.js
  3. 38 42
      src/cameras/PerspectiveCamera.js

+ 23 - 25
src/cameras/Camera.js

@@ -2,28 +2,24 @@ import { Matrix4 } from '../math/Matrix4.js';
 import { Object3D } from '../core/Object3D.js';
 import { Object3D } from '../core/Object3D.js';
 import { Vector3 } from '../math/Vector3.js';
 import { Vector3 } from '../math/Vector3.js';
 
 
-function Camera() {
+class Camera extends Object3D {
 
 
-	Object3D.call( this );
+	constructor() {
 
 
-	this.type = 'Camera';
+		super();
 
 
-	this.matrixWorldInverse = new Matrix4();
+		this.type = 'Camera';
 
 
-	this.projectionMatrix = new Matrix4();
-	this.projectionMatrixInverse = new Matrix4();
+		this.matrixWorldInverse = new Matrix4();
 
 
-}
-
-Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
-
-	constructor: Camera,
+		this.projectionMatrix = new Matrix4();
+		this.projectionMatrixInverse = new Matrix4();
 
 
-	isCamera: true,
+	}
 
 
-	copy: function ( source, recursive ) {
+	copy( source, recursive ) {
 
 
-		Object3D.prototype.copy.call( this, source, recursive );
+		super.copy( source, recursive );
 
 
 		this.matrixWorldInverse.copy( source.matrixWorldInverse );
 		this.matrixWorldInverse.copy( source.matrixWorldInverse );
 
 
@@ -32,9 +28,9 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 
 		return this;
 		return this;
 
 
-	},
+	}
 
 
-	getWorldDirection: function ( target ) {
+	getWorldDirection( target ) {
 
 
 		if ( target === undefined ) {
 		if ( target === undefined ) {
 
 
@@ -49,30 +45,32 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {
 
 
 		return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
 		return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize();
 
 
-	},
+	}
 
 
-	updateMatrixWorld: function ( force ) {
+	updateMatrixWorld( force ) {
 
 
-		Object3D.prototype.updateMatrixWorld.call( this, force );
+		super.updateMatrixWorld( force );
 
 
 		this.matrixWorldInverse.copy( this.matrixWorld ).invert();
 		this.matrixWorldInverse.copy( this.matrixWorld ).invert();
 
 
-	},
+	}
 
 
-	updateWorldMatrix: function ( updateParents, updateChildren ) {
+	updateWorldMatrix( updateParents, updateChildren ) {
 
 
-		Object3D.prototype.updateWorldMatrix.call( this, updateParents, updateChildren );
+		super.updateWorldMatrix( updateParents, updateChildren );
 
 
 		this.matrixWorldInverse.copy( this.matrixWorld ).invert();
 		this.matrixWorldInverse.copy( this.matrixWorld ).invert();
 
 
-	},
+	}
 
 
-	clone: function () {
+	clone() {
 
 
 		return new this.constructor().copy( this );
 		return new this.constructor().copy( this );
 
 
 	}
 	}
 
 
-} );
+}
+
+Camera.prototype.isCamera = true;
 
 
 export { Camera };
 export { Camera };

+ 1 - 2
src/cameras/OrthographicCamera.js

@@ -1,5 +1,4 @@
 import { Camera } from './Camera.js';
 import { Camera } from './Camera.js';
-import { Object3D } from '../core/Object3D.js';
 
 
 class OrthographicCamera extends Camera {
 class OrthographicCamera extends Camera {
 
 
@@ -114,7 +113,7 @@ class OrthographicCamera extends Camera {
 
 
 	toJSON( meta ) {
 	toJSON( meta ) {
 
 
-		const data = Object3D.prototype.toJSON.call( this, meta );
+		const data = super.toJSON( meta );
 
 
 		data.object.zoom = this.zoom;
 		data.object.zoom = this.zoom;
 		data.object.left = this.left;
 		data.object.left = this.left;

+ 38 - 42
src/cameras/PerspectiveCamera.js

@@ -1,39 +1,34 @@
 import { Camera } from './Camera.js';
 import { Camera } from './Camera.js';
-import { Object3D } from '../core/Object3D.js';
 import { MathUtils } from '../math/MathUtils.js';
 import { MathUtils } from '../math/MathUtils.js';
 
 
-function PerspectiveCamera( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
+class PerspectiveCamera extends Camera {
 
 
-	Camera.call( this );
+	constructor( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
 
 
-	this.type = 'PerspectiveCamera';
+		super();
 
 
-	this.fov = fov;
-	this.zoom = 1;
+		this.type = 'PerspectiveCamera';
 
 
-	this.near = near;
-	this.far = far;
-	this.focus = 10;
+		this.fov = fov;
+		this.zoom = 1;
 
 
-	this.aspect = aspect;
-	this.view = null;
+		this.near = near;
+		this.far = far;
+		this.focus = 10;
 
 
-	this.filmGauge = 35;	// width of the film (default in millimeters)
-	this.filmOffset = 0;	// horizontal film offset (same unit as gauge)
+		this.aspect = aspect;
+		this.view = null;
 
 
-	this.updateProjectionMatrix();
+		this.filmGauge = 35;	// width of the film (default in millimeters)
+		this.filmOffset = 0;	// horizontal film offset (same unit as gauge)
 
 
-}
-
-PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), {
-
-	constructor: PerspectiveCamera,
+		this.updateProjectionMatrix();
 
 
-	isPerspectiveCamera: true,
+	}
 
 
-	copy: function ( source, recursive ) {
+	copy( source, recursive ) {
 
 
-		Camera.prototype.copy.call( this, source, recursive );
+		super.copy( source, recursive );
 
 
 		this.fov = source.fov;
 		this.fov = source.fov;
 		this.zoom = source.zoom;
 		this.zoom = source.zoom;
@@ -50,7 +45,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 
 
 		return this;
 		return this;
 
 
-	},
+	}
 
 
 	/**
 	/**
 	 * Sets the FOV by focal length in respect to the current .filmGauge.
 	 * Sets the FOV by focal length in respect to the current .filmGauge.
@@ -60,7 +55,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 	 *
 	 *
 	 * Values for focal length and film gauge must have the same unit.
 	 * Values for focal length and film gauge must have the same unit.
 	 */
 	 */
-	setFocalLength: function ( focalLength ) {
+	setFocalLength( focalLength ) {
 
 
 		/** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */
 		/** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */
 		const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
 		const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
@@ -68,39 +63,39 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 		this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope );
 		this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope );
 		this.updateProjectionMatrix();
 		this.updateProjectionMatrix();
 
 
-	},
+	}
 
 
 	/**
 	/**
 	 * Calculates the focal length from the current .fov and .filmGauge.
 	 * Calculates the focal length from the current .fov and .filmGauge.
 	 */
 	 */
-	getFocalLength: function () {
+	getFocalLength() {
 
 
 		const vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
 		const vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov );
 
 
 		return 0.5 * this.getFilmHeight() / vExtentSlope;
 		return 0.5 * this.getFilmHeight() / vExtentSlope;
 
 
-	},
+	}
 
 
-	getEffectiveFOV: function () {
+	getEffectiveFOV() {
 
 
 		return MathUtils.RAD2DEG * 2 * Math.atan(
 		return MathUtils.RAD2DEG * 2 * Math.atan(
 			Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom );
 			Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom );
 
 
-	},
+	}
 
 
-	getFilmWidth: function () {
+	getFilmWidth() {
 
 
 		// film not completely covered in portrait format (aspect < 1)
 		// film not completely covered in portrait format (aspect < 1)
 		return this.filmGauge * Math.min( this.aspect, 1 );
 		return this.filmGauge * Math.min( this.aspect, 1 );
 
 
-	},
+	}
 
 
-	getFilmHeight: function () {
+	getFilmHeight() {
 
 
 		// film not completely covered in landscape format (aspect > 1)
 		// film not completely covered in landscape format (aspect > 1)
 		return this.filmGauge / Math.max( this.aspect, 1 );
 		return this.filmGauge / Math.max( this.aspect, 1 );
 
 
-	},
+	}
 
 
 	/**
 	/**
 	 * Sets an offset in a larger frustum. This is useful for multi-window or
 	 * Sets an offset in a larger frustum. This is useful for multi-window or
@@ -137,7 +132,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 	 *
 	 *
 	 *   Note there is no reason monitors have to be the same size or in a grid.
 	 *   Note there is no reason monitors have to be the same size or in a grid.
 	 */
 	 */
-	setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
+	setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
 
 
 		this.aspect = fullWidth / fullHeight;
 		this.aspect = fullWidth / fullHeight;
 
 
@@ -165,9 +160,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 
 
 		this.updateProjectionMatrix();
 		this.updateProjectionMatrix();
 
 
-	},
+	}
 
 
-	clearViewOffset: function () {
+	clearViewOffset() {
 
 
 		if ( this.view !== null ) {
 		if ( this.view !== null ) {
 
 
@@ -177,9 +172,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 
 
 		this.updateProjectionMatrix();
 		this.updateProjectionMatrix();
 
 
-	},
+	}
 
 
-	updateProjectionMatrix: function () {
+	updateProjectionMatrix() {
 
 
 		const near = this.near;
 		const near = this.near;
 		let top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom;
 		let top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom;
@@ -207,11 +202,11 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 
 
 		this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
 		this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
 
 
-	},
+	}
 
 
-	toJSON: function ( meta ) {
+	toJSON( meta ) {
 
 
-		const data = Object3D.prototype.toJSON.call( this, meta );
+		const data = super.toJSON( meta );
 
 
 		data.object.fov = this.fov;
 		data.object.fov = this.fov;
 		data.object.zoom = this.zoom;
 		data.object.zoom = this.zoom;
@@ -231,7 +226,8 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),
 
 
 	}
 	}
 
 
-} );
+}
 
 
+PerspectiveCamera.prototype.isPerspectiveCamera = true;
 
 
 export { PerspectiveCamera };
 export { PerspectiveCamera };