QuakeCamera.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author alteredq / http://alteredqualia.com/
  4. * @author paulirish / http://paulirish.com/
  5. *
  6. * parameters = {
  7. * fov: <float>,
  8. * aspect: <float>,
  9. * near: <float>,
  10. * far: <float>,
  11. * target: <THREE.Object3D>,
  12. * movementSpeed: <float>,
  13. * lookSpeed: <float>,
  14. * noFly: <bool>,
  15. * lookVertical: <bool>,
  16. * autoForward: <bool>,
  17. * constrainVertical: <bool>,
  18. * verticalMin: <float>,
  19. * verticalMax: <float>,
  20. * heightSpeed: <bool>,
  21. * heightCoef: <float>,
  22. * heightMin: <float>,
  23. * heightMax: <float>,
  24. * domElement: <HTMLElement>,
  25. * }
  26. */
  27. THREE.QuakeCamera = function ( parameters ) {
  28. THREE.Camera.call( this, parameters.fov, parameters.aspect, parameters.near, parameters.far, parameters.target );
  29. this.movementSpeed = 1.0;
  30. this.lookSpeed = 0.005;
  31. this.noFly = false;
  32. this.lookVertical = true;
  33. this.autoForward = false;
  34. this.activeLook = true;
  35. this.heightSpeed = false;
  36. this.heightCoef = 1.0;
  37. this.heightMin = 0.0;
  38. this.constrainVertical = false;
  39. this.verticalMin = 0;
  40. this.verticalMax = 3.14;
  41. this.domElement = document;
  42. if ( parameters ) {
  43. if ( parameters.movementSpeed !== undefined ) this.movementSpeed = parameters.movementSpeed;
  44. if ( parameters.lookSpeed !== undefined ) this.lookSpeed = parameters.lookSpeed;
  45. if ( parameters.noFly !== undefined ) this.noFly = parameters.noFly;
  46. if ( parameters.lookVertical !== undefined ) this.lookVertical = parameters.lookVertical;
  47. if ( parameters.autoForward !== undefined ) this.autoForward = parameters.autoForward;
  48. if ( parameters.activeLook !== undefined ) this.activeLook = parameters.activeLook;
  49. if ( parameters.heightSpeed !== undefined ) this.heightSpeed = parameters.heightSpeed;
  50. if ( parameters.heightCoef !== undefined ) this.heightCoef = parameters.heightCoef;
  51. if ( parameters.heightMin !== undefined ) this.heightMin = parameters.heightMin;
  52. if ( parameters.heightMax !== undefined ) this.heightMax = parameters.heightMax;
  53. if ( parameters.constrainVertical !== undefined ) this.constrainVertical = parameters.constrainVertical;
  54. if ( parameters.verticalMin !== undefined ) this.verticalMin = parameters.verticalMin;
  55. if ( parameters.verticalMax !== undefined ) this.verticalMax = parameters.verticalMax;
  56. if ( parameters.domElement !== undefined ) this.domElement = parameters.domElement;
  57. }
  58. this.autoSpeedFactor = 0.0;
  59. this.mouseX = 0;
  60. this.mouseY = 0;
  61. this.lat = 0;
  62. this.lon = 0;
  63. this.phi = 0;
  64. this.theta = 0;
  65. this.moveForward = false;
  66. this.moveBackward = false;
  67. this.moveLeft = false;
  68. this.moveRight = false;
  69. this.freeze = false;
  70. this.mouseDragOn = false;
  71. this.windowHalfX = window.innerWidth / 2;
  72. this.windowHalfY = window.innerHeight / 2;
  73. this.onMouseDown = function ( event ) {
  74. event.preventDefault();
  75. event.stopPropagation();
  76. if ( this.activeLook ) {
  77. switch ( event.button ) {
  78. case 0: this.moveForward = true; break;
  79. case 2: this.moveBackward = true; break;
  80. }
  81. }
  82. this.mouseDragOn = true;
  83. };
  84. this.onMouseUp = function ( event ) {
  85. event.preventDefault();
  86. event.stopPropagation();
  87. if ( this.activeLook ) {
  88. switch ( event.button ) {
  89. case 0: this.moveForward = false; break;
  90. case 2: this.moveBackward = false; break;
  91. }
  92. }
  93. this.mouseDragOn = false;
  94. };
  95. this.onMouseMove = function ( event ) {
  96. this.mouseX = event.clientX - this.windowHalfX;
  97. this.mouseY = event.clientY - this.windowHalfY;
  98. };
  99. this.onKeyDown = function ( event ) {
  100. switch( event.keyCode ) {
  101. case 38: /*up*/
  102. case 87: /*W*/ this.moveForward = true; break;
  103. case 37: /*left*/
  104. case 65: /*A*/ this.moveLeft = true; break;
  105. case 40: /*down*/
  106. case 83: /*S*/ this.moveBackward = true; break;
  107. case 39: /*right*/
  108. case 68: /*D*/ this.moveRight = true; break;
  109. case 81: this.freeze = !this.freeze; break;
  110. }
  111. };
  112. this.onKeyUp = function ( event ) {
  113. switch( event.keyCode ) {
  114. case 38: /*up*/
  115. case 87: /*W*/ this.moveForward = false; break;
  116. case 37: /*left*/
  117. case 65: /*A*/ this.moveLeft = false; break;
  118. case 40: /*down*/
  119. case 83: /*S*/ this.moveBackward = false; break;
  120. case 39: /*right*/
  121. case 68: /*D*/ this.moveRight = false; break;
  122. }
  123. };
  124. this.update = function() {
  125. if ( !this.freeze ) {
  126. if ( this.heightSpeed ) {
  127. var y = clamp( this.position.y, this.heightMin, this.heightMax ),
  128. delta = y - this.heightMin;
  129. this.autoSpeedFactor = delta * this.heightCoef;
  130. } else {
  131. this.autoSpeedFactor = 0.0;
  132. }
  133. if ( this.moveForward || ( this.autoForward && !this.moveBackward ) ) this.translateZ( - ( this.movementSpeed + this.autoSpeedFactor ) );
  134. if ( this.moveBackward ) this.translateZ( this.movementSpeed );
  135. if ( this.moveLeft ) this.translateX( - this.movementSpeed );
  136. if ( this.moveRight ) this.translateX( this.movementSpeed );
  137. var actualLookSpeed = this.lookSpeed;
  138. if ( !this.activeLook ) {
  139. actualLookSpeed = 0;
  140. }
  141. this.lon += this.mouseX * actualLookSpeed;
  142. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed;
  143. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  144. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  145. this.theta = this.lon * Math.PI / 180;
  146. var targetPosition = this.target.position,
  147. position = this.position;
  148. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  149. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  150. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  151. }
  152. this.lon += this.mouseX * actualLookSpeed;
  153. if( this.lookVertical ) this.lat -= this.mouseY * actualLookSpeed;
  154. this.lat = Math.max( - 85, Math.min( 85, this.lat ) );
  155. this.phi = ( 90 - this.lat ) * Math.PI / 180;
  156. this.theta = this.lon * Math.PI / 180;
  157. if ( this.constrainVertical ) {
  158. this.phi = map_linear( this.phi, 0, 3.14, this.verticalMin, this.verticalMax );
  159. }
  160. var targetPosition = this.target.position,
  161. position = this.position;
  162. targetPosition.x = position.x + 100 * Math.sin( this.phi ) * Math.cos( this.theta );
  163. targetPosition.y = position.y + 100 * Math.cos( this.phi );
  164. targetPosition.z = position.z + 100 * Math.sin( this.phi ) * Math.sin( this.theta );
  165. this.supr.update.call( this );
  166. };
  167. this.domElement.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );
  168. this.domElement.addEventListener( 'mousemove', bind( this, this.onMouseMove ), false );
  169. this.domElement.addEventListener( 'mousedown', bind( this, this.onMouseDown ), false );
  170. this.domElement.addEventListener( 'mouseup', bind( this, this.onMouseUp ), false );
  171. this.domElement.addEventListener( 'keydown', bind( this, this.onKeyDown ), false );
  172. this.domElement.addEventListener( 'keyup', bind( this, this.onKeyUp ), false );
  173. function bind( scope, fn ) {
  174. return function () {
  175. fn.apply( scope, arguments );
  176. };
  177. };
  178. function map_linear( x, sa, sb, ea, eb ) {
  179. return ( x - sa ) * ( eb - ea ) / ( sb - sa ) + ea;
  180. };
  181. function clamp_bottom( x, a ) {
  182. return x < a ? a : x;
  183. };
  184. function clamp( x, a, b ) {
  185. return x < a ? a : ( x > b ? b : x );
  186. };
  187. };
  188. THREE.QuakeCamera.prototype = new THREE.Camera();
  189. THREE.QuakeCamera.prototype.constructor = THREE.QuakeCamera;
  190. THREE.QuakeCamera.prototype.supr = THREE.Camera.prototype;
  191. THREE.QuakeCamera.prototype.translate = function ( distance, axis ) {
  192. this.matrix.rotateAxis( axis );
  193. if ( this.noFly ) {
  194. axis.y = 0;
  195. }
  196. this.position.addSelf( axis.multiplyScalar( distance ) );
  197. this.target.position.addSelf( axis.multiplyScalar( distance ) );
  198. };