misc_fps.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - platformer demo</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - platformer demo. cubemap by <a href="http://www.zfight.com/" target="_blank" rel="noopener">Jochum Skoglund</a>.<br />Use arrow keys to look around, WASD to move and SPACE to jump.</div>
  11. <script type="module">
  12. import {
  13. CubeTextureLoader,
  14. Euler,
  15. Object3D,
  16. ObjectLoader,
  17. PerspectiveCamera,
  18. Raycaster,
  19. RGBFormat,
  20. Scene,
  21. Vector2,
  22. Vector3,
  23. WebGLRenderer
  24. } from "../build/three.module.js";
  25. // player motion parameters
  26. var motion = {
  27. airborne: false,
  28. position: new Vector3(), velocity: new Vector3(),
  29. rotation: new Vector2(), spinning: new Vector2()
  30. };
  31. motion.position.y = - 150;
  32. // game systems code
  33. var resetPlayer = function () {
  34. if ( motion.position.y < - 123 ) {
  35. motion.position.set( - 2, 7.7, 25 );
  36. motion.velocity.multiplyScalar( 0 );
  37. }
  38. };
  39. var keyboardControls = ( function () {
  40. var keys = { SP: 32, W: 87, A: 65, S: 83, D: 68, UP: 38, LT: 37, DN: 40, RT: 39 };
  41. var keysPressed = {};
  42. ( function ( watchedKeyCodes ) {
  43. var handler = function ( down ) {
  44. return function ( e ) {
  45. var index = watchedKeyCodes.indexOf( e.keyCode );
  46. if ( index >= 0 ) {
  47. keysPressed[ watchedKeyCodes[ index ] ] = down;
  48. e.preventDefault();
  49. }
  50. };
  51. };
  52. window.addEventListener( "keydown", handler( true ), false );
  53. window.addEventListener( "keyup", handler( false ), false );
  54. } )( [
  55. keys.SP, keys.W, keys.A, keys.S, keys.D, keys.UP, keys.LT, keys.DN, keys.RT
  56. ] );
  57. var forward = new Vector3();
  58. var sideways = new Vector3();
  59. return function () {
  60. if ( ! motion.airborne ) {
  61. // look around
  62. var sx = keysPressed[ keys.UP ] ? 0.03 : ( keysPressed[ keys.DN ] ? - 0.03 : 0 );
  63. var sy = keysPressed[ keys.LT ] ? 0.03 : ( keysPressed[ keys.RT ] ? - 0.03 : 0 );
  64. if ( Math.abs( sx ) >= Math.abs( motion.spinning.x ) ) motion.spinning.x = sx;
  65. if ( Math.abs( sy ) >= Math.abs( motion.spinning.y ) ) motion.spinning.y = sy;
  66. // move around
  67. forward.set( Math.sin( motion.rotation.y ), 0, Math.cos( motion.rotation.y ) );
  68. sideways.set( forward.z, 0, - forward.x );
  69. forward.multiplyScalar( keysPressed[ keys.W ] ? - 0.1 : ( keysPressed[ keys.S ] ? 0.1 : 0 ) );
  70. sideways.multiplyScalar( keysPressed[ keys.A ] ? - 0.1 : ( keysPressed[ keys.D ] ? 0.1 : 0 ) );
  71. var combined = forward.add( sideways );
  72. if ( Math.abs( combined.x ) >= Math.abs( motion.velocity.x ) ) motion.velocity.x = combined.x;
  73. if ( Math.abs( combined.y ) >= Math.abs( motion.velocity.y ) ) motion.velocity.y = combined.y;
  74. if ( Math.abs( combined.z ) >= Math.abs( motion.velocity.z ) ) motion.velocity.z = combined.z;
  75. //jump
  76. var vy = keysPressed[ keys.SP ] ? 0.7 : 0;
  77. motion.velocity.y += vy;
  78. }
  79. };
  80. } )();
  81. var jumpPads = ( function () {
  82. var pads = [ new Vector3( - 17.5, 8, - 10 ), new Vector3( 17.5, 8, - 10 ), new Vector3( 0, 8, 21 ) ];
  83. var temp = new Vector3();
  84. return function () {
  85. if ( ! motion.airborne ) {
  86. for ( var j = 0, n = pads.length; j < n; j ++ ) {
  87. if ( pads[ j ].distanceToSquared( motion.position ) < 2.3 ) {
  88. // calculate velocity towards another side of platform from jump pad position
  89. temp.copy( pads[ j ] );
  90. temp.y = 0;
  91. temp.setLength( - 0.8 );
  92. temp.y = 0.7;
  93. motion.airborne = true;
  94. motion.velocity.copy( temp );
  95. break;
  96. }
  97. }
  98. }
  99. };
  100. } )();
  101. var applyPhysics = ( function () {
  102. var timeStep = 5;
  103. var timeLeft = timeStep + 1;
  104. var birdsEye = 100;
  105. var kneeDeep = 0.4;
  106. var raycaster = new Raycaster();
  107. raycaster.ray.direction.set( 0, - 1, 0 );
  108. var angles = new Vector2();
  109. var displacement = new Vector3();
  110. return function ( dt ) {
  111. var platform = scene.getObjectByName( "platform", true );
  112. if ( platform ) {
  113. timeLeft += dt;
  114. // run several fixed-step iterations to approximate varying-step
  115. dt = 5;
  116. while ( timeLeft >= dt ) {
  117. var time = 0.3, damping = 0.93, gravity = 0.01, tau = 2 * Math.PI;
  118. raycaster.ray.origin.copy( motion.position );
  119. raycaster.ray.origin.y += birdsEye;
  120. var hits = raycaster.intersectObject( platform );
  121. motion.airborne = true;
  122. // are we above, or at most knee deep in, the platform?
  123. if ( ( hits.length > 0 ) && ( hits[ 0 ].face.normal.y > 0 ) ) {
  124. var actualHeight = hits[ 0 ].distance - birdsEye;
  125. // collision: stick to the surface if landing on it
  126. if ( ( motion.velocity.y <= 0 ) && ( Math.abs( actualHeight ) < kneeDeep ) ) {
  127. motion.position.y -= actualHeight;
  128. motion.velocity.y = 0;
  129. motion.airborne = false;
  130. }
  131. }
  132. if ( motion.airborne ) motion.velocity.y -= gravity;
  133. angles.copy( motion.spinning ).multiplyScalar( time );
  134. if ( ! motion.airborne ) motion.spinning.multiplyScalar( damping );
  135. displacement.copy( motion.velocity ).multiplyScalar( time );
  136. if ( ! motion.airborne ) motion.velocity.multiplyScalar( damping );
  137. motion.rotation.add( angles );
  138. motion.position.add( displacement );
  139. // limit the tilt at ±0.4 radians
  140. motion.rotation.x = Math.max( - 0.4, Math.min( + 0.4, motion.rotation.x ) );
  141. // wrap horizontal rotation to 0...2π
  142. motion.rotation.y += tau;
  143. motion.rotation.y %= tau;
  144. timeLeft -= dt;
  145. }
  146. }
  147. };
  148. } )();
  149. var updateCamera = ( function () {
  150. var euler = new Euler( 0, 0, 0, 'YXZ' );
  151. return function () {
  152. euler.x = motion.rotation.x;
  153. euler.y = motion.rotation.y;
  154. camera.quaternion.setFromEuler( euler );
  155. camera.position.copy( motion.position );
  156. camera.position.y += 3.0;
  157. };
  158. } )();
  159. // init 3D stuff
  160. function makePlatform( url ) {
  161. var placeholder = new Object3D();
  162. var loader = new ObjectLoader();
  163. loader.load( url, function ( platform ) {
  164. placeholder.add( platform );
  165. } );
  166. return placeholder;
  167. }
  168. var renderer = new WebGLRenderer( { antialias: true } );
  169. renderer.setPixelRatio( window.devicePixelRatio );
  170. document.body.appendChild( renderer.domElement );
  171. var camera = new PerspectiveCamera( 60, 1, 0.1, 9000 );
  172. var scene = new Scene();
  173. var envMap = new CubeTextureLoader().load( [
  174. 'textures/cube/skybox/px.jpg', // right
  175. 'textures/cube/skybox/nx.jpg', // left
  176. 'textures/cube/skybox/py.jpg', // top
  177. 'textures/cube/skybox/ny.jpg', // bottom
  178. 'textures/cube/skybox/pz.jpg', // back
  179. 'textures/cube/skybox/nz.jpg' // front
  180. ] );
  181. envMap.format = RGBFormat;
  182. scene.background = envMap;
  183. scene.add( makePlatform(
  184. 'models/json/platform/platform.json'
  185. ) );
  186. // start the game
  187. var start = function ( gameLoop, gameViewportSize ) {
  188. var resize = function () {
  189. var viewport = gameViewportSize();
  190. renderer.setSize( viewport.width, viewport.height );
  191. camera.aspect = viewport.width / viewport.height;
  192. camera.updateProjectionMatrix();
  193. };
  194. window.addEventListener( 'resize', resize, false );
  195. resize();
  196. var lastTimeStamp;
  197. var render = function ( timeStamp ) {
  198. var timeElapsed = lastTimeStamp ? timeStamp - lastTimeStamp : 0;
  199. lastTimeStamp = timeStamp;
  200. // call our game loop with the time elapsed since last rendering, in ms
  201. gameLoop( timeElapsed );
  202. renderer.render( scene, camera );
  203. requestAnimationFrame( render );
  204. };
  205. requestAnimationFrame( render );
  206. };
  207. var gameLoop = function ( dt ) {
  208. resetPlayer();
  209. keyboardControls();
  210. jumpPads();
  211. applyPhysics( dt );
  212. updateCamera();
  213. };
  214. var gameViewportSize = function () {
  215. return {
  216. width: window.innerWidth, height: window.innerHeight
  217. };
  218. };
  219. start( gameLoop, gameViewportSize );
  220. </script>
  221. </body>
  222. </html>