GearVRController.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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' ) ) {
  18. return gamepad;
  19. }
  20. }
  21. }
  22. this.setHand = function ( hand = 'right' ) {
  23. var handPos;
  24. if ( hand === 'right' ) {
  25. handPos = 0.3;
  26. } else {
  27. handPos = - 0.3;
  28. }
  29. this.translateX( handPos );
  30. this.translateY( - 0.35 );
  31. this.translateZ( - 0.4 );
  32. };
  33. this.getGamepad = function () {
  34. return gamepad;
  35. };
  36. this.getTouchpadState = function () {
  37. return touchpadIsPressed;
  38. };
  39. this.update = function () {
  40. gamepad = findGamepad();
  41. if ( gamepad !== undefined && gamepad.pose !== undefined ) {
  42. var pose = gamepad.pose;
  43. if ( pose === null ) return; // no user action yet
  44. // orientation
  45. if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
  46. scope.updateMatrix();
  47. scope.visible = true;
  48. // angular velocity
  49. if ( pose.angularVelocity !== null && ! angularVelocity.equals( pose.angularVelocity ) ) {
  50. angularVelocity.fromArray( pose.angularVelocity );
  51. scope.dispatchEvent( { type: 'angularvelocitychanged', angularVelocity: angularVelocity } );
  52. }
  53. // axes (touchpad)
  54. if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
  55. axes[ 0 ] = gamepad.axes[ 0 ];
  56. axes[ 1 ] = gamepad.axes[ 1 ];
  57. scope.dispatchEvent( { type: 'axischanged', axes: axes } );
  58. }
  59. // button (touchpad)
  60. if ( touchpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
  61. touchpadIsPressed = gamepad.buttons[ 0 ].pressed;
  62. scope.dispatchEvent( { type: touchpadIsPressed ? 'touchpaddown' : 'touchpadup', axes: axes } );
  63. }
  64. // trigger
  65. if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
  66. triggerIsPressed = gamepad.buttons[ 1 ].pressed;
  67. scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
  68. }
  69. // app button not available, reserved for use by the browser
  70. } else {
  71. scope.visible = false;
  72. }
  73. };
  74. // DEPRECATED
  75. this.getTouchPadState = function () {
  76. console.warn( 'THREE.GearVRController: getTouchPadState() is now getTouchpadState()' );
  77. return touchpadIsPressed;
  78. };
  79. };
  80. THREE.GearVRController.prototype = Object.create( THREE.Object3D.prototype );
  81. THREE.GearVRController.prototype.constructor = THREE.GearVRController;