OSDN Git Service

4a387c3db35cec44decb14fa7e4e7e6a9550c9cd
[webglgame/webgl_framework.git] / webglFramework / Thirdparty / three.js-master / src / cameras / StereoCamera.js
1 import { Matrix4 } from '../math/Matrix4';
2 import { _Math } from '../math/Math';
3 import { PerspectiveCamera } from './PerspectiveCamera';
4
5 /**
6  * @author mrdoob / http://mrdoob.com/
7  */
8
9 function StereoCamera() {
10
11         this.type = 'StereoCamera';
12
13         this.aspect = 1;
14
15         this.eyeSep = 0.064;
16
17         this.cameraL = new PerspectiveCamera();
18         this.cameraL.layers.enable( 1 );
19         this.cameraL.matrixAutoUpdate = false;
20
21         this.cameraR = new PerspectiveCamera();
22         this.cameraR.layers.enable( 2 );
23         this.cameraR.matrixAutoUpdate = false;
24
25 }
26
27 Object.assign( StereoCamera.prototype, {
28
29         update: ( function () {
30
31                 var instance, focus, fov, aspect, near, far, zoom, eyeSep;
32
33                 var eyeRight = new Matrix4();
34                 var eyeLeft = new Matrix4();
35
36                 return function update( camera ) {
37
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;
41
42                         if ( needsUpdate ) {
43
44                                 instance = this;
45                                 focus = camera.focus;
46                                 fov = camera.fov;
47                                 aspect = camera.aspect * this.aspect;
48                                 near = camera.near;
49                                 far = camera.far;
50                                 zoom = camera.zoom;
51
52                                 // Off-axis stereoscopic effect based on
53                                 // http://paulbourke.net/stereographics/stereorender/
54
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;
59                                 var xmin, xmax;
60
61                                 // translate xOffset
62
63                                 eyeLeft.elements[ 12 ] = - eyeSep;
64                                 eyeRight.elements[ 12 ] = eyeSep;
65
66                                 // for left eye
67
68                                 xmin = - ymax * aspect + eyeSepOnProjection;
69                                 xmax = ymax * aspect + eyeSepOnProjection;
70
71                                 projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
72                                 projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
73
74                                 this.cameraL.projectionMatrix.copy( projectionMatrix );
75
76                                 // for right eye
77
78                                 xmin = - ymax * aspect - eyeSepOnProjection;
79                                 xmax = ymax * aspect - eyeSepOnProjection;
80
81                                 projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
82                                 projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
83
84                                 this.cameraR.projectionMatrix.copy( projectionMatrix );
85
86                         }
87
88                         this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( eyeLeft );
89                         this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( eyeRight );
90
91                 };
92
93         } )()
94
95 } );
96
97
98 export { StereoCamera };