VRControls.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. var vrInput;
  8. var onVRDevices = function ( devices ) {
  9. for ( var i = 0; i < devices.length; i ++ ) {
  10. var device = devices[ i ];
  11. if ( device instanceof PositionSensorVRDevice ) {
  12. vrInput = devices[ i ];
  13. return; // We keep the first we encounter
  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. if ( vrInput === undefined ) return;
  31. var state = vrInput.getState();
  32. if ( state.orientation !== null ) {
  33. object.quaternion.copy( state.orientation );
  34. }
  35. if ( state.position !== null ) {
  36. object.position.copy( state.position ).multiplyScalar( scope.scale );
  37. }
  38. };
  39. this.zeroSensor = function () {
  40. if ( vrInput === undefined ) return;
  41. vrInput.zeroSensor();
  42. };
  43. };