From ea01d64d1cf047203624a10ca072e53d8bbf6119 Mon Sep 17 00:00:00 2001 From: Mugen87 Date: Fri, 9 Apr 2021 17:40:28 +0200 Subject: [PATCH] Cameras: Convert to classes. --- src/cameras/Camera.js | 48 +++++++++---------- src/cameras/OrthographicCamera.js | 3 +- src/cameras/PerspectiveCamera.js | 80 +++++++++++++++---------------- 3 files changed, 62 insertions(+), 69 deletions(-) diff --git a/src/cameras/Camera.js b/src/cameras/Camera.js index d785a4f77c5364..071d2f7a450288 100644 --- a/src/cameras/Camera.js +++ b/src/cameras/Camera.js @@ -2,28 +2,24 @@ import { Matrix4 } from '../math/Matrix4.js'; import { Object3D } from '../core/Object3D.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 ); @@ -32,9 +28,9 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), { return this; - }, + } - getWorldDirection: function ( target ) { + getWorldDirection( target ) { 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(); - }, + } - updateMatrixWorld: function ( force ) { + updateMatrixWorld( force ) { - Object3D.prototype.updateMatrixWorld.call( this, force ); + super.updateMatrixWorld( force ); 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(); - }, + } - clone: function () { + clone() { return new this.constructor().copy( this ); } -} ); +} + +Camera.prototype.isCamera = true; export { Camera }; diff --git a/src/cameras/OrthographicCamera.js b/src/cameras/OrthographicCamera.js index b036d1fceba2e0..523fd2330ee2f4 100755 --- a/src/cameras/OrthographicCamera.js +++ b/src/cameras/OrthographicCamera.js @@ -1,5 +1,4 @@ import { Camera } from './Camera.js'; -import { Object3D } from '../core/Object3D.js'; class OrthographicCamera extends Camera { @@ -114,7 +113,7 @@ class OrthographicCamera extends Camera { toJSON( meta ) { - const data = Object3D.prototype.toJSON.call( this, meta ); + const data = super.toJSON( meta ); data.object.zoom = this.zoom; data.object.left = this.left; diff --git a/src/cameras/PerspectiveCamera.js b/src/cameras/PerspectiveCamera.js index e6080f33695506..d357491f3023bf 100644 --- a/src/cameras/PerspectiveCamera.js +++ b/src/cameras/PerspectiveCamera.js @@ -1,39 +1,34 @@ import { Camera } from './Camera.js'; -import { Object3D } from '../core/Object3D.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.zoom = source.zoom; @@ -50,7 +45,7 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), return this; - }, + } /** * 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. */ - setFocalLength: function ( focalLength ) { + setFocalLength( focalLength ) { /** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */ 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.updateProjectionMatrix(); - }, + } /** * Calculates the focal length from the current .fov and .filmGauge. */ - getFocalLength: function () { + getFocalLength() { const vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ); return 0.5 * this.getFilmHeight() / vExtentSlope; - }, + } - getEffectiveFOV: function () { + getEffectiveFOV() { return MathUtils.RAD2DEG * 2 * Math.atan( Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom ); - }, + } - getFilmWidth: function () { + getFilmWidth() { // film not completely covered in portrait format (aspect < 1) return this.filmGauge * Math.min( this.aspect, 1 ); - }, + } - getFilmHeight: function () { + getFilmHeight() { // film not completely covered in landscape format (aspect > 1) return this.filmGauge / Math.max( this.aspect, 1 ); - }, + } /** * 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. */ - setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) { + setViewOffset( fullWidth, fullHeight, x, y, width, height ) { this.aspect = fullWidth / fullHeight; @@ -165,9 +160,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), this.updateProjectionMatrix(); - }, + } - clearViewOffset: function () { + clearViewOffset() { if ( this.view !== null ) { @@ -177,9 +172,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), this.updateProjectionMatrix(); - }, + } - updateProjectionMatrix: function () { + updateProjectionMatrix() { const near = this.near; 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(); - }, + } - 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.zoom = this.zoom; @@ -231,7 +226,8 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), } -} ); +} +PerspectiveCamera.prototype.isPerspectiveCamera = true; export { PerspectiveCamera };