FlyControls.js 7.8 KB

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