GearVRController.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @author servinlp
  3. */
  4. THREE.GearVRController = function () {
  5. THREE.Object3D.call( this );
  6. var scope = this;
  7. var gamepad;
  8. var axes = [ 0, 0 ];
  9. var touchpadIsPressed = false;
  10. var triggerIsPressed = false;
  11. var angularVelocity = new THREE.Vector3();
  12. this.matrixAutoUpdate = true;
  13. function findGamepad() {
  14. var gamepads = navigator.getGamepads && navigator.getGamepads();
  15. for ( var i = 0; i < 4; i ++ ) {
  16. var gamepad = gamepads[ i ];
  17. if ( gamepad && ( gamepad.id === 'Gear VR Controller' || gamepad.id === 'Oculus Go Controller' ) ) {
  18. return gamepad;
  19. }
  20. }
  21. }
  22. this.getGamepad = function () {
  23. return gamepad;
  24. };
  25. this.getTouchpadState = function () {
  26. return touchpadIsPressed;
  27. };
  28. this.update = function () {
  29. gamepad = findGamepad();
  30. if ( gamepad !== undefined && gamepad.pose !== undefined ) {
  31. var pose = gamepad.pose;
  32. if ( pose === null ) return; // no user action yet
  33. // orientation
  34. if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
  35. scope.updateMatrix();
  36. scope.visible = true;
  37. // angular velocity
  38. if ( pose.angularVelocity !== null && ! angularVelocity.equals( pose.angularVelocity ) ) {
  39. angularVelocity.fromArray( pose.angularVelocity );
  40. scope.dispatchEvent( { type: 'angularvelocitychanged', angularVelocity: angularVelocity } );
  41. }
  42. // axes (touchpad)
  43. if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
  44. axes[ 0 ] = gamepad.axes[ 0 ];
  45. axes[ 1 ] = gamepad.axes[ 1 ];
  46. scope.dispatchEvent( { type: 'axischanged', axes: axes } );
  47. }
  48. // button (touchpad)
  49. if ( touchpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
  50. touchpadIsPressed = gamepad.buttons[ 0 ].pressed;
  51. scope.dispatchEvent( { type: touchpadIsPressed ? 'touchpaddown' : 'touchpadup', axes: axes } );
  52. }
  53. // trigger
  54. if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
  55. triggerIsPressed = gamepad.buttons[ 1 ].pressed;
  56. scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
  57. }
  58. // app button not available, reserved for use by the browser
  59. } else {
  60. scope.visible = false;
  61. }
  62. };
  63. // DEPRECATED
  64. this.getTouchPadState = function () {
  65. console.warn( 'THREE.GearVRController: getTouchPadState() is now getTouchpadState()' );
  66. return touchpadIsPressed;
  67. };
  68. this.setHand = function () {
  69. console.warn( 'THREE.GearVRController: setHand() has been removed.' );
  70. };
  71. };
  72. THREE.GearVRController.prototype = Object.create( THREE.Object3D.prototype );
  73. THREE.GearVRController.prototype.constructor = THREE.GearVRController;