WebVRManager.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function WebVRManager( renderer ) {
  5. var scope = this;
  6. var device = null;
  7. var frameData = null;
  8. if ( 'VRFrameData' in window ) {
  9. frameData = new window.VRFrameData();
  10. }
  11. var cameraL = new THREE.PerspectiveCamera();
  12. cameraL.bounds = new THREE.Vector4( 0.0, 0.0, 0.5, 1.0 );
  13. cameraL.layers.enable( 1 );
  14. var cameraR = new THREE.PerspectiveCamera();
  15. cameraR.bounds = new THREE.Vector4( 0.5, 0.0, 0.5, 1.0 );
  16. cameraR.layers.enable( 2 );
  17. var cameraVR = new THREE.ArrayCamera( [ cameraL, cameraR ] );
  18. //
  19. var currentSize, currentPixelRatio;
  20. function onVRDisplayPresentChange() {
  21. if ( device.isPresenting ) {
  22. var eyeParameters = device.getEyeParameters( 'left' );
  23. var renderWidth = eyeParameters.renderWidth;
  24. var renderHeight = eyeParameters.renderHeight;
  25. currentPixelRatio = renderer.getPixelRatio();
  26. currentSize = renderer.getSize();
  27. renderer.setPixelRatio( 1 );
  28. renderer.setSize( renderWidth * 2, renderHeight, false );
  29. } else if ( scope.enabled ) {
  30. renderer.setPixelRatio( currentPixelRatio );
  31. renderer.setSize( currentSize.width, currentSize.height, true );
  32. }
  33. }
  34. window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  35. //
  36. this.enabled = false;
  37. this.getDevice = function () {
  38. return device;
  39. };
  40. this.setDevice = function ( value ) {
  41. if ( value !== undefined ) device = value;
  42. };
  43. this.getCamera = function ( camera ) {
  44. if ( device === null ) return camera;
  45. device.depthNear = camera.near;
  46. device.depthFar = camera.far;
  47. device.getFrameData( frameData );
  48. //
  49. var pose = frameData.pose;
  50. if ( pose.position !== null ) {
  51. camera.position.fromArray( pose.position );
  52. } else {
  53. camera.position.set( 0, 0, 0 );
  54. }
  55. if ( pose.orientation !== null ) {
  56. camera.quaternion.fromArray( pose.orientation );
  57. }
  58. camera.updateMatrixWorld();
  59. if ( device.isPresenting === false ) return camera;
  60. //
  61. cameraVR.matrixWorld.copy( camera.matrixWorld );
  62. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  63. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  64. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  65. var parent = camera.parent;
  66. if ( parent !== null ) {
  67. cameraL.matrixWorldInverse.multiply( parent.matrixWorldInverse );
  68. cameraR.matrixWorldInverse.multiply( parent.matrixWorldInverse );
  69. }
  70. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  71. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  72. // HACK @mrdoob
  73. // https://github.com/w3c/webvr/issues/203
  74. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  75. //
  76. var layers = device.getLayers();
  77. if ( layers.length ) {
  78. var layer = layers[ 0 ];
  79. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  80. cameraL.bounds.fromArray( layer.leftBounds );
  81. }
  82. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  83. cameraR.bounds.fromArray( layer.rightBounds );
  84. }
  85. }
  86. return cameraVR;
  87. };
  88. this.submitFrame = function () {
  89. if ( device && device.isPresenting ) device.submitFrame();
  90. };
  91. }
  92. export { WebVRManager };