2128021280 * Assumes 2 cameras that are parallel and share an X-axis, and that
2128121281 * the cameras' projection and world matrices have already been set.
2128221282 * And that near and far planes are identical for both cameras.
21283+ * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765
2128321284 */
2128421285 function setProjectionFromUnion( camera, cameraL, cameraR ) {
2128521286
@@ -21293,23 +21294,21 @@
2129321294
2129421295 // VR systems will have identical far and near planes, and
2129521296 // most likely identical top and bottom frustum extents.
21296- // via: https://computergraphics.stackexchange.com/a/4765
21297+ // Use the left camera for these values.
2129721298 var near = projL[ 14 ] / ( projL[ 10 ] - 1 );
2129821299 var far = projL[ 14 ] / ( projL[ 10 ] + 1 );
21300+ var topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ];
21301+ var bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ];
2129921302
21300- var leftFovL = ( projL[ 8 ] - 1 ) / projL[ 0 ];
21301- var rightFovR = ( projR[ 8 ] + 1 ) / projR[ 0 ];
21302- var leftL = near * leftFovL;
21303- var rightR = near * rightFovR;
21304- var topL = near * ( projL[ 9 ] + 1 ) / projL[ 5 ];
21305- var topR = near * ( projR[ 9 ] + 1 ) / projR[ 5 ];
21306- var bottomL = near * ( projL[ 9 ] - 1 ) / projL[ 5 ];
21307- var bottomR = near * ( projR[ 9 ] - 1 ) / projR[ 5 ];
21303+ var leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ];
21304+ var rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ];
21305+ var left = near * leftFov;
21306+ var right = near * rightFov;
2130821307
2130921308 // Calculate the new camera's position offset from the
2131021309 // left camera. xOffset should be roughly half `ipd`.
21311- var zOffset = ipd / ( - leftFovL + rightFovR );
21312- var xOffset = zOffset * - leftFovL ;
21310+ var zOffset = ipd / ( - leftFov + rightFov );
21311+ var xOffset = zOffset * - leftFov ;
2131321312
2131421313 // TODO: Better way to apply this offset?
2131521314 cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale );
@@ -21323,12 +21322,12 @@
2132321322 // although must now be relative to the new union camera.
2132421323 var near2 = near + zOffset;
2132521324 var far2 = far + zOffset;
21326- var left = leftL - xOffset;
21327- var right = rightR + ( ipd - xOffset );
21328- var top = Math.max( topL, topR ) ;
21329- var bottom = Math.min( bottomL, bottomR ) ;
21325+ var left2 = left - xOffset;
21326+ var right2 = right + ( ipd - xOffset );
21327+ var top2 = topFov * far / far2 * near2 ;
21328+ var bottom2 = bottomFov * far / far2 * near2 ;
2133021329
21331- camera.projectionMatrix.makePerspective( left, right, top, bottom , near2, far2 );
21330+ camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2 , near2, far2 );
2133221331
2133321332 }
2133421333
0 commit comments