VRControls.js 1.5 KB

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