UNPKG

ggabcd-meshwalk

Version:

MeshWalk.js is a JS library which helps your TPS game development with three.js.

140 lines (106 loc) 3.93 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>=^.^=</title> <style> body{margin: 0;padding: 0; background: #000;} canvas{display: block;} .info{ color: #fff; position: absolute; top: 0; left: 0; } </style> </head> <body> <div class="info"> u can import terrain models as heightfields. </div> <script type="module"> import * as THREE from 'https://unpkg.com/three@0.120.0/build/three.module.js'; import { GLTFLoader } from 'https://unpkg.com/three@0.120.0/examples/jsm/loaders/GLTFLoader.js'; import * as MW from './meshwalk.module.js'; // See demo/3_cameraControl.html first MW.install( THREE ); const world = new MW.World(); const min = new THREE.Vector3( - 15, - 15, - 15 ); const max = new THREE.Vector3( 15, 15, 15 ); const partition = 5; const region = new MW.Octree( min, max, partition ); world.add( region ); const width = window.innerWidth; const height = window.innerHeight; const clock = new THREE.Clock(); const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera( 40, width / height, 1, 1000 ); camera.position.set( 0, 0, 8 ); const renderer = new THREE.WebGLRenderer(); renderer.setSize( width, height ); document.body.appendChild( renderer.domElement ); const playerRadius = 1; const playerObjectHolder = new THREE.Object3D(); playerObjectHolder.position.set( 0, 10, 0 ); scene.add( playerObjectHolder ); const sphere = new THREE.Mesh( new THREE.SphereGeometry( playerRadius, 16, 16 ), new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true} ) ); playerObjectHolder.add( sphere ); const playerController = new MW.CharacterController( playerObjectHolder, playerRadius ); world.add( playerController ); const keyInputControl = new MW.KeyInputControl(); const tpsCameraControls = new MW.TPSCameraControls( camera, // three.js camera playerObjectHolder, // tracking object renderer.domElement ); // bind events keyInputControl.addEventListener( 'movekeyon', () => playerController.isRunning = true ); keyInputControl.addEventListener( 'movekeyoff', () => playerController.isRunning = false ); keyInputControl.addEventListener( 'jumpkeypress', () => playerController.jump() ); // synk with keybord input and camera control input keyInputControl.addEventListener( 'movekeychange', () => { const cameraFrontAngle = tpsCameraControls.frontAngle; const characterFrontAngle = keyInputControl.frontAngle; playerController.direction = cameraFrontAngle + characterFrontAngle; } ); // 'updated' event is fired by `tpsCameraControls.update()` tpsCameraControls.addEventListener( 'update', () => { const cameraFrontAngle = tpsCameraControls.frontAngle; const characterFrontAngle = keyInputControl.frontAngle; playerController.direction = cameraFrontAngle + characterFrontAngle; } ); const loader = new GLTFLoader(); loader.load( 'terrain.glb', ( gltf ) => { const box = new THREE.Mesh( new THREE.BoxGeometry( 14, 1, 5 ), new THREE.MeshNormalMaterial() ); box.position.set( - 3, 7.5, - 13 ); scene.add( box ); const boxHelper = new THREE.WireframeHelper( box ); boxHelper.position.copy( box.position ); scene.add( boxHelper ); region.importThreeMesh( box ); const terrain = gltf.scene.children[ 0 ]; terrain.material = new THREE.MeshNormalMaterial(); terrain.scale.multiplyScalar( 2 ); scene.add( terrain ); const terrainHelper = new THREE.WireframeHelper( terrain ); terrainHelper.scale.copy( terrain.scale ); scene.add( terrainHelper ); region.importThreeMesh( terrain ); tpsCameraControls.colliderMeshes.push( terrain ); ( function update () { const delta = clock.getDelta(); requestAnimationFrame( update ); world.step( Math.min( delta, 0.02 ) ); tpsCameraControls.update( delta ); renderer.render( scene, camera ); } )(); } ); </script> </body> </html>