FirstPersonControls.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. console.warn( "THREE.FirstPersonControls: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author alteredq / http://alteredqualia.com/
  5. * @author paulirish / http://paulirish.com/
  6. */
  7. THREE.FirstPersonControls = function ( object, domElement ) {
  8. if ( domElement === undefined ) {
  9. console.warn( 'THREE.FirstPersonControls: The second parameter "domElement" is now mandatory.' );
  10. domElement = document;
  11. }
  12. this.object = object;
  13. this.domElement = domElement;
  14. // API
  15. this.enabled = true;
  16. this.movementSpeed = 1.0;
  17. this.lookSpeed = 0.005;
  18. this.lookVertical = true;
  19. this.autoForward = false;
  20. this.activeLook = true;
  21. this.heightSpeed = false;
  22. this.heightCoef = 1.0;
  23. this.heightMin = 0.0;
  24. this.heightMax = 1.0;
  25. this.constrainVertical = false;
  26. this.verticalMin = 0;
  27. this.verticalMax = Math.PI;
  28. this.mouseDragOn = false;
  29. // internals
  30. this.autoSpeedFactor = 0.0;
  31. this.mouseX = 0;
  32. this.mouseY = 0;
  33. this.moveForward = false;
  34. this.moveBackward = false;
  35. this.moveLeft = false;
  36. this.moveRight = false;
  37. this.viewHalfX = 0;
  38. this.viewHalfY = 0;
  39. // private variables
  40. var lat = 0;
  41. var lon = 0;
  42. var lookDirection = new THREE.Vector3();
  43. var spherical = new THREE.Spherical();
  44. var target = new THREE.Vector3();
  45. //
  46. if ( this.domElement !== document ) {
  47. this.domElement.setAttribute( 'tabindex', - 1 );
  48. }
  49. //
  50. this.handleResize = function () {
  51. if ( this.domElement === document ) {
  52. this.viewHalfX = window.innerWidth / 2;
  53. this.viewHalfY = window.innerHeight / 2;
  54. } else {
  55. this.viewHalfX = this.domElement.offsetWidth / 2;
  56. this.viewHalfY = this.domElement.offsetHeight / 2;
  57. }
  58. };
  59. this.onMouseDown = function ( event ) {
  60. if ( this.domElement !== document ) {
  61. this.domElement.focus();
  62. }
  63. event.preventDefault();
  64. event.stopPropagation();
  65. if ( this.activeLook ) {
  66. switch ( event.button ) {
  67. case 0: this.moveForward = true; break;
  68. case 2: this.moveBackward = true; break;
  69. }
  70. }
  71. this.mouseDragOn = true;
  72. };
  73. this.onMouseUp = function ( event ) {
  74. event.preventDefault();
  75. event.stopPropagation();
  76. if ( this.activeLook ) {
  77. switch ( event.button ) {
  78. case 0: this.moveForward = false; break;
  79. case 2: this.moveBackward = false; break;
  80. }
  81. }
  82. this.mouseDragOn = false;
  83. };
  84. this.onMouseMove = function ( event ) {
  85. if ( this.domElement === document ) {
  86. this.mouseX = event.pageX - this.viewHalfX;
  87. this.mouseY = event.pageY - this.viewHalfY;
  88. } else {
  89. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  90. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  91. }
  92. };
  93. this.onKeyDown = function ( event ) {
  94. //event.preventDefault();
  95. switch ( event.keyCode ) {
  96. case 38: /*up*/
  97. case 87: /*W*/ this.moveForward = true; break;
  98. case 37: /*left*/
  99. case 65: /*A*/ this.moveLeft = true; break;
  100. case 40: /*down*/
  101. case 83: /*S*/ this.moveBackward = true; break;
  102. case 39: /*right*/
  103. case 68: /*D*/ this.moveRight = true; break;
  104. case 82: /*R*/ this.moveUp = true; break;
  105. case 70: /*F*/ this.moveDown = true; break;
  106. }
  107. };
  108. this.onKeyUp = function ( event ) {
  109. switch ( event.keyCode ) {
  110. case 38: /*up*/
  111. case 87: /*W*/ this.moveForward = false; break;
  112. case 37: /*left*/
  113. case 65: /*A*/ this.moveLeft = false; break;
  114. case 40: /*down*/
  115. case 83: /*S*/ this.moveBackward = false; break;
  116. case 39: /*right*/
  117. case 68: /*D*/ this.moveRight = false; break;
  118. case 82: /*R*/ this.moveUp = false; break;
  119. case 70: /*F*/ this.moveDown = false; break;
  120. }
  121. };
  122. this.lookAt = function ( x, y, z ) {
  123. if ( x.isVector3 ) {
  124. target.copy( x );
  125. } else {
  126. target.set( x, y, z );
  127. }
  128. this.object.lookAt( target );
  129. setOrientation( this );
  130. return this;
  131. };
  132. this.update = function () {
  133. var targetPosition = new THREE.Vector3();
  134. return function update( delta ) {
  135. if ( this.enabled === false ) return;
  136. if ( this.heightSpeed ) {
  137. var y = THREE.MathUtils.clamp( this.object.position.y, this.heightMin, this.heightMax );
  138. var heightDelta = y - this.heightMin;
  139. this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  140. } else {
  141. this.autoSpeedFactor = 0.0;
  142. }
  143. var actualMoveSpeed = delta * this.movementSpeed;
  144. if ( this.moveForward || ( this.autoForward && ! this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
  145. if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  146. if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  147. if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  148. if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  149. if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  150. var actualLookSpeed = delta * this.lookSpeed;
  151. if ( ! this.activeLook ) {
  152. actualLookSpeed = 0;
  153. }
  154. var verticalLookRatio = 1;
  155. if ( this.constrainVertical ) {
  156. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  157. }
  158. lon -= this.mouseX * actualLookSpeed;
  159. if ( this.lookVertical ) lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
  160. lat = Math.max( - 85, Math.min( 85, lat ) );
  161. var phi = THREE.MathUtils.degToRad( 90 - lat );
  162. var theta = THREE.MathUtils.degToRad( lon );
  163. if ( this.constrainVertical ) {
  164. phi = THREE.MathUtils.mapLinear( phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  165. }
  166. var position = this.object.position;
  167. targetPosition.setFromSphericalCoords( 1, phi, theta ).add( position );
  168. this.object.lookAt( targetPosition );
  169. };
  170. }();
  171. function contextmenu( event ) {
  172. event.preventDefault();
  173. }
  174. this.dispose = function () {
  175. this.domElement.removeEventListener( 'contextmenu', contextmenu, false );
  176. this.domElement.removeEventListener( 'mousedown', _onMouseDown, false );
  177. this.domElement.removeEventListener( 'mousemove', _onMouseMove, false );
  178. this.domElement.removeEventListener( 'mouseup', _onMouseUp, false );
  179. window.removeEventListener( 'keydown', _onKeyDown, false );
  180. window.removeEventListener( 'keyup', _onKeyUp, false );
  181. };
  182. var _onMouseMove = bind( this, this.onMouseMove );
  183. var _onMouseDown = bind( this, this.onMouseDown );
  184. var _onMouseUp = bind( this, this.onMouseUp );
  185. var _onKeyDown = bind( this, this.onKeyDown );
  186. var _onKeyUp = bind( this, this.onKeyUp );
  187. this.domElement.addEventListener( 'contextmenu', contextmenu, false );
  188. this.domElement.addEventListener( 'mousemove', _onMouseMove, false );
  189. this.domElement.addEventListener( 'mousedown', _onMouseDown, false );
  190. this.domElement.addEventListener( 'mouseup', _onMouseUp, false );
  191. window.addEventListener( 'keydown', _onKeyDown, false );
  192. window.addEventListener( 'keyup', _onKeyUp, false );
  193. function bind( scope, fn ) {
  194. return function () {
  195. fn.apply( scope, arguments );
  196. };
  197. }
  198. function setOrientation( controls ) {
  199. var quaternion = controls.object.quaternion;
  200. lookDirection.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  201. spherical.setFromVector3( lookDirection );
  202. lat = 90 - THREE.MathUtils.radToDeg( spherical.phi );
  203. lon = THREE.MathUtils.radToDeg( spherical.theta );
  204. }
  205. this.handleResize();
  206. setOrientation( this );
  207. };