GearVRController.js 2.7 KB

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