Skip to content

Commit

Permalink
cameras: Convert to es6.
Browse files Browse the repository at this point in the history
  • Loading branch information
linbingquan committed Aug 17, 2020
1 parent 7a879a0 commit 75beab2
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 105 deletions.
120 changes: 68 additions & 52 deletions src/cameras/CubeCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,68 @@ import { PerspectiveCamera } from './PerspectiveCamera.js';

const fov = 90, aspect = 1;

function CubeCamera( near, far, renderTarget ) {
class CubeCamera extends Object3D {

Object3D.call( this );
constructor( near, far, renderTarget ) {

this.type = 'CubeCamera';
super();

if ( renderTarget.isWebGLCubeRenderTarget !== true ) {
this.type = 'CubeCamera';

console.error( 'THREE.CubeCamera: The constructor now expects an instance of WebGLCubeRenderTarget as third parameter.' );
return;
if ( renderTarget.isWebGLCubeRenderTarget !== true ) {

console.error( 'THREE.CubeCamera: The constructor now expects an instance of WebGLCubeRenderTarget as third parameter.' );
return;

}

this.renderTarget = renderTarget;

const cameraPX = new PerspectiveCamera( fov, aspect, near, far );
cameraPX.layers = this.layers;
cameraPX.up.set( 0, - 1, 0 );
cameraPX.lookAt( new Vector3( 1, 0, 0 ) );
this.cameraPX = cameraPX;
this.add( cameraPX );

const cameraNX = new PerspectiveCamera( fov, aspect, near, far );
cameraNX.layers = this.layers;
cameraNX.up.set( 0, - 1, 0 );
cameraNX.lookAt( new Vector3( - 1, 0, 0 ) );
this.cameraNX = cameraNX;
this.add( cameraNX );

const cameraPY = new PerspectiveCamera( fov, aspect, near, far );
cameraPY.layers = this.layers;
cameraPY.up.set( 0, 0, 1 );
cameraPY.lookAt( new Vector3( 0, 1, 0 ) );
this.cameraPY = cameraPY;
this.add( cameraPY );

const cameraNY = new PerspectiveCamera( fov, aspect, near, far );
cameraNY.layers = this.layers;
cameraNY.up.set( 0, 0, - 1 );
cameraNY.lookAt( new Vector3( 0, - 1, 0 ) );
this.cameraNY = cameraNY;
this.add( cameraNY );

const cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
cameraPZ.layers = this.layers;
cameraPZ.up.set( 0, - 1, 0 );
cameraPZ.lookAt( new Vector3( 0, 0, 1 ) );
this.cameraPZ = cameraPZ;
this.add( cameraPZ );

const cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
cameraNZ.layers = this.layers;
cameraNZ.up.set( 0, - 1, 0 );
cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) );
this.cameraNZ = cameraNZ;
this.add( cameraNZ );

}

this.renderTarget = renderTarget;

const cameraPX = new PerspectiveCamera( fov, aspect, near, far );
cameraPX.layers = this.layers;
cameraPX.up.set( 0, - 1, 0 );
cameraPX.lookAt( new Vector3( 1, 0, 0 ) );
this.add( cameraPX );

const cameraNX = new PerspectiveCamera( fov, aspect, near, far );
cameraNX.layers = this.layers;
cameraNX.up.set( 0, - 1, 0 );
cameraNX.lookAt( new Vector3( - 1, 0, 0 ) );
this.add( cameraNX );

const cameraPY = new PerspectiveCamera( fov, aspect, near, far );
cameraPY.layers = this.layers;
cameraPY.up.set( 0, 0, 1 );
cameraPY.lookAt( new Vector3( 0, 1, 0 ) );
this.add( cameraPY );

const cameraNY = new PerspectiveCamera( fov, aspect, near, far );
cameraNY.layers = this.layers;
cameraNY.up.set( 0, 0, - 1 );
cameraNY.lookAt( new Vector3( 0, - 1, 0 ) );
this.add( cameraNY );

const cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
cameraPZ.layers = this.layers;
cameraPZ.up.set( 0, - 1, 0 );
cameraPZ.lookAt( new Vector3( 0, 0, 1 ) );
this.add( cameraPZ );

const cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
cameraNZ.layers = this.layers;
cameraNZ.up.set( 0, - 1, 0 );
cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) );
this.add( cameraNZ );

