123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- import { Matrix4 } from '../../math/Matrix4.js';
- import { Vector4 } from '../../math/Vector4.js';
- import { ArrayCamera } from '../../cameras/ArrayCamera.js';
- import { PerspectiveCamera } from '../../cameras/PerspectiveCamera.js';
- function WebVRManager( renderer ) {
- var scope = this;
- var device = null;
- var frameData = null;
- var poseTarget = null;
- var standingMatrix = new Matrix4();
- var standingMatrixInverse = new Matrix4();
- if ( typeof window !== 'undefined' && 'VRFrameData' in window ) {
- frameData = new window.VRFrameData();
- }
- var matrixWorldInverse = new Matrix4();
- 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 );
- //
- var currentSize, currentPixelRatio;
- function onVRDisplayPresentChange() {
- if ( device !== null && device.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 );
- } else if ( scope.enabled ) {
- renderer.setDrawingBufferSize( currentSize.width, currentSize.height, currentPixelRatio );
- }
- }
- if ( typeof window !== 'undefined' ) {
- window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
- }
- //
- this.enabled = false;
- this.userHeight = 1.6;
- this.getDevice = function () {
- return device;
- };
- this.setDevice = function ( value ) {
- if ( value !== undefined ) device = value;
- };
- this.setPoseTarget = function ( object ) {
- if ( object !== undefined ) poseTarget = object;
- };
- this.getCamera = function ( camera ) {
- if ( device === null ) return camera;
- device.depthNear = camera.near;
- device.depthFar = camera.far;
- device.getFrameData( frameData );
- //
- var pose = frameData.pose;
- var poseObject = poseTarget !== null ? poseTarget : camera;
- if ( pose.position !== null ) {
- poseObject.position.fromArray( pose.position );
- } else {
- poseObject.position.set( 0, 0, 0 );
- }
- if ( pose.orientation !== null ) {
- poseObject.quaternion.fromArray( pose.orientation );
- }
- var stageParameters = device.stageParameters;
- if ( stageParameters ) {
- standingMatrix.fromArray( stageParameters.sittingToStandingTransform );
- } else {
- standingMatrix.makeTranslation( 0, scope.userHeight, 0 );
- }
- poseObject.position.applyMatrix4( standingMatrix );
- 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 );
- }
- }
- return cameraVR;
- };
- this.getStandingMatrix = function () {
- return standingMatrix;
- };
- this.submitFrame = function () {
- if ( device && device.isPresenting ) device.submitFrame();
- };
- this.dispose = function () {
- if ( typeof window !== 'undefined' ) {
- window.removeEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange );
- }
- };
- }
- export { WebVRManager };
|