VRControls.js 1.6 KB

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