123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- import { Group } from '../../objects/Group.js';
- import { Vector4 } from '../../math/Vector4.js';
- import { ArrayCamera } from '../../cameras/ArrayCamera.js';
- import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
- import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
- function WebXRManager( renderer ) {
- var gl = renderer.context;
- var device = null;
- var session = null;
- var frameOfRef = null;
- var pose = null;
- var controllers = [];
- var inputSources = [];
- function isPresenting() {
- return session !== null && frameOfRef !== null;
- }
- //
- var cameraL = new PerspectiveCamera();
- cameraL.layers.enable( 1 );
- cameraL.viewport = new Vector4();
- var cameraR = new PerspectiveCamera();
- cameraR.layers.enable( 2 );
- cameraR.viewport = new Vector4();
- var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
- cameraVR.layers.enable( 1 );
- cameraVR.layers.enable( 2 );
- //
- this.enabled = false;
- this.getController = function ( id ) {
- var controller = controllers[ id ];
- if ( controller === undefined ) {
- controller = new Group();
- controller.matrixAutoUpdate = false;
- controller.visible = false;
- controllers[ id ] = controller;
- }
- return controller;
- };
- this.getDevice = function () {
- return device;
- };
- this.setDevice = function ( value ) {
- if ( value !== undefined ) device = value;
- gl.setCompatibleXRDevice( value );
- };
- //
- function onSessionEvent( event ) {
- var controller = controllers[ inputSources.indexOf( event.inputSource ) ];
- if ( controller ) controller.dispatchEvent( { type: event.type } );
- }
- function onSessionEnd() {
- renderer.setFramebuffer( null );
- animation.stop();
- }
- this.setSession = function ( value, options ) {
- session = value;
- if ( session !== null ) {
- session.addEventListener( 'select', onSessionEvent );
- session.addEventListener( 'selectstart', onSessionEvent );
- session.addEventListener( 'selectend', onSessionEvent );
- session.addEventListener( 'end', onSessionEnd );
- session.baseLayer = new XRWebGLLayer( session, gl );
- session.requestFrameOfReference( options.frameOfReferenceType ).then( function ( value ) {
- frameOfRef = value;
- renderer.setFramebuffer( session.baseLayer.framebuffer );
- animation.setContext( session );
- animation.start();
- } );
- //
- inputSources = session.getInputSources();
- session.addEventListener( 'inputsourceschange', function () {
- inputSources = session.getInputSources();
- console.log( inputSources );
- } );
- }
- };
- function updateCamera( camera, parent ) {
- if ( parent === null ) {
- camera.matrixWorld.copy( camera.matrix );
- } else {
- camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix );
- }
- camera.matrixWorldInverse.getInverse( camera.matrixWorld );
- }
- this.getCamera = function ( camera ) {
- if ( isPresenting() ) {
- var parent = camera.parent;
- var cameras = cameraVR.cameras;
- // apply camera.parent to cameraVR
- updateCamera( cameraVR, parent );
- for ( var i = 0; i < cameras.length; i ++ ) {
- updateCamera( cameras[ i ], parent );
- }
- // update camera and its children
- camera.matrixWorld.copy( cameraVR.matrixWorld );
- var children = camera.children;
- for ( var i = 0, l = children.length; i < l; i ++ ) {
- children[ i ].updateMatrixWorld( true );
- }
- return cameraVR;
- }
- return camera;
- };
- this.isPresenting = isPresenting;
- // Animation Loop
- var onAnimationFrameCallback = null;
- function onAnimationFrame( time, frame ) {
- pose = frame.getDevicePose( frameOfRef );
- if ( pose !== null ) {
- var layer = session.baseLayer;
- var views = frame.views;
- for ( var i = 0; i < views.length; i ++ ) {
- var view = views[ i ];
- var viewport = layer.getViewport( view );
- var viewMatrix = pose.getViewMatrix( view );
- var camera = cameraVR.cameras[ i ];
- camera.matrix.fromArray( viewMatrix ).getInverse( camera.matrix );
- camera.projectionMatrix.fromArray( view.projectionMatrix );
- camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height );
- if ( i === 0 ) {
- cameraVR.matrix.copy( camera.matrix );
- // HACK (mrdoob)
- // https://github.com/w3c/webvr/issues/203
- cameraVR.projectionMatrix.copy( camera.projectionMatrix );
- }
- }
- }
- //
- for ( var i = 0; i < controllers.length; i ++ ) {
- var controller = controllers[ i ];
- var inputSource = inputSources[ i ];
- if ( inputSource ) {
- var inputPose = frame.getInputPose( inputSource, frameOfRef );
- if ( inputPose !== null ) {
- controller.matrix.elements = inputPose.gripMatrix;
- controller.matrix.decompose( controller.position, controller.rotation, controller.scale );
- controller.visible = true;
- continue;
- }
- }
- controller.visible = false;
- }
- if ( onAnimationFrameCallback ) onAnimationFrameCallback( time );
- }
- var animation = new WebGLAnimation();
- animation.setAnimationLoop( onAnimationFrame );
- this.setAnimationLoop = function ( callback ) {
- onAnimationFrameCallback = callback;
- };
- this.dispose = function () {};
- // DEPRECATED
- this.getStandingMatrix = function () {
- console.warn( 'THREE.WebXRManager: getStandingMatrix() is no longer needed.' );
- return new THREE.Matrix4();
- };
- this.submitFrame = function () {};
- }
- export { WebXRManager };
|