WebVRManager.js 5.4 KB

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