1 import { Matrix4 } from '../math/Matrix4';
2 import { _Math } from '../math/Math';
3 import { PerspectiveCamera } from './PerspectiveCamera';
6 * @author mrdoob / http://mrdoob.com/
9 function StereoCamera() {
11 this.type = 'StereoCamera';
17 this.cameraL = new PerspectiveCamera();
18 this.cameraL.layers.enable( 1 );
19 this.cameraL.matrixAutoUpdate = false;
21 this.cameraR = new PerspectiveCamera();
22 this.cameraR.layers.enable( 2 );
23 this.cameraR.matrixAutoUpdate = false;
27 Object.assign( StereoCamera.prototype, {
29 update: ( function () {
31 var instance, focus, fov, aspect, near, far, zoom, eyeSep;
33 var eyeRight = new Matrix4();
34 var eyeLeft = new Matrix4();
36 return function update( camera ) {
38 var needsUpdate = instance !== this || focus !== camera.focus || fov !== camera.fov ||
39 aspect !== camera.aspect * this.aspect || near !== camera.near ||
40 far !== camera.far || zoom !== camera.zoom || eyeSep !== this.eyeSep;
47 aspect = camera.aspect * this.aspect;
52 // Off-axis stereoscopic effect based on
53 // http://paulbourke.net/stereographics/stereorender/
55 var projectionMatrix = camera.projectionMatrix.clone();
56 eyeSep = this.eyeSep / 2;
57 var eyeSepOnProjection = eyeSep * near / focus;
58 var ymax = ( near * Math.tan( _Math.DEG2RAD * fov * 0.5 ) ) / zoom;
63 eyeLeft.elements[ 12 ] = - eyeSep;
64 eyeRight.elements[ 12 ] = eyeSep;
68 xmin = - ymax * aspect + eyeSepOnProjection;
69 xmax = ymax * aspect + eyeSepOnProjection;
71 projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
72 projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
74 this.cameraL.projectionMatrix.copy( projectionMatrix );
78 xmin = - ymax * aspect - eyeSepOnProjection;
79 xmax = ymax * aspect - eyeSepOnProjection;
81 projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
82 projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
84 this.cameraR.projectionMatrix.copy( projectionMatrix );
88 this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( eyeLeft );
89 this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( eyeRight );
98 export { StereoCamera };