GearVRController.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. window.SamsungChangeSky( skyBox );
  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. };
  75. THREE.GearVRController.prototype = Object.create( THREE.Object3D.prototype );
  76. THREE.GearVRController.prototype.constructor = THREE.GearVRController;