ViveController.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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' || gamepad.id === 'Oculus Touch (Left)' || gamepad.id === 'Oculus Touch (Right)' ) ) {
  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 !== undefined ) {
  40. if ( gamepad.pose === null ) return; // No user action yet
  41. // Position and orientation.
  42. var pose = gamepad.pose;
  43. if ( pose.position !== null ) scope.position.fromArray( pose.position );
  44. if ( pose.orientation !== null ) scope.quaternion.fromArray( pose.orientation );
  45. scope.matrix.compose( scope.position, scope.quaternion, scope.scale );
  46. scope.matrix.multiplyMatrices( scope.standingMatrix, scope.matrix );
  47. scope.matrixWorldNeedsUpdate = true;
  48. scope.visible = true;
  49. // Thumbpad and Buttons.
  50. if ( axes[ 0 ] !== gamepad.axes[ 0 ] || axes[ 1 ] !== gamepad.axes[ 1 ] ) {
  51. axes[ 0 ] = gamepad.axes[ 0 ]; // X axis: -1 = Left, +1 = Right.
  52. axes[ 1 ] = gamepad.axes[ 1 ]; // Y axis: -1 = Bottom, +1 = Top.
  53. scope.dispatchEvent( { type: 'axischanged', axes: axes } );
  54. }
  55. if ( thumbpadIsPressed !== gamepad.buttons[ 0 ].pressed ) {
  56. thumbpadIsPressed = gamepad.buttons[ 0 ].pressed;
  57. scope.dispatchEvent( { type: thumbpadIsPressed ? 'thumbpaddown' : 'thumbpadup' } );
  58. }
  59. if ( triggerIsPressed !== gamepad.buttons[ 1 ].pressed ) {
  60. triggerIsPressed = gamepad.buttons[ 1 ].pressed;
  61. scope.dispatchEvent( { type: triggerIsPressed ? 'triggerdown' : 'triggerup' } );
  62. }
  63. if ( gripsArePressed !== gamepad.buttons[ 2 ].pressed ) {
  64. gripsArePressed = gamepad.buttons[ 2 ].pressed;
  65. scope.dispatchEvent( { type: gripsArePressed ? 'gripsdown' : 'gripsup' } );
  66. }
  67. if ( menuIsPressed !== gamepad.buttons[ 3 ].pressed ) {
  68. menuIsPressed = gamepad.buttons[ 3 ].pressed;
  69. scope.dispatchEvent( { type: menuIsPressed ? 'menudown' : 'menuup' } );
  70. }
  71. } else {
  72. scope.visible = false;
  73. }
  74. };
  75. };
  76. THREE.ViveController.prototype = Object.create( THREE.Object3D.prototype );
  77. THREE.ViveController.prototype.constructor = THREE.ViveController;