FlyControls.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. import {
  2. Controls,
  3. Quaternion,
  4. Vector3
  5. } from 'three';
  6. /**
  7. * Fires when the camera has been transformed by the controls.
  8. *
  9. * @event FlyControls#change
  10. * @type {Object}
  11. */
  12. const _changeEvent = { type: 'change' };
  13. const _EPS = 0.000001;
  14. const _tmpQuaternion = new Quaternion();
  15. /**
  16. * This class enables a navigation similar to fly modes in DCC tools like Blender.
  17. * You can arbitrarily transform the camera in 3D space without any limitations
  18. * (e.g. focus on a specific target).
  19. *
  20. * @augments Controls
  21. * @three_import import { FlyControls } from 'three/addons/controls/FlyControls.js';
  22. */
  23. class FlyControls extends Controls {
  24. /**
  25. * Constructs a new controls instance.
  26. *
  27. * @param {Object3D} object - The object that is managed by the controls.
  28. * @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
  29. */
  30. constructor( object, domElement = null ) {
  31. super( object, domElement );
  32. /**
  33. * The movement speed.
  34. *
  35. * @type {number}
  36. * @default 1
  37. */
  38. this.movementSpeed = 1.0;
  39. /**
  40. * The rotation speed.
  41. *
  42. * @type {number}
  43. * @default 0.005
  44. */
  45. this.rollSpeed = 0.005;
  46. /**
  47. * If set to `true`, you can only look around by performing a drag interaction.
  48. *
  49. * @type {boolean}
  50. * @default false
  51. */
  52. this.dragToLook = false;
  53. /**
  54. * If set to `true`, the camera automatically moves forward (and does not stop) when initially translated.
  55. *
  56. * @type {boolean}
  57. * @default false
  58. */
  59. this.autoForward = false;
  60. // internals
  61. this._moveState = { up: 0, down: 0, left: 0, right: 0, forward: 0, back: 0, pitchUp: 0, pitchDown: 0, yawLeft: 0, yawRight: 0, rollLeft: 0, rollRight: 0 };
  62. this._moveVector = new Vector3( 0, 0, 0 );
  63. this._rotationVector = new Vector3( 0, 0, 0 );
  64. this._lastQuaternion = new Quaternion();
  65. this._lastPosition = new Vector3();
  66. this._status = 0;
  67. // event listeners
  68. this._onKeyDown = onKeyDown.bind( this );
  69. this._onKeyUp = onKeyUp.bind( this );
  70. this._onPointerMove = onPointerMove.bind( this );
  71. this._onPointerDown = onPointerDown.bind( this );
  72. this._onPointerUp = onPointerUp.bind( this );
  73. this._onPointerCancel = onPointerCancel.bind( this );
  74. this._onContextMenu = onContextMenu.bind( this );
  75. //
  76. if ( domElement !== null ) {
  77. this.connect( domElement );
  78. }
  79. }
  80. connect( element ) {
  81. super.connect( element );
  82. window.addEventListener( 'keydown', this._onKeyDown );
  83. window.addEventListener( 'keyup', this._onKeyUp );
  84. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  85. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  86. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  87. this.domElement.addEventListener( 'pointercancel', this._onPointerCancel );
  88. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  89. }
  90. disconnect() {
  91. window.removeEventListener( 'keydown', this._onKeyDown );
  92. window.removeEventListener( 'keyup', this._onKeyUp );
  93. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  94. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  95. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  96. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  97. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  98. }
  99. dispose() {
  100. this.disconnect();
  101. }
  102. update( delta ) {
  103. if ( this.enabled === false ) return;
  104. const object = this.object;
  105. const moveMult = delta * this.movementSpeed;
  106. const rotMult = delta * this.rollSpeed;
  107. object.translateX( this._moveVector.x * moveMult );
  108. object.translateY( this._moveVector.y * moveMult );
  109. object.translateZ( this._moveVector.z * moveMult );
  110. _tmpQuaternion.set( this._rotationVector.x * rotMult, this._rotationVector.y * rotMult, this._rotationVector.z * rotMult, 1 ).normalize();
  111. object.quaternion.multiply( _tmpQuaternion );
  112. if (
  113. this._lastPosition.distanceToSquared( object.position ) > _EPS ||
  114. 8 * ( 1 - this._lastQuaternion.dot( object.quaternion ) ) > _EPS
  115. ) {
  116. this.dispatchEvent( _changeEvent );
  117. this._lastQuaternion.copy( object.quaternion );
  118. this._lastPosition.copy( object.position );
  119. }
  120. }
  121. // private
  122. _updateMovementVector() {
  123. const forward = ( this._moveState.forward || ( this.autoForward && ! this._moveState.back ) ) ? 1 : 0;
  124. this._moveVector.x = ( - this._moveState.left + this._moveState.right );
  125. this._moveVector.y = ( - this._moveState.down + this._moveState.up );
  126. this._moveVector.z = ( - forward + this._moveState.back );
  127. //console.log( 'move:', [ this._moveVector.x, this._moveVector.y, this._moveVector.z ] );
  128. }
  129. _updateRotationVector() {
  130. this._rotationVector.x = ( - this._moveState.pitchDown + this._moveState.pitchUp );
  131. this._rotationVector.y = ( - this._moveState.yawRight + this._moveState.yawLeft );
  132. this._rotationVector.z = ( - this._moveState.rollRight + this._moveState.rollLeft );
  133. //console.log( 'rotate:', [ this._rotationVector.x, this._rotationVector.y, this._rotationVector.z ] );
  134. }
  135. _getContainerDimensions() {
  136. if ( this.domElement != document ) {
  137. return {
  138. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  139. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  140. };
  141. } else {
  142. return {
  143. size: [ window.innerWidth, window.innerHeight ],
  144. offset: [ 0, 0 ]
  145. };
  146. }
  147. }
  148. }
  149. function onKeyDown( event ) {
  150. if ( event.altKey || this.enabled === false ) {
  151. return;
  152. }
  153. switch ( event.code ) {
  154. case 'ShiftLeft':
  155. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  156. case 'KeyW': this._moveState.forward = 1; break;
  157. case 'KeyS': this._moveState.back = 1; break;
  158. case 'KeyA': this._moveState.left = 1; break;
  159. case 'KeyD': this._moveState.right = 1; break;
  160. case 'KeyR': this._moveState.up = 1; break;
  161. case 'KeyF': this._moveState.down = 1; break;
  162. case 'ArrowUp': this._moveState.pitchUp = 1; break;
  163. case 'ArrowDown': this._moveState.pitchDown = 1; break;
  164. case 'ArrowLeft': this._moveState.yawLeft = 1; break;
  165. case 'ArrowRight': this._moveState.yawRight = 1; break;
  166. case 'KeyQ': this._moveState.rollLeft = 1; break;
  167. case 'KeyE': this._moveState.rollRight = 1; break;
  168. }
  169. this._updateMovementVector();
  170. this._updateRotationVector();
  171. }
  172. function onKeyUp( event ) {
  173. if ( this.enabled === false ) return;
  174. switch ( event.code ) {
  175. case 'ShiftLeft':
  176. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  177. case 'KeyW': this._moveState.forward = 0; break;
  178. case 'KeyS': this._moveState.back = 0; break;
  179. case 'KeyA': this._moveState.left = 0; break;
  180. case 'KeyD': this._moveState.right = 0; break;
  181. case 'KeyR': this._moveState.up = 0; break;
  182. case 'KeyF': this._moveState.down = 0; break;
  183. case 'ArrowUp': this._moveState.pitchUp = 0; break;
  184. case 'ArrowDown': this._moveState.pitchDown = 0; break;
  185. case 'ArrowLeft': this._moveState.yawLeft = 0; break;
  186. case 'ArrowRight': this._moveState.yawRight = 0; break;
  187. case 'KeyQ': this._moveState.rollLeft = 0; break;
  188. case 'KeyE': this._moveState.rollRight = 0; break;
  189. }
  190. this._updateMovementVector();
  191. this._updateRotationVector();
  192. }
  193. function onPointerDown( event ) {
  194. if ( this.enabled === false ) return;
  195. if ( this.dragToLook ) {
  196. this._status ++;
  197. } else {
  198. switch ( event.button ) {
  199. case 0: this._moveState.forward = 1; break;
  200. case 2: this._moveState.back = 1; break;
  201. }
  202. this._updateMovementVector();
  203. }
  204. }
  205. function onPointerMove( event ) {
  206. if ( this.enabled === false ) return;
  207. if ( ! this.dragToLook || this._status > 0 ) {
  208. const container = this._getContainerDimensions();
  209. const halfWidth = container.size[ 0 ] / 2;
  210. const halfHeight = container.size[ 1 ] / 2;
  211. this._moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  212. this._moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  213. this._updateRotationVector();
  214. }
  215. }
  216. function onPointerUp( event ) {
  217. if ( this.enabled === false ) return;
  218. if ( this.dragToLook ) {
  219. this._status --;
  220. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  221. } else {
  222. switch ( event.button ) {
  223. case 0: this._moveState.forward = 0; break;
  224. case 2: this._moveState.back = 0; break;
  225. }
  226. this._updateMovementVector();
  227. }
  228. this._updateRotationVector();
  229. }
  230. function onPointerCancel() {
  231. if ( this.enabled === false ) return;
  232. if ( this.dragToLook ) {
  233. this._status = 0;
  234. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  235. } else {
  236. this._moveState.forward = 0;
  237. this._moveState.back = 0;
  238. this._updateMovementVector();
  239. }
  240. this._updateRotationVector();
  241. }
  242. function onContextMenu( event ) {
  243. if ( this.enabled === false ) return;
  244. event.preventDefault();
  245. }
  246. export { FlyControls };