2
0

misc_fps.html 8.2 KB

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