VRControls.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. function gotVRDevices( devices ) {
  9. for ( var i = 0; i < devices.length; i ++ ) {
  10. if ( ( 'VRDisplay' in window && devices[ i ] instanceof VRDisplay ) ||
  11. ( 'PositionSensorVRDevice' in window && devices[ i ] instanceof PositionSensorVRDevice ) ) {
  12. vrInput = devices[ i ];
  13. break; // We keep the first we encounter
  14. }
  15. }
  16. if ( !vrInput ) {
  17. if ( onError ) onError( 'VR input not available.' );
  18. }
  19. }
  20. if ( navigator.getVRDisplays ) {
  21. navigator.getVRDisplays().then( gotVRDevices );
  22. } else if ( navigator.getVRDevices ) {
  23. // Deprecated API.
  24. navigator.getVRDevices().then( gotVRDevices );
  25. }
  26. // the Rift SDK returns the position in meters
  27. // this scale factor allows the user to define how meters
  28. // are converted to scene units.
  29. this.scale = 1;
  30. this.update = function () {
  31. if ( vrInput ) {
  32. if ( vrInput.getPose ) {
  33. var pose = vrInput.getPose();
  34. if ( pose.orientation !== null ) {
  35. object.quaternion.fromArray( pose.orientation );
  36. }
  37. if ( pose.position !== null ) {
  38. object.position.fromArray( pose.position ).multiplyScalar( scope.scale );
  39. }
  40. } else {
  41. // Deprecated API.
  42. var state = vrInput.getState();
  43. if ( state.orientation !== null ) {
  44. object.quaternion.copy( state.orientation );
  45. }
  46. if ( state.position !== null ) {
  47. object.position.copy( state.position ).multiplyScalar( scope.scale );
  48. }
  49. }
  50. }
  51. };
  52. this.resetSensor = function () {
  53. if ( vrInput ) {
  54. if ( vrInput.resetPose !== undefined ) {
  55. vrInput.resetPose();
  56. } else if ( vrInput.resetSensor !== undefined ) {
  57. // Deprecated API.
  58. vrInput.resetSensor();
  59. } else if ( vrInput.zeroSensor !== undefined ) {
  60. // Really deprecated API.
  61. vrInput.zeroSensor();
  62. }
  63. }
  64. };
  65. this.zeroSensor = function () {
  66. console.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
  67. this.resetSensor();
  68. };
  69. this.dispose = function () {
  70. vrInput = null;
  71. };
  72. };