FirstPersonControls.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author paulirish / http://paulirish.com/
  5. */
  6. THREE.FirstPersonControls = function ( object, domElement ) {
  7. this.object = object;
  8. this.target = new THREE.Vector3( 0, 0, 0 );
  9. this.domElement = ( domElement !== undefined ) ? domElement : document;
  10. this.movementSpeed = 1.0;
  11. this.lookSpeed = 0.005;
  12. this.noFly = false;
  13. this.lookVertical = true;
  14. this.autoForward = false;
  15. this.activeLook = true;
  16. this.heightSpeed = false;
  17. this.heightCoef = 1.0;
  18. this.heightMin = 0.0;
  19. this.constrainVertical = false;
  20. this.verticalMin = 0;
  21. this.verticalMax = Math.PI;
  22. this.autoSpeedFactor = 0.0;
  23. this.mouseX = 0;
  24. this.mouseY = 0;
  25. this.lat = 0;
  26. this.lon = 0;
  27. this.phi = 0;
  28. this.theta = 0;
  29. this.moveForward = false;
  30. this.moveBackward = false;
  31. this.moveLeft = false;
  32. this.moveRight = false;
  33. this.freeze = false;
  34. this.mouseDragOn = false;
  35. if ( this.domElement === document ) {
  36. this.viewHalfX = window.innerWidth / 2;
  37. this.viewHalfY = window.innerHeight / 2;
  38. } else {
  39. this.viewHalfX = this.domElement.offsetWidth / 2;
  40. this.viewHalfY = this.domElement.offsetHeight / 2;
  41. this.domElement.setAttribute( 'tabindex', -1 );
  42. }
  43. this.onMouseDown = function ( event ) {
  44. if ( this.domElement !== document ) {
  45. this.domElement.focus();
  46. }
  47. event.preventDefault();
  48. event.stopPropagation();
  49. if ( this.activeLook ) {
  50. switch ( event.button ) {
  51. case 0: this.moveForward = true; break;
  52. case 2: this.moveBackward = true; break;
  53. }
  54. }
  55. this.mouseDragOn = true;
  56. };
  57. this.onMouseUp = function ( event ) {
  58. event.preventDefault();
  59. event.stopPropagation();
  60. if ( this.activeLook ) {
  61. switch ( event.button ) {
  62. case 0: this.moveForward = false; break;
  63. case 2: this.moveBackward = false; break;
  64. }
  65. }
  66. this.mouseDragOn = false;
  67. };
  68. this.onMouseMove = function ( event ) {
  69. if ( this.domElement === document ) {
  70. this.mouseX = event.pageX - this.viewHalfX;
  71. this.mouseY = event.pageY - this.viewHalfY;
  72. } else {
  73. this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
  74. this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
  75. }
  76. };
  77. this.onKeyDown = function ( event ) {
  78. switch( event.keyCode ) {
  79. case 38: /*up*/
  80. case 87: /*W*/ this.moveForward = true; break;
  81. case 37: /*left*/
  82. case 65: /*A*/ this.moveLeft = true; break;
  83. case 40: /*down*/
  84. case 83: /*S*/ this.moveBackward = true; break;
  85. case 39: /*right*/
  86. case 68: /*D*/ this.moveRight = true; break;
  87. case 82: /*R*/ this.moveUp = true; break;
  88. case 70: /*F*/ this.moveDown = true; break;
  89. case 81: this.freeze = !this.freeze; break;
  90. }
  91. };
  92. this.onKeyUp = function ( event ) {
  93. switch( event.keyCode ) {
  94. case 38: /*up*/
  95. case 87: /*W*/ this.moveForward = false; break;
  96. case 37: /*left*/
  97. case 65: /*A*/ this.moveLeft = false; break;
  98. case 40: /*down*/
  99. case 83: /*S*/ this.moveBackward = false; break;
  100. case 39: /*right*/
  101. case 68: /*D*/ this.moveRight = false; break;
  102. case 82: /*R*/ this.moveUp = false; break;
  103. case 70: /*F*/ this.moveDown = false; break;
  104. }
  105. };
  106. this.update = function( delta ) {
  107. delta *= 0.001;
  108. if ( !this.freeze ) {
  109. if ( this.heightSpeed ) {
  110. var y = THREE.Math.clamp( this.object.position.y, this.heightMin, this.heightMax );
  111. var heightDelta = y - this.heightMin;
  112. this.autoSpeedFactor = delta * ( heightDelta * this.heightCoef );
  113. } else {
  114. this.autoSpeedFactor = 0.0;
  115. }
  116. var actualMoveSpeed = delta * this.movementSpeed;
  117. if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.object.translateZ( - ( actualMoveSpeed + this.autoSpeedFactor ) );
  118. if ( this.moveBackward ) this.object.translateZ( actualMoveSpeed );
  119. if ( this.moveLeft ) this.object.translateX( - actualMoveSpeed );
  120. if ( this.moveRight ) this.object.translateX( actualMoveSpeed );
  121. if ( this.moveUp ) this.object.translateY( actualMoveSpeed );
  122. if ( this.moveDown ) this.object.translateY( - actualMoveSpeed );
  123. var actualLookSpeed = delta * this.lookSpeed;
  124. if ( !this.activeLook ) {
  125. actualLookSpeed = 0;
  126. }
  127. this.lon += this.mouseX * actualLookSpeed;
  128. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed;
  129. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  130. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  131. this.theta = this.lon * Math.PI / 180;
  132. var targetPosition = this.target,
  133. position = this.object.position;
  134. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  135. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  136. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  137. }
  138. var verticalLookRatio = 1;
  139. if ( this.constrainVertical ) {
  140. verticalLookRatio = Math.PI / ( this.verticalMax - this.verticalMin );
  141. }
  142. this.lon += this.mouseX * actualLookSpeed;
  143. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed * verticalLookRatio;
  144. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  145. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  146. this.theta = this.lon * Math.PI / 180;
  147. if ( this.constrainVertical ) {
  148. this.phi = THREE.Math.mapLinear( this.phi, 0, Math.PI, this.verticalMin, this.verticalMax );
  149. }
  150. var targetPosition = this.target,
  151. position = this.object.position;
  152. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  153. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  154. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  155. this.object.lookAt( targetPosition );
  156. };
  157. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  158. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  159. this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
  160. this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
  161. this.domElement.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
  162. this.domElement.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
  163. function bind( scope, fn ) {
  164. return function () {
  165. fn.apply( scope, arguments );
  166. };
  167. };
  168. };