VRControls.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @author dmarcos / https://github.com/dmarcos
  3. * @author mrdoob / http://mrdoob.com
  4. */
  5. THREE.VRControls = function ( object, callback ) {
  6. var scope = this;
  7. // Allow for multiple VR input devices.
  8. var vrInputs = [];
  9. var onVRDevices = function ( devices ) {
  10. for ( var i = 0; i < devices.length; i ++ ) {
  11. var device = devices[ i ];
  12. if ( device instanceof PositionSensorVRDevice ) {
  13. vrInputs.push( devices[ i ] );
  14. }
  15. }
  16. if ( callback !== undefined ) {
  17. callback( 'HMD not available' );
  18. }
  19. };
  20. if ( navigator.getVRDevices !== undefined ) {
  21. navigator.getVRDevices().then( onVRDevices );
  22. } else if ( callback !== undefined ) {
  23. callback( 'Your browser is not VR Ready' );
  24. }
  25. // the Rift SDK returns the position in meters
  26. // this scale factor allows the user to define how meters
  27. // are converted to scene units.
  28. this.scale = 1;
  29. this.update = function () {
  30. for ( var i = 0; i < vrInputs.length; i++ ) {
  31. var vrInput = vrInputs[ i ];
  32. var state = vrInput.getState();
  33. if ( state.orientation !== null ) {
  34. object.quaternion.copy( state.orientation );
  35. }
  36. if ( state.position !== null ) {
  37. object.position.copy( state.position ).multiplyScalar( scope.scale );
  38. }
  39. }
  40. };
  41. this.resetSensor = function () {
  42. for ( var i = 0; i < vrInputs.length; i++ ) {
  43. var vrInput = vrInputs[ i ];
  44. if ( vrInput.resetSensor !== undefined ) {
  45. vrInput.resetSensor();
  46. } else if ( vrInput.zeroSensor !== undefined ) {
  47. vrInput.zeroSensor();
  48. }
  49. };
  50. this.zeroSensor = function () {
  51. THREE.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
  52. this.resetSensor();
  53. };
  54. };