WebVRManager.js 5.8 KB

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