VRControls.js 2.0 KB

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