WebVRManager.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. cameraVR.layers.enable( 1 );
  26. cameraVR.layers.enable( 2 );
  27. //
  28. var currentSize, currentPixelRatio;
  29. function onVRDisplayPresentChange() {
  30. if ( 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. 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. cameraL.near = camera.near;
  77. cameraR.near = camera.near;
  78. cameraL.far = camera.far;
  79. cameraR.far = camera.far;
  80. cameraVR.matrixWorld.copy( camera.matrixWorld );
  81. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  82. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  83. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  84. if ( this.standing && stageParameters ) {
  85. cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
  86. cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
  87. }
  88. var parent = camera.parent;
  89. if ( parent !== null ) {
  90. matrixWorldInverse.getInverse( parent.matrixWorld );
  91. cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
  92. cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
  93. }
  94. // envMap and Mirror needs camera.matrixWorld
  95. cameraL.matrixWorld.getInverse( cameraL.matrixWorldInverse );
  96. cameraR.matrixWorld.getInverse( cameraR.matrixWorldInverse );
  97. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  98. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  99. // HACK @mrdoob
  100. // https://github.com/w3c/webvr/issues/203
  101. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  102. //
  103. var layers = device.getLayers();
  104. if ( layers.length ) {
  105. var layer = layers[ 0 ];
  106. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  107. cameraL.bounds.fromArray( layer.leftBounds );
  108. }
  109. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  110. cameraR.bounds.fromArray( layer.rightBounds );
  111. }
  112. }
  113. return cameraVR;
  114. };
  115. this.getStandingMatrix = function () {
  116. return standingMatrix;
  117. };
  118. this.submitFrame = function () {
  119. if ( device && device.isPresenting ) device.submitFrame();
  120. };
  121. this.dispose = function() {
  122. window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
  123. };
  124. }
  125. export { WebVRManager };
粤ICP备19079148号