WebXRManager.js 6.1 KB

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