WebXRManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
  11. function WebXRManager( gl ) {
  12. var scope = this;
  13. var device = null;
  14. var session = null;
  15. var frameOfRef = null;
  16. var isExclusive = false;
  17. var pose = null;
  18. function isPresenting() {
  19. return session !== null && frameOfRef !== null;
  20. }
  21. //
  22. var cameraL = new PerspectiveCamera();
  23. cameraL.layers.enable( 1 );
  24. cameraL.viewport = new Vector4();
  25. var cameraR = new PerspectiveCamera();
  26. cameraR.layers.enable( 2 );
  27. cameraR.viewport = new Vector4();
  28. var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
  29. cameraVR.layers.enable( 1 );
  30. cameraVR.layers.enable( 2 );
  31. //
  32. this.enabled = false;
  33. this.getDevice = function () {
  34. return device;
  35. };
  36. this.setDevice = function ( value ) {
  37. if ( value !== undefined ) device = value;
  38. gl.setCompatibleXRDevice( value );
  39. };
  40. //
  41. this.setSession = function ( value ) {
  42. session = value;
  43. if ( session !== null ) {
  44. session.addEventListener( 'end', function () {
  45. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  46. animation.stop();
  47. } );
  48. session.baseLayer = new XRWebGLLayer( session, gl );
  49. session.requestFrameOfReference( 'stage' ).then( function ( value ) {
  50. frameOfRef = value;
  51. isExclusive = session.exclusive;
  52. animation.setContext( session );
  53. animation.start();
  54. } );
  55. }
  56. };
  57. this.getCamera = function ( camera ) {
  58. return isPresenting() ? cameraVR : camera;
  59. };
  60. this.isPresenting = isPresenting;
  61. // Animation Loop
  62. var onAnimationFrameCallback = null;
  63. function onAnimationFrame( time, frame ) {
  64. pose = frame.getDevicePose( frameOfRef );
  65. var layer = session.baseLayer;
  66. var views = frame.views;
  67. for ( var i = 0; i < views.length; i ++ ) {
  68. var view = views[ i ];
  69. var viewport = layer.getViewport( view );
  70. var viewMatrix = pose.getViewMatrix( view );
  71. var camera = cameraVR.cameras[ i ];
  72. camera.projectionMatrix.fromArray( view.projectionMatrix );
  73. camera.matrixWorldInverse.fromArray( viewMatrix );
  74. camera.matrixWorld.getInverse( camera.matrixWorldInverse );
  75. camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
  76. if ( i === 0 ) {
  77. cameraVR.matrixWorld.copy( camera.matrixWorld );
  78. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  79. // HACK (mrdoob)
  80. // https://github.com/w3c/webvr/issues/203
  81. cameraVR.projectionMatrix.copy( camera.projectionMatrix );
  82. }
  83. }
  84. gl.bindFramebuffer( gl.FRAMEBUFFER, session.baseLayer.framebuffer );
  85. if ( onAnimationFrameCallback ) onAnimationFrameCallback();
  86. }
  87. var animation = new WebGLAnimation();
  88. animation.setAnimationLoop( onAnimationFrame );
  89. this.setAnimationLoop = function ( callback ) {
  90. onAnimationFrameCallback = callback;
  91. };
  92. // DEPRECATED
  93. this.getStandingMatrix = function () {
  94. console.warn( 'THREE.WebXRManager: getStandingMatrix() is no longer needed.' );
  95. return new THREE.Matrix4();
  96. };
  97. this.requestAnimationFrame = function () {};
  98. this.submitFrame = function () {};
  99. }
  100. export { WebXRManager };
粤ICP备19079148号