WebXRManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Vector4 } from '../../math/Vector4.js';
  5. import { ArrayCamera } from '../../cameras/ArrayCamera.js';
  6. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
  7. import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
  8. function WebXRManager( renderer ) {
  9. var gl = renderer.context;
  10. var device = null;
  11. var session = null;
  12. var frameOfRef = null;
  13. var pose = null;
  14. function isPresenting() {
  15. return session !== null && frameOfRef !== null;
  16. }
  17. //
  18. var cameraL = new PerspectiveCamera();
  19. cameraL.layers.enable( 1 );
  20. cameraL.viewport = new Vector4();
  21. var cameraR = new PerspectiveCamera();
  22. cameraR.layers.enable( 2 );
  23. cameraR.viewport = new Vector4();
  24. var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
  25. cameraVR.layers.enable( 1 );
  26. cameraVR.layers.enable( 2 );
  27. //
  28. this.enabled = false;
  29. this.getDevice = function () {
  30. return device;
  31. };
  32. this.setDevice = function ( value ) {
  33. if ( value !== undefined ) device = value;
  34. gl.setCompatibleXRDevice( value );
  35. };
  36. //
  37. this.setSession = function ( value, options ) {
  38. session = value;
  39. if ( session !== null ) {
  40. session.addEventListener( 'end', function () {
  41. renderer.setFramebuffer( null );
  42. animation.stop();
  43. } );
  44. session.baseLayer = new XRWebGLLayer( session, gl );
  45. session.requestFrameOfReference( options.frameOfReferenceType ).then( function ( value ) {
  46. frameOfRef = value;
  47. renderer.setFramebuffer( session.baseLayer.framebuffer );
  48. animation.setContext( session );
  49. animation.start();
  50. } );
  51. }
  52. };
  53. function updateCamera( camera, parent ) {
  54. if ( parent === null ) {
  55. camera.matrixWorld.copy( camera.matrix );
  56. } else {
  57. camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
  58. }
  59. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  60. }
  61. this.getCamera = function ( camera ) {
  62. if ( isPresenting() ) {
  63. var parent = camera.parent;
  64. var cameras = cameraVR.cameras;
  65. // apply camera.parent to cameraVR
  66. updateCamera( cameraVR, parent );
  67. for ( var i = 0; i < cameras.length; i ++ ) {
  68. updateCamera( cameras[ i ], parent );
  69. }
  70. // update camera and its children
  71. camera.matrixWorld.copy( cameraVR.matrixWorld );
  72. var children = camera.children;
  73. for ( var i = 0, l = children.length; i < l; i ++ ) {
  74. children[ i ].updateMatrixWorld( true );
  75. }
  76. return cameraVR;
  77. }
  78. return camera;
  79. };
  80. this.isPresenting = isPresenting;
  81. // Animation Loop
  82. var onAnimationFrameCallback = null;
  83. function onAnimationFrame( time, frame ) {
  84. pose = frame.getDevicePose( frameOfRef );
  85. var layer = session.baseLayer;
  86. var views = frame.views;
  87. for ( var i = 0; i < views.length; i ++ ) {
  88. var view = views[ i ];
  89. var viewport = layer.getViewport( view );
  90. var viewMatrix = pose.getViewMatrix( view );
  91. var camera = cameraVR.cameras[ i ];
  92. camera.matrix.fromArray( viewMatrix ).getInverse( camera.matrix );
  93. camera.projectionMatrix.fromArray( view.projectionMatrix );
  94. camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
  95. if ( i === 0 ) {
  96. cameraVR.matrix.copy( camera.matrix );
  97. // HACK (mrdoob)
  98. // https://github.com/w3c/webvr/issues/203
  99. cameraVR.projectionMatrix.copy( camera.projectionMatrix );
  100. }
  101. }
  102. if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
  103. }
  104. var animation = new WebGLAnimation();
  105. animation.setAnimationLoop( onAnimationFrame );
  106. this.setAnimationLoop = function ( callback ) {
  107. onAnimationFrameCallback = callback;
  108. };
  109. this.dispose = function () {};
  110. // DEPRECATED
  111. this.getStandingMatrix = function () {
  112. console.warn( 'THREE.WebXRManager: getStandingMatrix() is no longer needed.' );
  113. return new THREE.Matrix4();
  114. };
  115. this.submitFrame = function () {};
  116. }
  117. export { WebXRManager };