WebVRManager.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Matrix4 } from '../../math/Matrix4.js';
  5. import { Vector4 } from '../../math/Vector4.js';
  6. import { ArrayCamera } from '../../cameras/ArrayCamera.js';
  7. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
  8. function WebVRManager( renderer ) {
  9. var scope = this;
  10. var device = null;
  11. var frameData = null;
  12. var poseTarget = null;
  13. var standingMatrix = new Matrix4();
  14. var standingMatrixInverse = new Matrix4();
  15. if ( typeof window !== 'undefined' && 'VRFrameData' in window ) {
  16. frameData = new window.VRFrameData();
  17. }
  18. var matrixWorldInverse = new Matrix4();
  19. var cameraL = new PerspectiveCamera();
  20. cameraL.bounds = new Vector4( 0.0, 0.0, 0.5, 1.0 );
  21. cameraL.layers.enable( 1 );
  22. var cameraR = new PerspectiveCamera();
  23. cameraR.bounds = new Vector4( 0.5, 0.0, 0.5, 1.0 );
  24. cameraR.layers.enable( 2 );
  25. var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
  26. cameraVR.layers.enable( 1 );
  27. cameraVR.layers.enable( 2 );
  28. //
  29. var currentSize, currentPixelRatio;
  30. function onVRDisplayPresentChange() {
  31. if ( device !== null && device.isPresenting ) {
  32. var eyeParameters = device.getEyeParameters( 'left' );
  33. var renderWidth = eyeParameters.renderWidth;
  34. var renderHeight = eyeParameters.renderHeight;
  35. currentPixelRatio = renderer.getPixelRatio();
  36. currentSize = renderer.getSize();
  37. renderer.setDrawingBufferSize( renderWidth * 2, renderHeight, 1 );
  38. } else if ( scope.enabled ) {
  39. renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio );
  40. }
  41. }
  42. if ( typeof window !== 'undefined' ) {
  43. window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  44. }
  45. //
  46. this.enabled = false;
  47. this.userHeight = 1.6;
  48. this.getDevice = function () {
  49. return device;
  50. };
  51. this.setDevice = function ( value ) {
  52. if ( value !== undefined ) device = value;
  53. };
  54. this.setPoseTarget = function ( object ) {
  55. if ( object !== undefined ) poseTarget = object;
  56. };
  57. this.getCamera = function ( camera ) {
  58. if ( device === null ) return camera;
  59. device.depthNear = camera.near;
  60. device.depthFar = camera.far;
  61. device.getFrameData( frameData );
  62. //
  63. var pose = frameData.pose;
  64. var poseObject = poseTarget !== null ? poseTarget : camera;
  65. if ( pose.position !== null ) {
  66. poseObject.position.fromArray( pose.position );
  67. } else {
  68. poseObject.position.set( 0, 0, 0 );
  69. }
  70. if ( pose.orientation !== null ) {
  71. poseObject.quaternion.fromArray( pose.orientation );
  72. }
  73. var stageParameters = device.stageParameters;
  74. if ( stageParameters ) {
  75. standingMatrix.fromArray( stageParameters.sittingToStandingTransform );
  76. } else {
  77. standingMatrix.makeTranslation( 0, scope.userHeight, 0 );
  78. }
  79. poseObject.position.applyMatrix4( standingMatrix );
  80. poseObject.updateMatrixWorld();
  81. if ( device.isPresenting === false ) return camera;
  82. //
  83. cameraL.near = camera.near;
  84. cameraR.near = camera.near;
  85. cameraL.far = camera.far;
  86. cameraR.far = camera.far;
  87. cameraVR.matrixWorld.copy( camera.matrixWorld );
  88. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  89. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  90. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  91. // TODO (mrdoob) Double check this code
  92. standingMatrixInverse.getInverse( standingMatrix );
  93. cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
  94. cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
  95. var parent = poseObject.parent;
  96. if ( parent !== null ) {
  97. matrixWorldInverse.getInverse( parent.matrixWorld );
  98. cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
  99. cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
  100. }
  101. // envMap and Mirror needs camera.matrixWorld
  102. cameraL.matrixWorld.getInverse( cameraL.matrixWorldInverse );
  103. cameraR.matrixWorld.getInverse( cameraR.matrixWorldInverse );
  104. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  105. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  106. // HACK (mrdoob)
  107. // https://github.com/w3c/webvr/issues/203
  108. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  109. //
  110. var layers = device.getLayers();
  111. if ( layers.length ) {
  112. var layer = layers[ 0 ];
  113. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  114. cameraL.bounds.fromArray( layer.leftBounds );
  115. }
  116. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  117. cameraR.bounds.fromArray( layer.rightBounds );
  118. }
  119. }
  120. return cameraVR;
  121. };
  122. this.getStandingMatrix = function () {
  123. return standingMatrix;
  124. };
  125. this.submitFrame = function () {
  126. if ( device && device.isPresenting ) device.submitFrame();
  127. };
  128. this.dispose = function () {
  129. if ( typeof window !== 'undefined' ) {
  130. window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
  131. }
  132. };
  133. }
  134. export { WebVRManager };