FlyControls.js 7.6 KB

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