VRControls.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, onError ) {
  6. var scope = this;
  7. var vrDisplay, vrDisplays;
  8. var standingMatrix = new THREE.Matrix4();
  9. function gotVRDisplays( displays ) {
  10. vrDisplays = displays;
  11. for ( var i = 0; i < displays.length; i ++ ) {
  12. if ( ( 'VRDisplay' in window && displays[ i ] instanceof VRDisplay ) ||
  13. ( 'PositionSensorVRDevice' in window && displays[ i ] instanceof PositionSensorVRDevice ) ) {
  14. vrDisplay = displays[ i ];
  15. break; // We keep the first we encounter
  16. }
  17. }
  18. if ( vrDisplay === undefined ) {
  19. if ( onError ) onError( 'VR input not available.' );
  20. }
  21. }
  22. if ( navigator.getVRDisplays ) {
  23. navigator.getVRDisplays().then( gotVRDisplays );
  24. } else if ( navigator.getVRDevices ) {
  25. // Deprecated API.
  26. navigator.getVRDevices().then( gotVRDisplays );
  27. }
  28. // the Rift SDK returns the position in meters
  29. // this scale factor allows the user to define how meters
  30. // are converted to scene units.
  31. this.scale = 1;
  32. // If true will use "standing space" coordinate system where y=0 is the
  33. // floor and x=0, z=0 is the center of the room.
  34. this.standing = false;
  35. // Distance from the users eyes to the floor in meters. Used when
  36. // standing=true but the VRDisplay doesn't provide stageParameters.
  37. this.userHeight = 1.6;
  38. this.getVRDisplay = function () {
  39. return vrDisplay;
  40. };
  41. this.getVRDisplays = function () {
  42. return vrDisplays;
  43. };
  44. this.getStandingMatrix = function () {
  45. return standingMatrix;
  46. };
  47. this.update = function () {
  48. if ( vrDisplay ) {
  49. if ( vrDisplay.getPose ) {
  50. var pose = vrDisplay.getPose();
  51. if ( pose.orientation !== null ) {
  52. object.quaternion.fromArray( pose.orientation );
  53. }
  54. if ( pose.position !== null ) {
  55. object.position.fromArray( pose.position );
  56. } else {
  57. object.position.set( 0, 0, 0 );
  58. }
  59. } else {
  60. // Deprecated API.
  61. var state = vrDisplay.getState();
  62. if ( state.orientation !== null ) {
  63. object.quaternion.copy( state.orientation );
  64. }
  65. if ( state.position !== null ) {
  66. object.position.copy( state.position );
  67. } else {
  68. object.position.set( 0, 0, 0 );
  69. }
  70. }
  71. if ( this.standing ) {
  72. if ( vrDisplay.stageParameters ) {
  73. object.updateMatrix();
  74. standingMatrix.fromArray( vrDisplay.stageParameters.sittingToStandingTransform );
  75. object.applyMatrix( standingMatrix );
  76. } else {
  77. object.position.setY( object.position.y + this.userHeight );
  78. }
  79. }
  80. object.position.multiplyScalar( scope.scale );
  81. }
  82. };
  83. this.resetPose = function () {
  84. if ( vrDisplay ) {
  85. if ( vrDisplay.resetPose !== undefined ) {
  86. vrDisplay.resetPose();
  87. } else if ( vrDisplay.resetSensor !== undefined ) {
  88. // Deprecated API.
  89. vrDisplay.resetSensor();
  90. } else if ( vrDisplay.zeroSensor !== undefined ) {
  91. // Really deprecated API.
  92. vrDisplay.zeroSensor();
  93. }
  94. }
  95. };
  96. this.resetSensor = function () {
  97. console.warn( 'THREE.VRControls: .resetSensor() is now .resetPose().' );
  98. this.resetPose();
  99. };
  100. this.zeroSensor = function () {
  101. console.warn( 'THREE.VRControls: .zeroSensor() is now .resetPose().' );
  102. this.resetPose();
  103. };
  104. this.dispose = function () {
  105. vrDisplay = null;
  106. };
  107. };