VRControls.js 3.0 KB

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