WebVRManager.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. } else {
  88. poseObject.position.set( 0, 0, 0 );
  89. }
  90. poseObject.updateMatrixWorld();
  91. if ( device.isPresenting === false ) return camera;
  92. //
  93. cameraL.near = camera.near;
  94. cameraR.near = camera.near;
  95. cameraL.far = camera.far;
  96. cameraR.far = camera.far;
  97. cameraVR.matrixWorld.copy( camera.matrixWorld );
  98. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  99. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  100. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  101. // TODO (mrdoob) Double check this code
  102. standingMatrixInverse.getInverse( standingMatrix );
  103. cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
  104. cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
  105. var parent = poseObject.parent;
  106. if ( parent !== null ) {
  107. matrixWorldInverse.getInverse( parent.matrixWorld );
  108. cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
  109. cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
  110. }
  111. // envMap and Mirror needs camera.matrixWorld
  112. cameraL.matrixWorld.getInverse( cameraL.matrixWorldInverse );
  113. cameraR.matrixWorld.getInverse( cameraR.matrixWorldInverse );
  114. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  115. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  116. // HACK (mrdoob)
  117. // https://github.com/w3c/webvr/issues/203
  118. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  119. //
  120. var layers = device.getLayers();
  121. if ( layers.length ) {
  122. var layer = layers[ 0 ];
  123. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  124. cameraL.bounds.fromArray( layer.leftBounds );
  125. }
  126. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  127. cameraR.bounds.fromArray( layer.rightBounds );
  128. }
  129. }
  130. return cameraVR;
  131. };
  132. this.getStandingMatrix = function () {
  133. return standingMatrix;
  134. };
  135. this.submitFrame = function () {
  136. if ( device && device.isPresenting ) device.submitFrame();
  137. };
  138. this.dispose = function () {
  139. if ( typeof window !== 'undefined' ) {
  140. window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
  141. }
  142. };
  143. }
  144. export { WebVRManager };
粤ICP备19079148号