WebVRManager.js 4.1 KB

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