GearVRController.js 2.6 KB

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