123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- import { Group } from '../../objects/Group.js';
- import { Matrix4 } from '../../math/Matrix4.js';
- import { Vector3 } from '../../math/Vector3.js';
- import { Vector4 } from '../../math/Vector4.js';
- import { Quaternion } from '../../math/Quaternion.js';
- import { ArrayCamera } from '../../cameras/ArrayCamera.js';
- import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
- import { WebGLAnimation } from '../webgl/WebGLAnimation.js';
- function WebVRManager( renderer ) {
- var scope = this;
- var device = null;
- var frameData = null;
- var poseTarget = null;
- var controllers = [];
- var standingMatrix = new Matrix4();
- var standingMatrixInverse = new Matrix4();
- if ( typeof window !== 'undefined' && 'VRFrameData' in window ) {
- frameData = new window.VRFrameData();
- window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
- }
- var matrixWorldInverse = new Matrix4();
- var tempQuaternion = new Quaternion();
- var tempPosition = new Vector3();
- var cameraL = new PerspectiveCamera();
- cameraL.bounds = new Vector4( 0.0, 0.0, 0.5, 1.0 );
- cameraL.layers.enable( 1 );
- var cameraR = new PerspectiveCamera();
- cameraR.bounds = new Vector4( 0.5, 0.0, 0.5, 1.0 );
- cameraR.layers.enable( 2 );
- var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
- cameraVR.layers.enable( 1 );
- cameraVR.layers.enable( 2 );
- //
- function isPresenting() {
- return device !== null && device.isPresenting === true;
- }
- var currentSize, currentPixelRatio;
- function onVRDisplayPresentChange() {
- if ( isPresenting() ) {
- var eyeParameters = device.getEyeParameters( 'left' );
- var renderWidth = eyeParameters.renderWidth;
- var renderHeight = eyeParameters.renderHeight;
- currentPixelRatio = renderer.getPixelRatio();
- currentSize = renderer.getSize();
- renderer.setDrawingBufferSize( renderWidth * 2, renderHeight, 1 );
- animation.start();
- } else if ( scope.enabled ) {
- renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio );
- animation.stop();
- }
- }
- //
- var triggers = [];
- function findGamepad( id ) {
- var gamepads = navigator.getGamepads && navigator.getGamepads();
- for ( var i = 0, j = 0, l = gamepads.length; i < l; i ++ ) {
- var gamepad = gamepads[ i ];
- if ( gamepad && ( gamepad.id === 'Daydream Controller' ||
- gamepad.id === 'Gear VR Controller' || gamepad.id === 'Oculus Go Controller' ||
- gamepad.id === 'OpenVR Gamepad' || gamepad.id.startsWith( 'Oculus Touch' ) ||
- gamepad.id.startsWith( 'Spatial Controller' ) ) ) {
- if ( j === id ) return gamepad;
- j ++;
- }
- }
- }
- function updateControllers() {
- for ( var i = 0; i < controllers.length; i ++ ) {
- var controller = controllers[ i ];
- var gamepad = findGamepad( i );
- if ( gamepad !== undefined && gamepad.pose !== undefined ) {
- if ( gamepad.pose === null ) return;
- // Pose
- var pose = gamepad.pose;
- if ( pose.hasPosition === false ) controller.position.set( 0.2, - 0.6, - 0.05 );
- if ( pose.position !== null ) controller.position.fromArray( pose.position );
- if ( pose.orientation !== null ) controller.quaternion.fromArray( pose.orientation );
- controller.matrix.compose( controller.position, controller.quaternion, controller.scale );
- controller.matrix.premultiply( standingMatrix );
- controller.matrix.decompose( controller.position, controller.quaternion, controller.scale );
- controller.matrixWorldNeedsUpdate = true;
- controller.visible = true;
- // Trigger
- var buttonId = gamepad.id === 'Daydream Controller' ? 0 : 1;
- if ( triggers[ i ] !== gamepad.buttons[ buttonId ].pressed ) {
- triggers[ i ] = gamepad.buttons[ buttonId ].pressed;
- if ( triggers[ i ] === true ) {
- controller.dispatchEvent( { type: 'selectstart' } );
- } else {
- controller.dispatchEvent( { type: 'selectend' } );
- controller.dispatchEvent( { type: 'select' } );
- }
- }
- } else {
- controller.visible = false;
- }
- }
- }
- //
- this.enabled = false;
- this.userHeight = 1.6;
- 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;
- animation.setContext( value );
- };
- this.setPoseTarget = function ( object ) {
- if ( object !== undefined ) poseTarget = object;
- };
- this.getCamera = function ( camera ) {
- if ( device === null ) {
- camera.position.set( 0, scope.userHeight, 0 );
- return camera;
- }
- device.depthNear = camera.near;
- device.depthFar = camera.far;
- device.getFrameData( frameData );
- //
- var stageParameters = device.stageParameters;
- if ( stageParameters ) {
- standingMatrix.fromArray( stageParameters.sittingToStandingTransform );
- } else {
- standingMatrix.makeTranslation( 0, scope.userHeight, 0 );
- }
- var pose = frameData.pose;
- var poseObject = poseTarget !== null ? poseTarget : camera;
- // We want to manipulate poseObject by its position and quaternion components since users may rely on them.
- poseObject.matrix.copy( standingMatrix );
- poseObject.matrix.decompose( poseObject.position, poseObject.quaternion, poseObject.scale );
- if ( pose.orientation !== null ) {
- tempQuaternion.fromArray( pose.orientation );
- poseObject.quaternion.multiply( tempQuaternion );
- }
- if ( pose.position !== null ) {
- tempQuaternion.setFromRotationMatrix( standingMatrix );
- tempPosition.fromArray( pose.position );
- tempPosition.applyQuaternion( tempQuaternion );
- poseObject.position.add( tempPosition );
- }
- poseObject.updateMatrixWorld();
- if ( device.isPresenting === false ) return camera;
- //
- cameraL.near = camera.near;
- cameraR.near = camera.near;
- cameraL.far = camera.far;
- cameraR.far = camera.far;
- cameraVR.matrixWorld.copy( camera.matrixWorld );
- cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
- cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
- cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
- // TODO (mrdoob) Double check this code
- standingMatrixInverse.getInverse( standingMatrix );
- cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
- cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
- var parent = poseObject.parent;
- if ( parent !== null ) {
- matrixWorldInverse.getInverse( parent.matrixWorld );
- cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
- cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
- }
- // envMap and Mirror needs camera.matrixWorld
- cameraL.matrixWorld.getInverse( cameraL.matrixWorldInverse );
- cameraR.matrixWorld.getInverse( cameraR.matrixWorldInverse );
- cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
- cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
- // HACK (mrdoob)
- // https://github.com/w3c/webvr/issues/203
- cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
- //
- var layers = device.getLayers();
- if ( layers.length ) {
- var layer = layers[ 0 ];
- if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
- cameraL.bounds.fromArray( layer.leftBounds );
- }
- if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
- cameraR.bounds.fromArray( layer.rightBounds );
- }
- }
- updateControllers();
- return cameraVR;
- };
- this.getStandingMatrix = function () {
- return standingMatrix;
- };
- this.isPresenting = isPresenting;
- // Animation Loop
- var animation = new WebGLAnimation();
- this.setAnimationLoop = function ( callback ) {
- animation.setAnimationLoop( callback );
- };
- this.submitFrame = function () {
- if ( isPresenting() ) device.submitFrame();
- };
- this.dispose = function () {
- if ( typeof window !== 'undefined' ) {
- window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
- }
- };
- }
- export { WebVRManager };
|