WebXRManager.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { Group } from '../../objects/Group.js';
  5. import { Vector4 } from '../../math/Vector4.js';
  6. import { ArrayCamera } from '../../cameras/ArrayCamera.js';
  7. import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
  8. import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
  9. function WebXRManager( renderer ) {
  10. var gl = renderer.context;
  11. var device = null;
  12. var session = null;
  13. var frameOfReference = null;
  14. var frameOfReferenceType = 'stage';
  15. var pose = null;
  16. var controllers = [];
  17. var inputSources = [];
  18. function isPresenting() {
  19. return session !== null && frameOfReference !== 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.getController = function ( id ) {
  34. var controller = controllers[ id ];
  35. if ( controller === undefined ) {
  36. controller = new Group();
  37. controller.matrixAutoUpdate = false;
  38. controller.visible = false;
  39. controllers[ id ] = controller;
  40. }
  41. return controller;
  42. };
  43. this.getDevice = function () {
  44. return device;
  45. };
  46. this.setDevice = function ( value ) {
  47. if ( value !== undefined ) device = value;
  48. if ( value instanceof XRDevice ) gl.setCompatibleXRDevice( value );
  49. };
  50. //
  51. function onSessionEvent( event ) {
  52. var controller = controllers[ inputSources.indexOf( event.inputSource ) ];
  53. if ( controller ) controller.dispatchEvent( { type: event.type } );
  54. }
  55. function onSessionEnd() {
  56. renderer.setFramebuffer( null );
  57. animation.stop();
  58. }
  59. this.setFrameOfReferenceType = function ( value ) {
  60. frameOfReferenceType = value;
  61. };
  62. this.setSession = function ( value ) {
  63. session = value;
  64. if ( session !== null ) {
  65. session.addEventListener( 'select', onSessionEvent );
  66. session.addEventListener( 'selectstart', onSessionEvent );
  67. session.addEventListener( 'selectend', onSessionEvent );
  68. session.addEventListener( 'end', onSessionEnd );
  69. session.baseLayer = new XRWebGLLayer( session, gl );
  70. session.requestFrameOfReference( frameOfReferenceType ).then( function ( value ) {
  71. frameOfReference = value;
  72. renderer.setFramebuffer( session.baseLayer.framebuffer );
  73. animation.setContext( session );
  74. animation.start();
  75. } );
  76. //
  77. inputSources = session.getInputSources();
  78. session.addEventListener( 'inputsourceschange', function () {
  79. inputSources = session.getInputSources();
  80. console.log( inputSources );
  81. } );
  82. }
  83. };
  84. function updateCamera( camera, parent ) {
  85. if ( parent === null ) {
  86. camera.matrixWorld.copy( camera.matrix );
  87. } else {
  88. camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
  89. }
  90. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  91. }
  92. this.getCamera = function ( camera ) {
  93. if ( isPresenting() ) {
  94. var parent = camera.parent;
  95. var cameras = cameraVR.cameras;
  96. updateCamera( cameraVR, parent );
  97. for ( var i = 0; i < cameras.length; i ++ ) {
  98. updateCamera( cameras[ i ], parent );
  99. }
  100. // update camera and its children
  101. camera.matrixWorld.copy( cameraVR.matrixWorld );
  102. var children = camera.children;
  103. for ( var i = 0, l = children.length; i < l; i ++ ) {
  104. children[ i ].updateMatrixWorld( true );
  105. }
  106. cameraVR.setProjectionFromUnion();
  107. return cameraVR;
  108. }
  109. return camera;
  110. };
  111. this.isPresenting = isPresenting;
  112. // Animation Loop
  113. var onAnimationFrameCallback = null;
  114. function onAnimationFrame( time, frame ) {
  115. pose = frame.getDevicePose( frameOfReference );
  116. if ( pose !== null ) {
  117. var layer = session.baseLayer;
  118. var views = frame.views;
  119. for ( var i = 0; i < views.length; i ++ ) {
  120. var view = views[ i ];
  121. var viewport = layer.getViewport( view );
  122. var viewMatrix = pose.getViewMatrix( view );
  123. var camera = cameraVR.cameras[ i ];
  124. camera.matrix.fromArray( viewMatrix ).getInverse( camera.matrix );
  125. camera.projectionMatrix.fromArray( view.projectionMatrix );
  126. camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
  127. if ( i === 0 ) {
  128. cameraVR.matrix.copy( camera.matrix );
  129. }
  130. }
  131. }
  132. //
  133. for ( var i = 0; i < controllers.length; i ++ ) {
  134. var controller = controllers[ i ];
  135. var inputSource = inputSources[ i ];
  136. if ( inputSource ) {
  137. var inputPose = frame.getInputPose( inputSource, frameOfReference );
  138. if ( inputPose !== null ) {
  139. if ( 'targetRay' in inputPose ) {
  140. controller.matrix.elements = inputPose.targetRay.transformMatrix;
  141. } else if ( 'pointerMatrix' in inputPose ) {
  142. // DEPRECATED
  143. controller.matrix.elements = inputPose.pointerMatrix;
  144. }
  145. controller.matrix.decompose( controller.position, controller.rotation, controller.scale );
  146. controller.visible = true;
  147. continue;
  148. }
  149. }
  150. controller.visible = false;
  151. }
  152. if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
  153. }
  154. var animation = new WebGLAnimation();
  155. animation.setAnimationLoop( onAnimationFrame );
  156. this.setAnimationLoop = function ( callback ) {
  157. onAnimationFrameCallback = callback;
  158. };
  159. this.dispose = function () {};
  160. // DEPRECATED
  161. this.getStandingMatrix = function () {
  162. console.warn( 'THREE.WebXRManager: getStandingMatrix() is no longer needed.' );
  163. return new THREE.Matrix4();
  164. };
  165. this.submitFrame = function () {};
  166. }
  167. export { WebXRManager };