ViveController.js 3.2 KB

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