WebVRManager.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. function WebVRManager( renderer ) {
  5. var scope = this;
  6. var device = null;
  7. var frameData = null;
  8. if ( 'VRFrameData' in window ) {
  9. frameData = new window.VRFrameData();
  10. }
  11. var matrixWorldInverse = new Matrix4();
  12. var standingMatrix = new Matrix4();
  13. var standingMatrixInverse = new Matrix4();
  14. var cameraL = new PerspectiveCamera();
  15. cameraL.bounds = new Vector4( 0.0, 0.0, 0.5, 1.0 );
  16. cameraL.layers.enable( 1 );
  17. var cameraR = new PerspectiveCamera();
  18. cameraR.bounds = new Vector4( 0.5, 0.0, 0.5, 1.0 );
  19. cameraR.layers.enable( 2 );
  20. var cameraVR = new ArrayCamera( [ cameraL, cameraR ] );
  21. //
  22. var currentSize, currentPixelRatio;
  23. function onVRDisplayPresentChange() {
  24. if ( device.isPresenting ) {
  25. var eyeParameters = device.getEyeParameters( 'left' );
  26. var renderWidth = eyeParameters.renderWidth;
  27. var renderHeight = eyeParameters.renderHeight;
  28. currentPixelRatio = renderer.getPixelRatio();
  29. currentSize = renderer.getSize();
  30. renderer.setPixelRatio( 1 );
  31. renderer.setSize( renderWidth * 2, renderHeight, false );
  32. } else if ( scope.enabled ) {
  33. renderer.setPixelRatio( currentPixelRatio );
  34. renderer.setSize( currentSize.width, currentSize.height, true );
  35. }
  36. }
  37. window.addEventListener( 'vrdisplaypresentchange', onVRDisplayPresentChange, false );
  38. //
  39. this.enabled = false;
  40. this.standing = false;
  41. this.getDevice = function () {
  42. return device;
  43. };
  44. this.setDevice = function ( value ) {
  45. if ( value !== undefined ) device = value;
  46. };
  47. this.getCamera = function ( camera ) {
  48. if ( device === null ) return camera;
  49. device.depthNear = camera.near;
  50. device.depthFar = camera.far;
  51. device.getFrameData( frameData );
  52. //
  53. var pose = frameData.pose;
  54. if ( pose.position !== null ) {
  55. camera.position.fromArray( pose.position );
  56. } else {
  57. camera.position.set( 0, 0, 0 );
  58. }
  59. if ( pose.orientation !== null ) {
  60. camera.quaternion.fromArray( pose.orientation );
  61. }
  62. camera.updateMatrixWorld();
  63. var stageParameters = device.stageParameters;
  64. if ( this.standing && stageParameters ) {
  65. standingMatrix.fromArray( stageParameters.sittingToStandingTransform );
  66. standingMatrixInverse.getInverse( standingMatrix );
  67. camera.matrixWorld.multiply( standingMatrix );
  68. camera.matrixWorldInverse.multiply( standingMatrixInverse );
  69. }
  70. if ( device.isPresenting === false ) return camera;
  71. //
  72. cameraVR.matrixWorld.copy( camera.matrixWorld );
  73. cameraVR.matrixWorldInverse.copy( camera.matrixWorldInverse );
  74. cameraL.matrixWorldInverse.fromArray( frameData.leftViewMatrix );
  75. cameraR.matrixWorldInverse.fromArray( frameData.rightViewMatrix );
  76. if ( this.standing && stageParameters ) {
  77. cameraL.matrixWorldInverse.multiply( standingMatrixInverse );
  78. cameraR.matrixWorldInverse.multiply( standingMatrixInverse );
  79. }
  80. var parent = camera.parent;
  81. if ( parent !== null ) {
  82. matrixWorldInverse.getInverse( parent.matrixWorld );
  83. cameraL.matrixWorldInverse.multiply( matrixWorldInverse );
  84. cameraR.matrixWorldInverse.multiply( matrixWorldInverse );
  85. }
  86. cameraL.projectionMatrix.fromArray( frameData.leftProjectionMatrix );
  87. cameraR.projectionMatrix.fromArray( frameData.rightProjectionMatrix );
  88. // HACK @mrdoob
  89. // https://github.com/w3c/webvr/issues/203
  90. cameraVR.projectionMatrix.copy( cameraL.projectionMatrix );
  91. //
  92. var layers = device.getLayers();
  93. if ( layers.length ) {
  94. var layer = layers[ 0 ];
  95. if ( layer.leftBounds !== null && layer.leftBounds.length === 4 ) {
  96. cameraL.bounds.fromArray( layer.leftBounds );
  97. }
  98. if ( layer.rightBounds !== null && layer.rightBounds.length === 4 ) {
  99. cameraR.bounds.fromArray( layer.rightBounds );
  100. }
  101. }
  102. return cameraVR;
  103. };
  104. this.getStandingMatrix = function () {
  105. return standingMatrix;
  106. };
  107. this.submitFrame = function () {
  108. if ( device && device.isPresenting ) device.submitFrame();
  109. };
  110. }
  111. export { WebVRManager };