Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cameras: Convert to classes. #21623

Merged
merged 1 commit into from
Apr 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/cameras/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand All @@ -32,9 +28,9 @@ Camera.prototype = Object.assign( Object.create( Object3D.prototype ), {

return this;

},
}

getWorldDirection: function ( target ) {
getWorldDirection( target ) {

if ( target === undefined ) {

Expand All @@ -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 };
3 changes: 1 addition & 2 deletions src/cameras/OrthographicCamera.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Camera } from './Camera.js';
import { Object3D } from '../core/Object3D.js';

class OrthographicCamera extends Camera {

Expand Down Expand Up @@ -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;
Expand Down
80 changes: 38 additions & 42 deletions src/cameras/PerspectiveCamera.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.
Expand All @@ -60,47 +55,47 @@ 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;

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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -165,9 +160,9 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),

this.updateProjectionMatrix();

},
}

clearViewOffset: function () {
clearViewOffset() {

if ( this.view !== null ) {

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -231,7 +226,8 @@ PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ),

}

} );
}

PerspectiveCamera.prototype.isPerspectiveCamera = true;

export { PerspectiveCamera };