2
0

FlyControls.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ( function () {
  2. const _changeEvent = {
  3. type: 'change'
  4. };
  5. class FlyControls extends THREE.EventDispatcher {
  6. constructor( object, domElement ) {
  7. super();
  8. this.object = object;
  9. this.domElement = domElement; // API
  10. this.movementSpeed = 1.0;
  11. this.rollSpeed = 0.005;
  12. this.dragToLook = false;
  13. this.autoForward = false; // disable default target object behavior
  14. // internals
  15. const scope = this;
  16. const EPS = 0.000001;
  17. const lastQuaternion = new THREE.Quaternion();
  18. const lastPosition = new THREE.Vector3();
  19. this.tmpQuaternion = new THREE.Quaternion();
  20. this.status = 0;
  21. this.moveState = {
  22. up: 0,
  23. down: 0,
  24. left: 0,
  25. right: 0,
  26. forward: 0,
  27. back: 0,
  28. pitchUp: 0,
  29. pitchDown: 0,
  30. yawLeft: 0,
  31. yawRight: 0,
  32. rollLeft: 0,
  33. rollRight: 0
  34. };
  35. this.moveVector = new THREE.Vector3( 0, 0, 0 );
  36. this.rotationVector = new THREE.Vector3( 0, 0, 0 );
  37. this.keydown = function ( event ) {
  38. if ( event.altKey ) {
  39. return;
  40. }
  41. switch ( event.code ) {
  42. case 'ShiftLeft':
  43. case 'ShiftRight':
  44. this.movementSpeedMultiplier = .1;
  45. break;
  46. case 'KeyW':
  47. this.moveState.forward = 1;
  48. break;
  49. case 'KeyS':
  50. this.moveState.back = 1;
  51. break;
  52. case 'KeyA':
  53. this.moveState.left = 1;
  54. break;
  55. case 'KeyD':
  56. this.moveState.right = 1;
  57. break;
  58. case 'KeyR':
  59. this.moveState.up = 1;
  60. break;
  61. case 'KeyF':
  62. this.moveState.down = 1;
  63. break;
  64. case 'ArrowUp':
  65. this.moveState.pitchUp = 1;
  66. break;
  67. case 'ArrowDown':
  68. this.moveState.pitchDown = 1;
  69. break;
  70. case 'ArrowLeft':
  71. this.moveState.yawLeft = 1;
  72. break;
  73. case 'ArrowRight':
  74. this.moveState.yawRight = 1;
  75. break;
  76. case 'KeyQ':
  77. this.moveState.rollLeft = 1;
  78. break;
  79. case 'KeyE':
  80. this.moveState.rollRight = 1;
  81. break;
  82. }
  83. this.updateMovementVector();
  84. this.updateRotationVector();
  85. };
  86. this.keyup = function ( event ) {
  87. switch ( event.code ) {
  88. case 'ShiftLeft':
  89. case 'ShiftRight':
  90. this.movementSpeedMultiplier = 1;
  91. break;
  92. case 'KeyW':
  93. this.moveState.forward = 0;
  94. break;
  95. case 'KeyS':
  96. this.moveState.back = 0;
  97. break;
  98. case 'KeyA':
  99. this.moveState.left = 0;
  100. break;
  101. case 'KeyD':
  102. this.moveState.right = 0;
  103. break;
  104. case 'KeyR':
  105. this.moveState.up = 0;
  106. break;
  107. case 'KeyF':
  108. this.moveState.down = 0;
  109. break;
  110. case 'ArrowUp':
  111. this.moveState.pitchUp = 0;
  112. break;
  113. case 'ArrowDown':
  114. this.moveState.pitchDown = 0;
  115. break;
  116. case 'ArrowLeft':
  117. this.moveState.yawLeft = 0;
  118. break;
  119. case 'ArrowRight':
  120. this.moveState.yawRight = 0;
  121. break;
  122. case 'KeyQ':
  123. this.moveState.rollLeft = 0;
  124. break;
  125. case 'KeyE':
  126. this.moveState.rollRight = 0;
  127. break;
  128. }
  129. this.updateMovementVector();
  130. this.updateRotationVector();
  131. };
  132. this.pointerdown = function ( event ) {
  133. if ( this.dragToLook ) {
  134. this.status ++;
  135. } else {
  136. switch ( event.button ) {
  137. case 0:
  138. this.moveState.forward = 1;
  139. break;
  140. case 2:
  141. this.moveState.back = 1;
  142. break;
  143. }
  144. this.updateMovementVector();
  145. }
  146. };
  147. this.pointermove = function ( event ) {
  148. if ( ! this.dragToLook || this.status > 0 ) {
  149. const container = this.getContainerDimensions();
  150. const halfWidth = container.size[ 0 ] / 2;
  151. const halfHeight = container.size[ 1 ] / 2;
  152. this.moveState.yawLeft = - ( event.pageX - container.offset[ 0 ] - halfWidth ) / halfWidth;
  153. this.moveState.pitchDown = ( event.pageY - container.offset[ 1 ] - halfHeight ) / halfHeight;
  154. this.updateRotationVector();
  155. }
  156. };
  157. this.pointerup = function ( event ) {
  158. if ( this.dragToLook ) {
  159. this.status --;
  160. this.moveState.yawLeft = this.moveState.pitchDown = 0;
  161. } else {
  162. switch ( event.button ) {
  163. case 0:
  164. this.moveState.forward = 0;
  165. break;
  166. case 2:
  167. this.moveState.back = 0;
  168. break;
  169. }
  170. this.updateMovementVector();
  171. }
  172. this.updateRotationVector();
  173. };
  174. this.update = function ( delta ) {
  175. const moveMult = delta * scope.movementSpeed;
  176. const rotMult = delta * scope.rollSpeed;
  177. scope.object.translateX( scope.moveVector.x * moveMult );
  178. scope.object.translateY( scope.moveVector.y * moveMult );
  179. scope.object.translateZ( scope.moveVector.z * moveMult );
  180. scope.tmpQuaternion.set( scope.rotationVector.x * rotMult, scope.rotationVector.y * rotMult, scope.rotationVector.z * rotMult, 1 ).normalize();
  181. scope.object.quaternion.multiply( scope.tmpQuaternion );
  182. if ( lastPosition.distanceToSquared( scope.object.position ) > EPS || 8 * ( 1 - lastQuaternion.dot( scope.object.quaternion ) ) > EPS ) {
  183. scope.dispatchEvent( _changeEvent );
  184. lastQuaternion.copy( scope.object.quaternion );
  185. lastPosition.copy( scope.object.position );
  186. }
  187. };
  188. this.updateMovementVector = function () {
  189. const forward = this.moveState.forward || this.autoForward && ! this.moveState.back ? 1 : 0;
  190. this.moveVector.x = - this.moveState.left + this.moveState.right;
  191. this.moveVector.y = - this.moveState.down + this.moveState.up;
  192. this.moveVector.z = - forward + this.moveState.back; //console.log( 'move:', [ this.moveVector.x, this.moveVector.y, this.moveVector.z ] );
  193. };
  194. this.updateRotationVector = function () {
  195. this.rotationVector.x = - this.moveState.pitchDown + this.moveState.pitchUp;
  196. this.rotationVector.y = - this.moveState.yawRight + this.moveState.yawLeft;
  197. this.rotationVector.z = - this.moveState.rollRight + this.moveState.rollLeft; //console.log( 'rotate:', [ this.rotationVector.x, this.rotationVector.y, this.rotationVector.z ] );
  198. };
  199. this.getContainerDimensions = function () {
  200. if ( this.domElement != document ) {
  201. return {
  202. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  203. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  204. };
  205. } else {
  206. return {
  207. size: [ window.innerWidth, window.innerHeight ],
  208. offset: [ 0, 0 ]
  209. };
  210. }
  211. };
  212. this.dispose = function () {
  213. this.domElement.removeEventListener( 'contextmenu', contextmenu );
  214. this.domElement.removeEventListener( 'pointerdown', _pointerdown );
  215. this.domElement.removeEventListener( 'pointermove', _pointermove );
  216. this.domElement.removeEventListener( 'pointerup', _pointerup );
  217. window.removeEventListener( 'keydown', _keydown );
  218. window.removeEventListener( 'keyup', _keyup );
  219. };
  220. const _pointermove = this.pointermove.bind( this );
  221. const _pointerdown = this.pointerdown.bind( this );
  222. const _pointerup = this.pointerup.bind( this );
  223. const _keydown = this.keydown.bind( this );
  224. const _keyup = this.keyup.bind( this );
  225. this.domElement.addEventListener( 'contextmenu', contextmenu );
  226. this.domElement.addEventListener( 'pointerdown', _pointerdown );
  227. this.domElement.addEventListener( 'pointermove', _pointermove );
  228. this.domElement.addEventListener( 'pointerup', _pointerup );
  229. window.addEventListener( 'keydown', _keydown );
  230. window.addEventListener( 'keyup', _keyup );
  231. this.updateMovementVector();
  232. this.updateRotationVector();
  233. }
  234. }
  235. function contextmenu( event ) {
  236. event.preventDefault();
  237. }
  238. THREE.FlyControls = FlyControls;
  239. } )();