WebVRManager.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if ( typeof window !== 'undefined' && '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. cameraVR.layers.enable( 1 );
  26. cameraVR.layers.enable( 2 );
  27. //
  28. var currentSize, currentPixelRatio;
  29. function onVRDisplayPresentChange() {
  30. if ( device !== null && device.isPresenting ) {
  31. var eyeParameters = device.getEyeParameters( 'left' );
  32. var renderWidth = eyeParameters.renderWidth;
  33. var renderHeight = eyeParameters.renderHeight;
  34. currentPixelRatio = renderer.getPixelRatio();
  35. currentSize = renderer.getSize();
  36. renderer.setDrawingBufferSize( renderWidth * 2, renderHeight, 1 );
  37. } else if ( scope.enabled ) {
  38. renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio );
  39. }
  40. }
  41. if ( typeof window !== 'undefined' ) {
  42. window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  43. }
  44. //
  45. this.enabled = false;
  46. this.standing = false;
  47. this.getDevice = function () {
  48. return device;
  49. };
  50. this.setDevice = function ( value ) {
  51. if ( value !== undefined ) device = value;
  52. };
  53. this.getCamera = function ( camera ) {
  54. if ( device === null ) return camera;
  55. device.depthNear = camera.near;
  56. device.depthFar = camera.far;
  57. device.getFrameData( frameData );
  58. //
  59. var pose = frameData.pose;
  60. if ( pose.position !== null ) {
  61. camera.position.fromArray( pose.position );
  62. } else {
  63. camera.position.set( 0, 0, 0 );
  64. }
  65. if ( pose.orientation !== null ) {
  66. camera.quaternion.fromArray( pose.orientation );
  67. }
  68. camera.updateMatrixWorld();
  69. var stageParameters = device.stageParameters;
  70. if ( this.standing && stageParameters ) {
  71. standingMatrix.fromArray( stageParameters.sittingToStandingTransform );
  72. standingMatrixInverse.getInverse( standingMatrix );
  73. camera.matrixWorld.multiply( standingMatrix );
  74. camera.matrixWorldInverse.multiply( standingMatrixInverse );
  75. }
  76. if ( device.isPresenting === false ) return camera;
  77. //
  78. cameraL.near = camera.near;
  79. cameraR.near = camera.near;
  80. cameraL.far = camera.far;
  81. cameraR.far = camera.far;
  82. cameraVR.matrixWorld.copy( camera.matrixWorld );
  83. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  84. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  85. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  86. if ( this.standing && stageParameters ) {
  87. cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
  88. cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
  89. }
  90. var parent = camera.parent;
  91. if ( parent !== null ) {
  92. matrixWorldInverse.getInverse( parent.matrixWorld );
  93. cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
  94. cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
  95. }
  96. // envMap and Mirror needs camera.matrixWorld
  97. cameraL.matrixWorld.getInverse( cameraL.matrixWorldInverse );
  98. cameraR.matrixWorld.getInverse( cameraR.matrixWorldInverse );
  99. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  100. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  101. // HACK @mrdoob
  102. // https://github.com/w3c/webvr/issues/203
  103. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  104. //
  105. var layers = device.getLayers();
  106. if ( layers.length ) {
  107. var layer = layers[ 0 ];
  108. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  109. cameraL.bounds.fromArray( layer.leftBounds );
  110. }
  111. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  112. cameraR.bounds.fromArray( layer.rightBounds );
  113. }
  114. }
  115. return cameraVR;
  116. };
  117. this.getStandingMatrix = function () {
  118. return standingMatrix;
  119. };
  120. this.submitFrame = function () {
  121. if ( device && device.isPresenting ) device.submitFrame();
  122. };
  123. this.dispose = function () {
  124. window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
  125. };
  126. }
  127. export { WebVRManager };
粤ICP备19079148号