VRControls.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. break; // We keep the first we encounter
  14. }
  15. }
  16. if ( onError ) onError( 'HMD not available' );
  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. THREE.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
  49. this.resetSensor();
  50. };
  51. };