ViveController.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * @author mrdoob / http://mrdoob.com
  3. * @author stewdio / http://stewd.io
  4. */
  5. THREE.ViveController = function ( id ) {
  6. THREE.Object3D.call( this );
  7. var scope = this;
  8. var gamepad;
  9. var axes = [ 0, 0 ];
  10. var thumbpadIsPressed = false;
  11. var triggerIsPressed = false;
  12. var gripsArePressed = false;
  13. var menuIsPressed = false;
  14. function findGamepad( id ) {
  15. // Iterate across gamepads as Vive Controllers may not be
  16. // in position 0 and 1.
  17. var gamepads = navigator.getGamepads();
  18. for ( var i = 0, j = 0; i < 4; i ++ ) {
  19. var gamepad = gamepads[ i ];
  20. if ( gamepad && gamepad.id === 'OpenVR Gamepad' ) {
  21. if ( j === id ) return gamepad;
  22. j ++;
  23. }
  24. }
  25. }
  26. this.matrixAutoUpdate = false;
  27. this.standingMatrix = new THREE.Matrix4();
  28. this.getGamepad = function () {
  29. return gamepad;
  30. };
  31. this.getButtonState = function ( button ) {
  32. if ( button === 'thumbpad' ) return thumbpadIsPressed;
  33. if ( button === 'trigger' ) return triggerIsPressed;
  34. if ( button === 'grips' ) return gripsArePressed;
  35. if ( button === 'menu' ) return menuIsPressed;
  36. };
  37. this.update = function () {
  38. gamepad = findGamepad( id );
  39. if ( gamepad !== undefined && gamepad.pose !== null ) {
  40. // Position and orientation.
  41. var pose = gamepad.pose;
  42. if ( pose.position !== null ) scope.position.fromArray( pose.position );
  43. if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
  44. scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
  45. scope.matrix.multiplyMatrices( scope.standingMatrix, scope.matrix );
  46. scope.matrixWorldNeedsUpdate = true;
  47. scope.visible = true;
  48. // Thumbpad and Buttons.
  49. if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
  50. axes[ 0 ] = gamepad.axes[ 0 ]; // X axis: -1 = Left, +1 = Right.
  51. axes[ 1 ] = gamepad.axes[ 1 ]; // Y axis: -1 = Bottom, +1 = Top.
  52. scope.dispatchEvent( { type: 'axischanged', axes: axes } );
  53. }
  54. if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
  55. thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
  56. scope.dispatchEvent( { type: thumbpadIsPressed ? 'thumbpaddown' : 'thumbpadup' } );
  57. }
  58. if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
  59. triggerIsPressed = gamepad.buttons[ 1 ].pressed;
  60. scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
  61. }
  62. if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
  63. gripsArePressed = gamepad.buttons[ 2 ].pressed;
  64. scope.dispatchEvent( { type: gripsArePressed ? 'gripsdown' : 'gripsup' } );
  65. }
  66. if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {
  67. menuIsPressed = gamepad.buttons[ 3 ].pressed;
  68. scope.dispatchEvent( { type: menuIsPressed ? 'menudown' : 'menuup' } );
  69. }
  70. } else {
  71. scope.visible = false;
  72. }
  73. };
  74. };
  75. THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype );
  76. THREE.ViveController.prototype.constructor = THREE.ViveController;