StereoEffect.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @author alteredq / http://alteredqualia.com/
  3. * @authod mrdoob / http://mrdoob.com/
  4. * @authod arodic / http://aleksandarrodic.com/
  5. */
  6. THREE.StereoEffect = function ( renderer ) {
  7. // API
  8. this.separation = 0;
  9. this.offset = 0;
  10. // internals
  11. var _width, _height, _offset;
  12. var _position = new THREE.Vector3();
  13. var _quaternion = new THREE.Quaternion();
  14. var _scale = new THREE.Vector3();
  15. var _cameraL = new THREE.PerspectiveCamera();
  16. var _cameraR = new THREE.PerspectiveCamera();
  17. // initialization http://www.youtube.com/watch?v=3xWiBCIxjIk
  18. renderer.autoClear = false;
  19. this.setSize = function ( width, height ) {
  20. _width = width / 2;
  21. _height = height;
  22. _offset = _width * this.offset;
  23. renderer.setSize( width, height );
  24. };
  25. this.render = function ( scene, camera ) {
  26. scene.updateMatrixWorld();
  27. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  28. camera.matrixWorld.decompose( _position, _quaternion, _scale );
  29. // left
  30. _cameraL.fov = camera.fov;
  31. _cameraL.aspect = 0.5 * camera.aspect;
  32. _cameraL.near = camera.near;
  33. _cameraL.far = camera.far;
  34. _cameraL.setViewOffset( _width, _height, this.offset * _width / 2.0, 0, _width, _height);
  35. //_cameraL.updateProjectionMatrix();
  36. _cameraL.position.copy( _position );
  37. _cameraL.quaternion.copy( _quaternion );
  38. _cameraL.translateX( - this.separation );
  39. _cameraL.updateMatrixWorld();
  40. // right
  41. _cameraR.fov = camera.fov;
  42. _cameraR.aspect = 0.5 * camera.aspect;
  43. _cameraR.near = camera.near;
  44. _cameraR.far = camera.far;
  45. _cameraR.setViewOffset( _width, _height, - this.offset * _width / 2.0, 0, _width, _height);
  46. //_cameraR.projectionMatrix = _cameraL.projectionMatrix;
  47. _cameraR.position.copy( _position );
  48. _cameraR.quaternion.copy( _quaternion );
  49. _cameraR.translateX( this.separation );
  50. _cameraR.updateMatrixWorld();
  51. renderer.setViewport( 0, 0, _width * 2, _height );
  52. renderer.clear();
  53. //world.updateLeft();
  54. renderer.setViewport( 0, 0, _width, _height );
  55. renderer.render( scene, _cameraL );
  56. //world.updateRight();
  57. renderer.setViewport( _width, 0, _width, _height );
  58. renderer.render( scene, _cameraR );
  59. };
  60. };