VRControls.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 filterInvalidDevices( devices ) {
  9. var
  10. OculusDeviceName = 'VR Position Device (oculus)',
  11. CardboardDeviceName = 'VR Position Device (cardboard)';
  12. // Exclude Cardboard position sensor if Oculus exists.
  13. var oculusDevices = devices.filter( function ( device ) {
  14. return device.deviceName === OculusDeviceName;
  15. } );
  16. if ( oculusDevices.length >= 1 ) {
  17. return devices.filter( function ( device ) {
  18. return device.deviceName !== CardboardDeviceName;
  19. } );
  20. } else {
  21. return devices;
  22. }
  23. }
  24. function gotVRDevices( devices ) {
  25. devices = filterInvalidDevices( devices );
  26. for ( var i = 0; i < devices.length; i ++ ) {
  27. if ( devices[ i ] instanceof PositionSensorVRDevice ) {
  28. vrInputs.push( devices[ i ] );
  29. }
  30. }
  31. if ( onError ) onError( 'HMD not available' );
  32. }
  33. if ( navigator.getVRDevices ) {
  34. navigator.getVRDevices().then( gotVRDevices );
  35. }
  36. // the Rift SDK returns the position in meters
  37. // this scale factor allows the user to define how meters
  38. // are converted to scene units.
  39. this.scale = 1;
  40. this.update = function () {
  41. for ( var i = 0; i < vrInputs.length; i ++ ) {
  42. var vrInput = vrInputs[ i ];
  43. var state = vrInput.getState();
  44. if ( state.orientation !== null ) {
  45. object.quaternion.copy( state.orientation );
  46. }
  47. if ( state.position !== null ) {
  48. object.position.copy( state.position ).multiplyScalar( scope.scale );
  49. }
  50. }
  51. };
  52. this.resetSensor = function () {
  53. for ( var i = 0; i < vrInputs.length; i ++ ) {
  54. var vrInput = vrInputs[ i ];
  55. if ( vrInput.resetSensor !== undefined ) {
  56. vrInput.resetSensor();
  57. } else if ( vrInput.zeroSensor !== undefined ) {
  58. vrInput.zeroSensor();
  59. }
  60. }
  61. };
  62. this.zeroSensor = function () {
  63. THREE.warn( 'THREE.VRControls: .zeroSensor() is now .resetSensor().' );
  64. this.resetSensor();
  65. };
  66. };