Created
October 5, 2011 09:14
-
-
Save zz85/1263996 to your computer and use it in GitHub Desktop.
A general purpose camera for Three.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
THREE.CombinedCamera = function ( width, height, fov, near, far, orthonear, orthofar ) { | |
THREE.Camera.call( this ); | |
this.cameraO = new THREE.OrthographicCamera( width / - 2, width / 2, height / 2, height / - 2, orthonear, orthofar ); | |
this.cameraP = new THREE.PerspectiveCamera( fov, width/height, near, far ); | |
this.toPerspective(); | |
}; | |
THREE.CombinedCamera.prototype = new THREE.Camera(); | |
THREE.CombinedCamera.prototype.constructor = THREE.CoolCamera; | |
THREE.CombinedCamera.prototype.toPerspective = function () { | |
this.near = this.cameraP.near; | |
this.far = this.cameraP.far; | |
this.projectionMatrix = this.cameraP.projectionMatrix; | |
}; | |
THREE.CombinedCamera.prototype.toOrthographic = function () { | |
this.near = this.cameraO.near; | |
this.far = this.cameraO.far; | |
this.projectionMatrix = this.cameraO.projectionMatrix; | |
}; | |
THREE.CombinedCamera.prototype.setFov = function(fov) { | |
this.cameraP.fov = fov; | |
this.cameraP.updateProjectionMatrix(); | |
this.toPerspective(); | |
}; | |
/* | |
* Uses Focal Length (in mm) to estimate and set FOV | |
* 35mm (fullframe) camera is used if frame size is not specified; | |
* Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html | |
*/ | |
THREE.CombinedCamera.prototype.setLens = function(focalLength, framesize) { | |
if (!framesize) framesize = 43.25; // 36x24mm | |
var fov = 2 * Math.atan( framesize / (focalLength * 2)); | |
fov = 180 / Math.PI * fov; | |
this.setFov(fov); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment