StereoEffect.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @authod mrdoob / http://mrdoob.com/
  4. * @authod arodic / http://aleksandarrodic.com/
  5. * @authod fonserbc / http://fonserbc.github.io/
  6. *
  7. * Off-axis stereoscopic effect based on http://paulbourke.net/stereographics/stereorender/
  8. */
  9. THREE.StereoEffect = function ( renderer ) {
  10. // API
  11. this.eyeSeparation = 3;
  12. /*
  13. * Distance to the non-parallax or projection plane
  14. */
  15. this.focalLength = 15;
  16. // internals
  17. var _width, _height;
  18. var _position = new THREE.Vector3();
  19. var _quaternion = new THREE.Quaternion();
  20. var _scale = new THREE.Vector3();
  21. var _cameraL = new THREE.PerspectiveCamera();
  22. var _cameraR = new THREE.PerspectiveCamera();
  23. var _fov;
  24. var _outer, _inner, _top, _bottom;
  25. var _ndfl, _halfFocalWidth, _halfFocalHeight;
  26. var _innerFactor, _outerFactor;
  27. // initialization
  28. renderer.autoClear = false;
  29. this.setSize = function ( width, height ) {
  30. _width = width / 2;
  31. _height = height;
  32. renderer.setSize( width, height );
  33. };
  34. this.render = function ( scene, camera ) {
  35. scene.updateMatrixWorld();
  36. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  37. camera.matrixWorld.decompose( _position, _quaternion, _scale );
  38. // Stereo frustum calculation
  39. // Effective fov of the camera
  40. _fov = THREE.Math.radToDeg( 2 * Math.atan( Math.tan( THREE.Math.degToRad( camera.fov ) * 0.5 ) / camera.zoom ) );
  41. _ndfl = camera.near / this.focalLength;
  42. _halfFocalHeight = Math.tan( THREE.Math.degToRad( _fov ) * 0.5 ) * this.focalLength;
  43. _halfFocalWidth = _halfFocalHeight * 0.5 * camera.aspect;
  44. _top = _halfFocalHeight * _ndfl;
  45. _bottom = -_top;
  46. _innerFactor = ( _halfFocalWidth + this.eyeSeparation / 2.0 ) / ( _halfFocalWidth * 2.0 );
  47. _outerFactor = 1.0 - _innerFactor;
  48. _outer = _halfFocalWidth * 2.0 * _ndfl * _outerFactor;
  49. _inner = _halfFocalWidth * 2.0 * _ndfl * _innerFactor;
  50. // left
  51. _cameraL.projectionMatrix.makeFrustum(
  52. -_outer,
  53. _inner,
  54. _bottom,
  55. _top,
  56. camera.near,
  57. camera.far
  58. );
  59. _cameraL.position.copy( _position );
  60. _cameraL.quaternion.copy( _quaternion );
  61. _cameraL.translateX( - this.eyeSeparation / 2.0 );
  62. // right
  63. _cameraR.projectionMatrix.makeFrustum(
  64. -_inner,
  65. _outer,
  66. _bottom,
  67. _top,
  68. camera.near,
  69. camera.far
  70. );
  71. _cameraR.position.copy( _position );
  72. _cameraR.quaternion.copy( _quaternion );
  73. _cameraR.translateX( this.eyeSeparation / 2.0 );
  74. //
  75. renderer.setViewport( 0, 0, _width * 2, _height );
  76. renderer.clear();
  77. renderer.setViewport( 0, 0, _width, _height );
  78. renderer.render( scene, _cameraL );
  79. renderer.setViewport( _width, 0, _width, _height );
  80. renderer.render( scene, _cameraR );
  81. };
  82. };