misc_controls_pointerlock.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - pointerlock controls</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. <style>
  9. #blocker {
  10. position: absolute;
  11. width: 100%;
  12. height: 100%;
  13. background-color: rgba(0,0,0,0.5);
  14. }
  15. #instructions {
  16. width: 100%;
  17. height: 100%;
  18. display: -webkit-box;
  19. display: -moz-box;
  20. display: box;
  21. -webkit-box-orient: horizontal;
  22. -moz-box-orient: horizontal;
  23. box-orient: horizontal;
  24. -webkit-box-pack: center;
  25. -moz-box-pack: center;
  26. box-pack: center;
  27. -webkit-box-align: center;
  28. -moz-box-align: center;
  29. box-align: center;
  30. color: #ffffff;
  31. text-align: center;
  32. font-family: Arial;
  33. font-size: 14px;
  34. line-height: 24px;
  35. cursor: pointer;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="blocker">
  41. <div id="instructions">
  42. <span style="font-size:36px">Click to play</span>
  43. <br /><br />
  44. Move: WASD<br/>
  45. Jump: SPACE<br/>
  46. Look: MOUSE
  47. </div>
  48. </div>
  49. <script type="module">
  50. import * as THREE from '../build/three.module.js';
  51. import { PointerLockControls } from './jsm/controls/PointerLockControls.js';
  52. var camera, scene, renderer, controls;
  53. var objects = [];
  54. var raycaster;
  55. var moveForward = false;
  56. var moveBackward = false;
  57. var moveLeft = false;
  58. var moveRight = false;
  59. var canJump = false;
  60. var prevTime = performance.now();
  61. var velocity = new THREE.Vector3();
  62. var direction = new THREE.Vector3();
  63. var vertex = new THREE.Vector3();
  64. var color = new THREE.Color();
  65. init();
  66. animate();
  67. function init() {
  68. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
  69. camera.position.y = 10;
  70. scene = new THREE.Scene();
  71. scene.background = new THREE.Color( 0xffffff );
  72. scene.fog = new THREE.Fog( 0xffffff, 0, 750 );
  73. var light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
  74. light.position.set( 0.5, 1, 0.75 );
  75. scene.add( light );
  76. controls = new PointerLockControls( camera, document.body );
  77. var blocker = document.getElementById( 'blocker' );
  78. var instructions = document.getElementById( 'instructions' );
  79. instructions.addEventListener( 'click', function () {
  80. controls.lock();
  81. }, false );
  82. controls.addEventListener( 'lock', function () {
  83. instructions.style.display = 'none';
  84. blocker.style.display = 'none';
  85. } );
  86. controls.addEventListener( 'unlock', function () {
  87. blocker.style.display = 'block';
  88. instructions.style.display = '';
  89. } );
  90. scene.add( controls.getObject() );
  91. var onKeyDown = function ( event ) {
  92. switch ( event.keyCode ) {
  93. case 38: // up
  94. case 87: // w
  95. moveForward = true;
  96. break;
  97. case 37: // left
  98. case 65: // a
  99. moveLeft = true;
  100. break;
  101. case 40: // down
  102. case 83: // s
  103. moveBackward = true;
  104. break;
  105. case 39: // right
  106. case 68: // d
  107. moveRight = true;
  108. break;
  109. case 32: // space
  110. if ( canJump === true ) velocity.y += 350;
  111. canJump = false;
  112. break;
  113. }
  114. };
  115. var onKeyUp = function ( event ) {
  116. switch ( event.keyCode ) {
  117. case 38: // up
  118. case 87: // w
  119. moveForward = false;
  120. break;
  121. case 37: // left
  122. case 65: // a
  123. moveLeft = false;
  124. break;
  125. case 40: // down
  126. case 83: // s
  127. moveBackward = false;
  128. break;
  129. case 39: // right
  130. case 68: // d
  131. moveRight = false;
  132. break;
  133. }
  134. };
  135. document.addEventListener( 'keydown', onKeyDown, false );
  136. document.addEventListener( 'keyup', onKeyUp, false );
  137. raycaster = new THREE.Raycaster( new THREE.Vector3(), new THREE.Vector3( 0, - 1, 0 ), 0, 10 );
  138. // floor
  139. var floorGeometry = new THREE.PlaneBufferGeometry( 2000, 2000, 100, 100 );
  140. floorGeometry.rotateX( - Math.PI / 2 );
  141. // vertex displacement
  142. var position = floorGeometry.attributes.position;
  143. for ( var i = 0, l = position.count; i < l; i ++ ) {
  144. vertex.fromBufferAttribute( position, i );
  145. vertex.x += Math.random() * 20 - 10;
  146. vertex.y += Math.random() * 2;
  147. vertex.z += Math.random() * 20 - 10;
  148. position.setXYZ( i, vertex.x, vertex.y, vertex.z );
  149. }
  150. floorGeometry = floorGeometry.toNonIndexed(); // ensure each face has unique vertices
  151. position = floorGeometry.attributes.position;
  152. var colors = [];
  153. for ( var i = 0, l = position.count; i < l; i ++ ) {
  154. color.setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
  155. colors.push( color.r, color.g, color.b );
  156. }
  157. floorGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  158. var floorMaterial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } );
  159. var floor = new THREE.Mesh( floorGeometry, floorMaterial );
  160. scene.add( floor );
  161. // objects
  162. var boxGeometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  163. boxGeometry = boxGeometry.toNonIndexed(); // ensure each face has unique vertices
  164. position = boxGeometry.attributes.position;
  165. colors = [];
  166. for ( var i = 0, l = position.count; i < l; i ++ ) {
  167. color.setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
  168. colors.push( color.r, color.g, color.b );
  169. }
  170. boxGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  171. for ( var i = 0; i < 500; i ++ ) {
  172. var boxMaterial = new THREE.MeshPhongMaterial( { specular: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors } );
  173. boxMaterial.color.setHSL( Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
  174. var box = new THREE.Mesh( boxGeometry, boxMaterial );
  175. box.position.x = Math.floor( Math.random() * 20 - 10 ) * 20;
  176. box.position.y = Math.floor( Math.random() * 20 ) * 20 + 10;
  177. box.position.z = Math.floor( Math.random() * 20 - 10 ) * 20;
  178. scene.add( box );
  179. objects.push( box );
  180. }
  181. //
  182. renderer = new THREE.WebGLRenderer( { antialias: true } );
  183. renderer.setPixelRatio( window.devicePixelRatio );
  184. renderer.setSize( window.innerWidth, window.innerHeight );
  185. document.body.appendChild( renderer.domElement );
  186. //
  187. window.addEventListener( 'resize', onWindowResize, false );
  188. }
  189. function onWindowResize() {
  190. camera.aspect = window.innerWidth / window.innerHeight;
  191. camera.updateProjectionMatrix();
  192. renderer.setSize( window.innerWidth, window.innerHeight );
  193. }
  194. function animate() {
  195. requestAnimationFrame( animate );
  196. if ( controls.isLocked === true ) {
  197. raycaster.ray.origin.copy( controls.getObject().position );
  198. raycaster.ray.origin.y -= 10;
  199. var intersections = raycaster.intersectObjects( objects );
  200. var onObject = intersections.length > 0;
  201. var time = performance.now();
  202. var delta = ( time - prevTime ) / 1000;
  203. velocity.x -= velocity.x * 10.0 * delta;
  204. velocity.z -= velocity.z * 10.0 * delta;
  205. velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
  206. direction.z = Number( moveForward ) - Number( moveBackward );
  207. direction.x = Number( moveRight ) - Number( moveLeft );
  208. direction.normalize(); // this ensures consistent movements in all directions
  209. if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
  210. if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * delta;
  211. if ( onObject === true ) {
  212. velocity.y = Math.max( 0, velocity.y );
  213. canJump = true;
  214. }
  215. controls.moveRight( - velocity.x * delta );
  216. controls.moveForward( - velocity.z * delta );
  217. controls.getObject().position.y += ( velocity.y * delta ); // new behavior
  218. if ( controls.getObject().position.y < 10 ) {
  219. velocity.y = 0;
  220. controls.getObject().position.y = 10;
  221. canJump = true;
  222. }
  223. prevTime = time;
  224. }
  225. renderer.render( scene, camera );
  226. }
  227. </script>
  228. </body>
  229. </html>