FlyControls.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 {?HTMLElement} 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. this.domElement.style.touchAction = 'none'; // Disable touch scroll
  90. }
  91. disconnect() {
  92. window.removeEventListener( 'keydown', this._onKeyDown );
  93. window.removeEventListener( 'keyup', this._onKeyUp );
  94. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  95. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  96. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  97. this.domElement.removeEventListener( 'pointercancel', this._onPointerCancel );
  98. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  99. this.domElement.style.touchAction = ''; // Restore touch scroll
  100. }
  101. dispose() {
  102. this.disconnect();
  103. }
  104. update( delta ) {
  105. if ( this.enabled === false ) return;
  106. const object = this.object;
  107. const moveMult = delta * this.movementSpeed;
  108. const rotMult = delta * this.rollSpeed;
  109. object.translateX( this._moveVector.x * moveMult );
  110. object.translateY( this._moveVector.y * moveMult );
  111. object.translateZ( this._moveVector.z * moveMult );
  112. _tmpQuaternion.set( this._rotationVector.x * rotMult, this._rotationVector.y * rotMult, this._rotationVector.z * rotMult, 1 ).normalize();
  113. object.quaternion.multiply( _tmpQuaternion );
  114. if (
  115. this._lastPosition.distanceToSquared( object.position ) > _EPS ||
  116. 8 * ( 1 - this._lastQuaternion.dot( object.quaternion ) ) > _EPS
  117. ) {
  118. this.dispatchEvent( _changeEvent );
  119. this._lastQuaternion.copy( object.quaternion );
  120. this._lastPosition.copy( object.position );
  121. }
  122. }
  123. // private
  124. _updateMovementVector() {
  125. const forward = ( this._moveState.forward || ( this.autoForward && ! this._moveState.back ) ) ? 1 : 0;
  126. this._moveVector.x = ( - this._moveState.left + this._moveState.right );
  127. this._moveVector.y = ( - this._moveState.down + this._moveState.up );
  128. this._moveVector.z = ( - forward + this._moveState.back );
  129. //console.log( 'move:', [ this._moveVector.x, this._moveVector.y, this._moveVector.z ] );
  130. }
  131. _updateRotationVector() {
  132. this._rotationVector.x = ( - this._moveState.pitchDown + this._moveState.pitchUp );
  133. this._rotationVector.y = ( - this._moveState.yawRight + this._moveState.yawLeft );
  134. this._rotationVector.z = ( - this._moveState.rollRight + this._moveState.rollLeft );
  135. //console.log( 'rotate:', [ this._rotationVector.x, this._rotationVector.y, this._rotationVector.z ] );
  136. }
  137. _getContainerDimensions() {
  138. if ( this.domElement != document ) {
  139. return {
  140. size: [ this.domElement.offsetWidth, this.domElement.offsetHeight ],
  141. offset: [ this.domElement.offsetLeft, this.domElement.offsetTop ]
  142. };
  143. } else {
  144. return {
  145. size: [ window.innerWidth, window.innerHeight ],
  146. offset: [ 0, 0 ]
  147. };
  148. }
  149. }
  150. }
  151. function onKeyDown( event ) {
  152. if ( event.altKey || this.enabled === false ) {
  153. return;
  154. }
  155. switch ( event.code ) {
  156. case 'ShiftLeft':
  157. case 'ShiftRight': this.movementSpeedMultiplier = .1; break;
  158. case 'KeyW': this._moveState.forward = 1; break;
  159. case 'KeyS': this._moveState.back = 1; break;
  160. case 'KeyA': this._moveState.left = 1; break;
  161. case 'KeyD': this._moveState.right = 1; break;
  162. case 'KeyR': this._moveState.up = 1; break;
  163. case 'KeyF': this._moveState.down = 1; break;
  164. case 'ArrowUp': this._moveState.pitchUp = 1; break;
  165. case 'ArrowDown': this._moveState.pitchDown = 1; break;
  166. case 'ArrowLeft': this._moveState.yawLeft = 1; break;
  167. case 'ArrowRight': this._moveState.yawRight = 1; break;
  168. case 'KeyQ': this._moveState.rollLeft = 1; break;
  169. case 'KeyE': this._moveState.rollRight = 1; break;
  170. }
  171. this._updateMovementVector();
  172. this._updateRotationVector();
  173. }
  174. function onKeyUp( event ) {
  175. if ( this.enabled === false ) return;
  176. switch ( event.code ) {
  177. case 'ShiftLeft':
  178. case 'ShiftRight': this.movementSpeedMultiplier = 1; break;
  179. case 'KeyW': this._moveState.forward = 0; break;
  180. case 'KeyS': this._moveState.back = 0; break;
  181. case 'KeyA': this._moveState.left = 0; break;
  182. case 'KeyD': this._moveState.right = 0; break;
  183. case 'KeyR': this._moveState.up = 0; break;
  184. case 'KeyF': this._moveState.down = 0; break;
  185. case 'ArrowUp': this._moveState.pitchUp = 0; break;
  186. case 'ArrowDown': this._moveState.pitchDown = 0; break;
  187. case 'ArrowLeft': this._moveState.yawLeft = 0; break;
  188. case 'ArrowRight': this._moveState.yawRight = 0; break;
  189. case 'KeyQ': this._moveState.rollLeft = 0; break;
  190. case 'KeyE': this._moveState.rollRight = 0; break;
  191. }
  192. this._updateMovementVector();
  193. this._updateRotationVector();
  194. }
  195. function onPointerDown( event ) {
  196. if ( this.enabled === false ) return;
  197. if ( this.dragToLook ) {
  198. this._status ++;
  199. } else {
  200. switch ( event.button ) {
  201. case 0: this._moveState.forward = 1; break;
  202. case 2: this._moveState.back = 1; break;
  203. }
  204. this._updateMovementVector();
  205. }
  206. }
  207. function onPointerMove( event ) {
  208. if ( this.enabled === false ) return;
  209. if ( ! this.dragToLook || this._status > 0 ) {
  210. const container = this._getContainerDimensions();
  211. const halfWidth = container.size[ 0 ] / 2;
  212. const halfHeight = container.size[ 1 ] / 2;
  213. this._moveState.yawLeft = - ( ( event.pageX - container.offset[ 0 ] ) - halfWidth ) / halfWidth;
  214. this._moveState.pitchDown = ( ( event.pageY - container.offset[ 1 ] ) - halfHeight ) / halfHeight;
  215. this._updateRotationVector();
  216. }
  217. }
  218. function onPointerUp( event ) {
  219. if ( this.enabled === false ) return;
  220. if ( this.dragToLook ) {
  221. this._status --;
  222. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  223. } else {
  224. switch ( event.button ) {
  225. case 0: this._moveState.forward = 0; break;
  226. case 2: this._moveState.back = 0; break;
  227. }
  228. this._updateMovementVector();
  229. }
  230. this._updateRotationVector();
  231. }
  232. function onPointerCancel() {
  233. if ( this.enabled === false ) return;
  234. if ( this.dragToLook ) {
  235. this._status = 0;
  236. this._moveState.yawLeft = this._moveState.pitchDown = 0;
  237. } else {
  238. this._moveState.forward = 0;
  239. this._moveState.back = 0;
  240. this._updateMovementVector();
  241. }
  242. this._updateRotationVector();
  243. }
  244. function onContextMenu( event ) {
  245. if ( this.enabled === false ) return;
  246. event.preventDefault();
  247. }
  248. export { FlyControls };
粤ICP备19079148号