1
0

FirstPersonControls.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import {
  2. Controls,
  3. MathUtils,
  4. Spherical,
  5. Vector3
  6. } from 'three';
  7. const _lookDirection = new Vector3();
  8. const _spherical = new Spherical();
  9. const _target = new Vector3();
  10. const _targetPosition = new Vector3();
  11. /**
  12. * This class is an alternative implementation of {@link FlyControls}.
  13. *
  14. * @augments Controls
  15. * @three_import import { FirstPersonControls } from 'three/addons/controls/FirstPersonControls.js';
  16. */
  17. class FirstPersonControls extends Controls {
  18. /**
  19. * Constructs a new controls instance.
  20. *
  21. * @param {Object3D} object - The object that is managed by the controls.
  22. * @param {?HTMLElement} domElement - The HTML element used for event listeners.
  23. */
  24. constructor( object, domElement = null ) {
  25. super( object, domElement );
  26. /**
  27. * The movement speed.
  28. *
  29. * @type {number}
  30. * @default 1
  31. */
  32. this.movementSpeed = 1.0;
  33. /**
  34. * The look around speed.
  35. *
  36. * @type {number}
  37. * @default 0.005
  38. */
  39. this.lookSpeed = 0.005;
  40. /**
  41. * Whether it's possible to vertically look around or not.
  42. *
  43. * @type {boolean}
  44. * @default true
  45. */
  46. this.lookVertical = true;
  47. /**
  48. * Whether the camera is automatically moved forward or not.
  49. *
  50. * @type {boolean}
  51. * @default false
  52. */
  53. this.autoForward = false;
  54. /**
  55. * Whether or not the camera's height influences the forward movement speed.
  56. * Use the properties `heightCoef`, `heightMin` and `heightMax` for configuration.
  57. *
  58. * @type {boolean}
  59. * @default false
  60. */
  61. this.heightSpeed = false;
  62. /**
  63. * Determines how much faster the camera moves when it's y-component is near `heightMax`.
  64. *
  65. * @type {number}
  66. * @default 1
  67. */
  68. this.heightCoef = 1.0;
  69. /**
  70. * Lower camera height limit used for movement speed adjustment.
  71. *
  72. * @type {number}
  73. * @default 0
  74. */
  75. this.heightMin = 0.0;
  76. /**
  77. * Upper camera height limit used for movement speed adjustment.
  78. *
  79. * @type {number}
  80. * @default 1
  81. */
  82. this.heightMax = 1.0;
  83. /**
  84. * Whether or not looking around is vertically constrained by `verticalMin` and `verticalMax`.
  85. *
  86. * @type {boolean}
  87. * @default false
  88. */
  89. this.constrainVertical = false;
  90. /**
  91. * How far you can vertically look around, lower limit. Range is `0` to `Math.PI` in radians.
  92. *
  93. * @type {number}
  94. * @default 0
  95. */
  96. this.verticalMin = 0;
  97. /**
  98. * How far you can vertically look around, upper limit. Range is `0` to `Math.PI` in radians.
  99. *
  100. * @type {number}
  101. * @default 0
  102. */
  103. this.verticalMax = Math.PI;
  104. /**
  105. * Whether the mouse is pressed down or not.
  106. *
  107. * @type {boolean}
  108. * @readonly
  109. * @default false
  110. */
  111. this.mouseDragOn = false;
  112. // internals
  113. this._autoSpeedFactor = 0.0;
  114. this._pointerX = 0;
  115. this._pointerY = 0;
  116. this._pointerDownX = 0;
  117. this._pointerDownY = 0;
  118. this._pointerCount = 0;
  119. this._moveForward = false;
  120. this._moveBackward = false;
  121. this._moveLeft = false;
  122. this._moveRight = false;
  123. this._lat = 0;
  124. this._lon = 0;
  125. // event listeners
  126. this._onPointerMove = onPointerMove.bind( this );
  127. this._onPointerDown = onPointerDown.bind( this );
  128. this._onPointerUp = onPointerUp.bind( this );
  129. this._onContextMenu = onContextMenu.bind( this );
  130. this._onKeyDown = onKeyDown.bind( this );
  131. this._onKeyUp = onKeyUp.bind( this );
  132. //
  133. if ( domElement !== null ) {
  134. this.connect( domElement );
  135. }
  136. this._setOrientation();
  137. }
  138. connect( element ) {
  139. super.connect( element );
  140. window.addEventListener( 'keydown', this._onKeyDown );
  141. window.addEventListener( 'keyup', this._onKeyUp );
  142. this.domElement.addEventListener( 'pointermove', this._onPointerMove );
  143. this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
  144. this.domElement.addEventListener( 'pointerup', this._onPointerUp );
  145. this.domElement.addEventListener( 'contextmenu', this._onContextMenu );
  146. this.domElement.style.touchAction = 'none'; // Disable touch scroll
  147. }
  148. disconnect() {
  149. window.removeEventListener( 'keydown', this._onKeyDown );
  150. window.removeEventListener( 'keyup', this._onKeyUp );
  151. this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
  152. this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
  153. this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
  154. this.domElement.removeEventListener( 'contextmenu', this._onContextMenu );
  155. this.domElement.style.touchAction = ''; // Restore touch scroll
  156. }
  157. dispose() {
  158. this.disconnect();
  159. }
  160. /**
  161. * Rotates the camera towards the defined target position.
  162. *
  163. * @param {number|Vector3} x - The x coordinate of the target position or alternatively a vector representing the target position.
  164. * @param {number} y - The y coordinate of the target position.
  165. * @param {number} z - The z coordinate of the target position.
  166. * @return {FirstPersonControls} A reference to this controls.
  167. */
  168. lookAt( x, y, z ) {
  169. if ( x.isVector3 ) {
  170. _target.copy( x );
  171. } else {
  172. _target.set( x, y, z );
  173. }
  174. this.object.lookAt( _target );
  175. this._setOrientation();
  176. return this;
  177. }
  178. update( delta ) {
  179. if ( this.enabled === false ) return;
  180. if ( this.heightSpeed ) {
  181. const y = MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
  182. const heightDelta = y - this.heightMin;
  183. this._autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  184. } else {
  185. this._autoSpeedFactor = 0.0;
  186. }
  187. const actualMoveSpeed = delta * this.movementSpeed;
  188. if ( this._moveForward || ( this.autoForward && ! this._moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this._autoSpeedFactor ) );
  189. if ( this._moveBackward ) this.object.translateZ( actualMoveSpeed );
  190. if ( this._moveLeft ) this.object.translateX( - actualMoveSpeed );
  191. if ( this._moveRight ) this.object.translateX( actualMoveSpeed );
  192. if ( this._moveUp ) this.object.translateY( actualMoveSpeed );
  193. if ( this._moveDown ) this.object.translateY( - actualMoveSpeed );
  194. const actualLookSpeed = delta * this.lookSpeed;
  195. let verticalLookRatio = 1;
  196. if ( this.constrainVertical ) {
  197. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  198. }
  199. if ( this.mouseDragOn ) {
  200. this._lon -= this._pointerX * actualLookSpeed;
  201. if ( this.lookVertical ) this._lat -= this._pointerY * actualLookSpeed * verticalLookRatio;
  202. }
  203. this._lat = Math.max( - 85, Math.min( 85, this._lat ) );
  204. let phi = MathUtils.degToRad( 90 - this._lat );
  205. const theta = MathUtils.degToRad( this._lon );
  206. if ( this.constrainVertical ) {
  207. phi = MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  208. }
  209. const position = this.object.position;
  210. _targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
  211. this.object.lookAt( _targetPosition );
  212. }
  213. _setOrientation() {
  214. const quaternion = this.object.quaternion;
  215. _lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  216. _spherical.setFromVector3( _lookDirection );
  217. this._lat = 90 - MathUtils.radToDeg( _spherical.phi );
  218. this._lon = MathUtils.radToDeg( _spherical.theta );
  219. }
  220. /**
  221. * @deprecated, r184. This method is no longer needed.
  222. */
  223. handleResize() {
  224. console.warn( 'THREE.FirstPersonControls: handleResize() has been removed.' );
  225. }
  226. }
  227. function onPointerDown( event ) {
  228. if ( this.domElement !== document ) {
  229. this.domElement.focus();
  230. }
  231. this.domElement.setPointerCapture( event.pointerId );
  232. this._pointerCount ++;
  233. if ( event.pointerType === 'touch' ) {
  234. this._moveForward = this._pointerCount === 1;
  235. this._moveBackward = this._pointerCount >= 2;
  236. } else {
  237. switch ( event.button ) {
  238. case 0: this._moveForward = true; break;
  239. case 2: this._moveBackward = true; break;
  240. }
  241. }
  242. this._pointerDownX = event.pageX;
  243. this._pointerDownY = event.pageY;
  244. this._pointerX = 0;
  245. this._pointerY = 0;
  246. this.mouseDragOn = true;
  247. }
  248. function onPointerUp( event ) {
  249. this.domElement.releasePointerCapture( event.pointerId );
  250. this._pointerCount --;
  251. if ( event.pointerType === 'touch' ) {
  252. this._moveForward = this._pointerCount === 1;
  253. this._moveBackward = false;
  254. } else {
  255. switch ( event.button ) {
  256. case 0: this._moveForward = false; break;
  257. case 2: this._moveBackward = false; break;
  258. }
  259. }
  260. this._pointerX = 0;
  261. this._pointerY = 0;
  262. if ( this._pointerCount === 0 ) this.mouseDragOn = false;
  263. }
  264. function onPointerMove( event ) {
  265. if ( this.mouseDragOn === false ) return;
  266. this._pointerX = event.pageX - this._pointerDownX;
  267. this._pointerY = event.pageY - this._pointerDownY;
  268. }
  269. function onKeyDown( event ) {
  270. switch ( event.code ) {
  271. case 'ArrowUp':
  272. case 'KeyW': this._moveForward = true; break;
  273. case 'ArrowLeft':
  274. case 'KeyA': this._moveLeft = true; break;
  275. case 'ArrowDown':
  276. case 'KeyS': this._moveBackward = true; break;
  277. case 'ArrowRight':
  278. case 'KeyD': this._moveRight = true; break;
  279. case 'KeyR': this._moveUp = true; break;
  280. case 'KeyF': this._moveDown = true; break;
  281. }
  282. }
  283. function onKeyUp( event ) {
  284. switch ( event.code ) {
  285. case 'ArrowUp':
  286. case 'KeyW': this._moveForward = false; break;
  287. case 'ArrowLeft':
  288. case 'KeyA': this._moveLeft = false; break;
  289. case 'ArrowDown':
  290. case 'KeyS': this._moveBackward = false; break;
  291. case 'ArrowRight':
  292. case 'KeyD': this._moveRight = false; break;
  293. case 'KeyR': this._moveUp = false; break;
  294. case 'KeyF': this._moveDown = false; break;
  295. }
  296. }
  297. function onContextMenu( event ) {
  298. if ( this.enabled === false ) return;
  299. event.preventDefault();
  300. }
  301. export { FirstPersonControls };
粤ICP备19079148号