VRControls.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 vrInputs = [];
  8. function gotVRDevices( devices ) {
  9. for ( var i = 0; i < devices.length; i ++ ) {
  10. var device = devices[ i ];
  11. if ( device instanceof PositionSensorVRDevice ) {
  12. vrInputs.push( devices[ i ] );
  13. }
  14. }
  15. if ( onError ) onError( 'HMD not available' );
  16. }
  17. if ( navigator.getVRDevices ) {
  18. navigator.getVRDevices().then( gotVRDevices );
  19. }
  20. // the Rift SDK returns the position in meters
  21. // this scale factor allows the user to define how meters
  22. // are converted to scene units.
  23. this.scale = 1;
  24. this.update = function () {
  25. for ( var i = 0; i < vrInputs.length; i++ ) {
  26. var vrInput = vrInputs[ i ];
  27. var state = vrInput.getState();
  28. if ( state.orientation !== null ) {
  29. object.quaternion.copy( state.orientation );
  30. }
  31. if ( state.position !== null ) {
  32. object.position.copy( state.position ).multiplyScalar( scope.scale );
  33. }
  34. }
  35. };
  36. this.resetSensor = function () {
  37. for ( var i = 0; i < vrInputs.length; i++ ) {
  38. var vrInput = vrInputs[ i ];
  39. if ( vrInput.resetSensor !== undefined ) {
  40. vrInput.resetSensor();
  41. } else if ( vrInput.zeroSensor !== undefined ) {
  42. vrInput.zeroSensor();
  43. }
  44. }
  45. };
  46. this.zeroSensor = function () {
  47. THREE.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
  48. this.resetSensor();
  49. };
  50. };