this.update = function ( renderer, scene ) {
update( renderer, scene ) {

if ( this.parent === null ) this.updateMatrixWorld();

Expand All @@ -64,40 +74,50 @@ function CubeCamera( near, far, renderTarget ) {

renderer.xr.enabled = false;

const renderTarget = this.renderTarget;

const generateMipmaps = renderTarget.texture.generateMipmaps;

renderTarget.texture.generateMipmaps = false;

renderer.setRenderTarget( renderTarget, 0 );
const cameraPX = this.cameraPX;
renderer.render( scene, cameraPX );

renderer.setRenderTarget( renderTarget, 1 );
const cameraNX = this.cameraNX;
renderer.render( scene, cameraNX );

renderer.setRenderTarget( renderTarget, 2 );
const cameraPY = this.cameraPY;
renderer.render( scene, cameraPY );

renderer.setRenderTarget( renderTarget, 3 );
const cameraNY = this.cameraNY;
renderer.render( scene, cameraNY );

renderer.setRenderTarget( renderTarget, 4 );
const cameraPZ = this.cameraPZ;
renderer.render( scene, cameraPZ );

renderTarget.texture.generateMipmaps = generateMipmaps;

renderer.setRenderTarget( renderTarget, 5 );
const cameraNZ = this.cameraNZ;
renderer.render( scene, cameraNZ );

renderer.setRenderTarget( currentRenderTarget );

renderer.xr.enabled = currentXrEnabled;

};
}

this.clear = function ( renderer, color, depth, stencil ) {
clear( renderer, color, depth, stencil ) {

const currentRenderTarget = renderer.getRenderTarget();

const renderTarget = this.renderTarget;

for ( let i = 0; i < 6; i ++ ) {

renderer.setRenderTarget( renderTarget, i );
Expand All @@ -108,12 +128,8 @@ function CubeCamera( near, far, renderTarget ) {

renderer.setRenderTarget( currentRenderTarget );

};
}

}

CubeCamera.prototype = Object.create( Object3D.prototype );
CubeCamera.prototype.constructor = CubeCamera;


export { CubeCamera };
55 changes: 26 additions & 29 deletions src/cameras/OrthographicCamera.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
import { Camera } from './Camera.js';
import { Object3D } from '../core/Object3D.js';

function OrthographicCamera( left, right, top, bottom, near, far ) {
class OrthographicCamera extends Camera {

Camera.call( this );
constructor( left, right, top, bottom, near, far ) {

this.type = 'OrthographicCamera';
super();

this.zoom = 1;
this.view = null;
this.type = 'OrthographicCamera';

this.left = ( left !== undefined ) ? left : - 1;
this.right = ( right !== undefined ) ? right : 1;
this.top = ( top !== undefined ) ? top : 1;
this.bottom = ( bottom !== undefined ) ? bottom : - 1;
this.zoom = 1;
this.view = null;

this.near = ( near !== undefined ) ? near : 0.1;
this.far = ( far !== undefined ) ? far : 2000;
this.left = ( left !== undefined ) ? left : - 1;
this.right = ( right !== undefined ) ? right : 1;
this.top = ( top !== undefined ) ? top : 1;
this.bottom = ( bottom !== undefined ) ? bottom : - 1;

this.updateProjectionMatrix();
this.near = ( near !== undefined ) ? near : 0.1;
this.far = ( far !== undefined ) ? far : 2000;

}

OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), {

constructor: OrthographicCamera,
this.updateProjectionMatrix();

isOrthographicCamera: true,
}

copy: function ( source, recursive ) {
copy( source, recursive ) {

Camera.prototype.copy.call( this, source, recursive );
super.copy( source, recursive );

this.left = source.left;
this.right = source.right;
Expand All @@ -44,9 +40,9 @@ OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ),

return this;

},
}

setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) {
setViewOffset( fullWidth, fullHeight, x, y, width, height ) {

if ( this.view === null ) {

Expand All @@ -72,9 +68,9 @@ OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ),

this.updateProjectionMatrix();

},
}

clearViewOffset: function () {
clearViewOffset() {

if ( this.view !== null ) {

Expand All @@ -84,9 +80,9 @@ OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ),

this.updateProjectionMatrix();

},
}

updateProjectionMatrix: function () {
updateProjectionMatrix() {

const dx = ( this.right - this.left ) / ( 2 * this.zoom );
const dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
Expand Down Expand Up @@ -114,9 +110,9 @@ OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ),

this.projectionMatrixInverse.getInverse( this.projectionMatrix );

},
}

toJSON: function ( meta ) {
toJSON( meta ) {

const data = Object3D.prototype.toJSON.call( this, meta );

Expand All @@ -134,7 +130,8 @@ OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ),

}

} );
}

OrthographicCamera.prototype.isOrthographicCamera = true;

export { OrthographicCamera };
47 changes: 23 additions & 24 deletions src/cameras/StereoCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import { PerspectiveCamera } from './PerspectiveCamera.js';
const _eyeRight = new Matrix4();
const _eyeLeft = new Matrix4();

function StereoCamera() {
class StereoCamera {

this.type = 'StereoCamera';
constructor() {

this.aspect = 1;
this.type = 'StereoCamera';

this.eyeSep = 0.064;
this.aspect = 1;

this.cameraL = new PerspectiveCamera();
this.cameraL.layers.enable( 1 );
this.cameraL.matrixAutoUpdate = false;
this.eyeSep = 0.064;

this.cameraR = new PerspectiveCamera();
this.cameraR.layers.enable( 2 );
this.cameraR.matrixAutoUpdate = false;
this.cameraL = new PerspectiveCamera();
this.cameraL.layers.enable( 1 );
this.cameraL.matrixAutoUpdate = false;

this._cache = {
focus: null,
fov: null,
aspect: null,
near: null,
far: null,
zoom: null,
eyeSep: null
};
this.cameraR = new PerspectiveCamera();
this.cameraR.layers.enable( 2 );
this.cameraR.matrixAutoUpdate = false;

}
this._cache = {
focus: null,
fov: null,
aspect: null,
near: null,
far: null,
zoom: null,
eyeSep: null
};

Object.assign( StereoCamera.prototype, {
}

update: function ( camera ) {
update( camera ) {

const cache = this._cache;

Expand Down Expand Up @@ -94,7 +94,6 @@ Object.assign( StereoCamera.prototype, {

}

} );

}

export { StereoCamera };

0 comments on commit 75beab2

Please sign in to comment.