123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js - pointerlock controls</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- <style>
- #blocker {
- position: absolute;
- width: 100%;
- height: 100%;
- background-color: rgba(0,0,0,0.5);
- }
- #instructions {
- width: 100%;
- height: 100%;
- display: -webkit-box;
- display: -moz-box;
- display: box;
- -webkit-box-orient: horizontal;
- -moz-box-orient: horizontal;
- box-orient: horizontal;
- -webkit-box-pack: center;
- -moz-box-pack: center;
- box-pack: center;
- -webkit-box-align: center;
- -moz-box-align: center;
- box-align: center;
- color: #ffffff;
- text-align: center;
- font-family: Arial;
- font-size: 14px;
- line-height: 24px;
- cursor: pointer;
- }
- </style>
- </head>
- <body>
- <div id="blocker">
- <div id="instructions">
- <span style="font-size:36px">Click to play</span>
- <br /><br />
- Move: WASD<br/>
- Jump: SPACE<br/>
- Look: MOUSE
- </div>
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { PointerLockControls } from './jsm/controls/PointerLockControls.js';
- var camera, scene, renderer, controls;
- var objects = [];
- var raycaster;
- var moveForward = false;
- var moveBackward = false;
- var moveLeft = false;
- var moveRight = false;
- var canJump = false;
- var prevTime = performance.now();
- var velocity = new THREE.Vector3();
- var direction = new THREE.Vector3();
- var vertex = new THREE.Vector3();
- var color = new THREE.Color();
- init();
- animate();
- function init() {
- camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 1000 );
- camera.position.y = 10;
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- scene.fog = new THREE.Fog( 0xffffff, 0, 750 );
- var light = new THREE.HemisphereLight( 0xeeeeff, 0x777788, 0.75 );
- light.position.set( 0.5, 1, 0.75 );
- scene.add( light );
- controls = new PointerLockControls( camera, document.body );
- var blocker = document.getElementById( 'blocker' );
- var instructions = document.getElementById( 'instructions' );
- instructions.addEventListener( 'click', function () {
- controls.lock();
- }, false );
- controls.addEventListener( 'lock', function () {
- instructions.style.display = 'none';
- blocker.style.display = 'none';
- } );
- controls.addEventListener( 'unlock', function () {
- blocker.style.display = 'block';
- instructions.style.display = '';
- } );
- scene.add( controls.getObject() );
- var onKeyDown = function ( event ) {
- switch ( event.keyCode ) {
- case 38: // up
- case 87: // w
- moveForward = true;
- break;
- case 37: // left
- case 65: // a
- moveLeft = true;
- break;
- case 40: // down
- case 83: // s
- moveBackward = true;
- break;
- case 39: // right
- case 68: // d
- moveRight = true;
- break;
- case 32: // space
- if ( canJump === true ) velocity.y += 350;
- canJump = false;
- break;
- }
- };
- var onKeyUp = function ( event ) {
- switch ( event.keyCode ) {
- case 38: // up
- case 87: // w
- moveForward = false;
- break;
- case 37: // left
- case 65: // a
- moveLeft = false;
- break;
- case 40: // down
- case 83: // s
- moveBackward = false;
- break;
- case 39: // right
- case 68: // d
- moveRight = false;
- break;
- }
- };
- document.addEventListener( 'keydown', onKeyDown, false );
- document.addEventListener( 'keyup', onKeyUp, false );
- raycaster = new THREE.Raycaster( new THREE.Vector3(), new THREE.Vector3( 0, - 1, 0 ), 0, 10 );
- // floor
- var floorGeometry = new THREE.PlaneBufferGeometry( 2000, 2000, 100, 100 );
- floorGeometry.rotateX( - Math.PI / 2 );
- // vertex displacement
- var position = floorGeometry.attributes.position;
- for ( var i = 0, l = position.count; i < l; i ++ ) {
- vertex.fromBufferAttribute( position, i );
- vertex.x += Math.random() * 20 - 10;
- vertex.y += Math.random() * 2;
- vertex.z += Math.random() * 20 - 10;
- position.setXYZ( i, vertex.x, vertex.y, vertex.z );
- }
- floorGeometry = floorGeometry.toNonIndexed(); // ensure each face has unique vertices
- position = floorGeometry.attributes.position;
- var colors = [];
- for ( var i = 0, l = position.count; i < l; i ++ ) {
- color.setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
- colors.push( color.r, color.g, color.b );
- }
- floorGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- var floorMaterial = new THREE.MeshBasicMaterial( { vertexColors: THREE.VertexColors } );
- var floor = new THREE.Mesh( floorGeometry, floorMaterial );
- scene.add( floor );
- // objects
- var boxGeometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
- boxGeometry = boxGeometry.toNonIndexed(); // ensure each face has unique vertices
- position = boxGeometry.attributes.position;
- colors = [];
- for ( var i = 0, l = position.count; i < l; i ++ ) {
- color.setHSL( Math.random() * 0.3 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
- colors.push( color.r, color.g, color.b );
- }
- boxGeometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
- for ( var i = 0; i < 500; i ++ ) {
- var boxMaterial = new THREE.MeshPhongMaterial( { specular: 0xffffff, flatShading: true, vertexColors: THREE.VertexColors } );
- boxMaterial.color.setHSL( Math.random() * 0.2 + 0.5, 0.75, Math.random() * 0.25 + 0.75 );
- var box = new THREE.Mesh( boxGeometry, boxMaterial );
- box.position.x = Math.floor( Math.random() * 20 - 10 ) * 20;
- box.position.y = Math.floor( Math.random() * 20 ) * 20 + 10;
- box.position.z = Math.floor( Math.random() * 20 - 10 ) * 20;
- scene.add( box );
- objects.push( box );
- }
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- document.body.appendChild( renderer.domElement );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- requestAnimationFrame( animate );
- if ( controls.isLocked === true ) {
- raycaster.ray.origin.copy( controls.getObject().position );
- raycaster.ray.origin.y -= 10;
- var intersections = raycaster.intersectObjects( objects );
- var onObject = intersections.length > 0;
- var time = performance.now();
- var delta = ( time - prevTime ) / 1000;
- velocity.x -= velocity.x * 10.0 * delta;
- velocity.z -= velocity.z * 10.0 * delta;
- velocity.y -= 9.8 * 100.0 * delta; // 100.0 = mass
- direction.z = Number( moveForward ) - Number( moveBackward );
- direction.x = Number( moveRight ) - Number( moveLeft );
- direction.normalize(); // this ensures consistent movements in all directions
- if ( moveForward || moveBackward ) velocity.z -= direction.z * 400.0 * delta;
- if ( moveLeft || moveRight ) velocity.x -= direction.x * 400.0 * delta;
- if ( onObject === true ) {
- velocity.y = Math.max( 0, velocity.y );
- canJump = true;
- }
- controls.moveRight( - velocity.x * delta );
- controls.moveForward( - velocity.z * delta );
- controls.getObject().position.y += ( velocity.y * delta ); // new behavior
- if ( controls.getObject().position.y < 10 ) {
- velocity.y = 0;
- controls.getObject().position.y = 10;
- canJump = true;
- }
- prevTime = time;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|