|
| 1 | +/** |
| 2 | + * @author mrdoob / http://mrdoob.com/ |
| 3 | + */ |
| 4 | + |
| 5 | +import { Matrix4 } from '../../math/Matrix4.js'; |
| 6 | +import { Vector4 } from '../../math/Vector4.js'; |
| 7 | +import { Vector3 } from '../../math/Vector3.js'; |
| 8 | +import { Quaternion } from '../../math/Quaternion.js'; |
| 9 | +import { ArrayCamera } from '../../cameras/ArrayCamera.js'; |
| 10 | +import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js'; |
| 11 | + |
| 12 | +function WebXRManager( gl ) { |
| 13 | + |
| 14 | + var scope = this; |
| 15 | + |
| 16 | + var device = null; |
| 17 | + var session = null; |
| 18 | + |
| 19 | + var frameOfRef = null; |
| 20 | + var isExclusive = false; |
| 21 | + |
| 22 | + var pose = null; |
| 23 | + |
| 24 | + function isPresenting() { |
| 25 | + |
| 26 | + return session !== null && frameOfRef !== null; |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + // |
| 31 | + |
| 32 | + var cameraL = new PerspectiveCamera(); |
| 33 | + cameraL.layers.enable( 1 ); |
| 34 | + cameraL.viewport = new Vector4(); |
| 35 | + |
| 36 | + var cameraR = new PerspectiveCamera(); |
| 37 | + cameraR.layers.enable( 2 ); |
| 38 | + cameraR.viewport = new Vector4(); |
| 39 | + |
| 40 | + var cameraVR = new ArrayCamera( [ cameraL, cameraR ] ); |
| 41 | + cameraVR.layers.enable( 1 ); |
| 42 | + cameraVR.layers.enable( 2 ); |
| 43 | + |
| 44 | + // |
| 45 | + |
| 46 | + this.enabled = false; |
| 47 | + |
| 48 | + this.getDevice = function () { |
| 49 | + |
| 50 | + return device; |
| 51 | + |
| 52 | + }; |
| 53 | + |
| 54 | + this.setDevice = function ( value ) { |
| 55 | + |
| 56 | + if ( value !== undefined ) device = value; |
| 57 | + |
| 58 | + gl.setCompatibleXRDevice( value ); |
| 59 | + |
| 60 | + }; |
| 61 | + |
| 62 | + this.setSession = function ( value ) { |
| 63 | + |
| 64 | + session = value; |
| 65 | + |
| 66 | + if ( session !== null ) { |
| 67 | + |
| 68 | + session.baseLayer = new XRWebGLLayer( session, gl ); |
| 69 | + session.requestFrameOfReference( 'stage' ).then( function ( value ) { |
| 70 | + |
| 71 | + frameOfRef = value; |
| 72 | + isExclusive = session.exclusive; |
| 73 | + |
| 74 | + console.log( 0 ); |
| 75 | + |
| 76 | + } ); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + }; |
| 81 | + |
| 82 | + this.getCamera = function ( camera ) { |
| 83 | + |
| 84 | + return isPresenting() ? cameraVR : camera; |
| 85 | + |
| 86 | + }; |
| 87 | + |
| 88 | + this.isPresenting = isPresenting; |
| 89 | + |
| 90 | + this.requestAnimationFrame = function ( callback ) { |
| 91 | + |
| 92 | + console.log( 1 ); |
| 93 | + |
| 94 | + function onFrame( time, frame ) { |
| 95 | + |
| 96 | + pose = frame.getDevicePose( frameOfRef ); |
| 97 | + |
| 98 | + var layer = session.baseLayer; |
| 99 | + var views = frame.views; |
| 100 | + |
| 101 | + for ( var i = 0; i < views.length; i ++ ) { |
| 102 | + |
| 103 | + var view = views[ i ]; |
| 104 | + var viewport = layer.getViewport( view ); |
| 105 | + var viewMatrix = pose.getViewMatrix( view ); |
| 106 | + |
| 107 | + var camera = cameraVR.cameras[ i ]; |
| 108 | + camera.projectionMatrix.fromArray( view.projectionMatrix ); |
| 109 | + camera.matrixWorldInverse.fromArray( viewMatrix ); |
| 110 | + camera.matrixWorld.getInverse( camera.matrixWorldInverse ); |
| 111 | + camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height ); |
| 112 | + |
| 113 | + if ( i === 0 ) { |
| 114 | + |
| 115 | + cameraVR.matrixWorld.copy( camera.matrixWorld ); |
| 116 | + cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse ); |
| 117 | + |
| 118 | + // HACK (mrdoob) |
| 119 | + // https://github.com/w3c/webvr/issues/203 |
| 120 | + |
| 121 | + cameraVR.projectionMatrix.copy( camera.projectionMatrix ); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + gl.bindFramebuffer( gl.FRAMEBUFFER, session.baseLayer.framebuffer ); |
| 128 | + |
| 129 | + callback(); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + session.requestAnimationFrame( onFrame ); |
| 134 | + |
| 135 | + }; |
| 136 | + |
| 137 | + this.submitFrame = function () { |
| 138 | + |
| 139 | + // if ( device && device.isPresenting ) device.submitFrame(); |
| 140 | + |
| 141 | + }; |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +export { WebXRManager }; |
0 commit comments