123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - morphtargets - MD2 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>
- body {
- background-color: #fff;
- color: #444;
- }
- a {
- color: #08f;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MD2 Loader -
- Ogro by <a href="http://planetquake.gamespy.com/View.php?view=Quake2.Detail&id=556">Magarnigal</a><br />
- use arrows to control characters, mouse for camera
- </div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import Stats from './jsm/libs/stats.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { MD2CharacterComplex } from './jsm/misc/MD2CharacterComplex.js';
- import { Gyroscope } from './jsm/misc/Gyroscope.js';
- var SCREEN_WIDTH = window.innerWidth;
- var SCREEN_HEIGHT = window.innerHeight;
- var container, stats;
- var camera, scene, renderer;
- var characters = [];
- var nCharacters = 0;
- var cameraControls;
- var controls = {
- moveForward: false,
- moveBackward: false,
- moveLeft: false,
- moveRight: false
- };
- var clock = new THREE.Clock();
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- // CAMERA
- camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
- camera.position.set( 0, 150, 1300 );
- // SCENE
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0xffffff );
- scene.fog = new THREE.Fog( 0xffffff, 1000, 4000 );
- scene.add( camera );
- // LIGHTS
- scene.add( new THREE.AmbientLight( 0x222222 ) );
- var light = new THREE.DirectionalLight( 0xffffff, 2.25 );
- light.position.set( 200, 450, 500 );
- light.castShadow = true;
- light.shadow.mapSize.width = 1024;
- light.shadow.mapSize.height = 512;
- light.shadow.camera.near = 100;
- light.shadow.camera.far = 1200;
- light.shadow.camera.left = - 1000;
- light.shadow.camera.right = 1000;
- light.shadow.camera.top = 350;
- light.shadow.camera.bottom = - 350;
- scene.add( light );
- // scene.add( new CameraHelper( light.shadow.camera ) );
- // GROUND
- var gt = new THREE.TextureLoader().load( "textures/terrain/grasslight-big.jpg" );
- var gg = new THREE.PlaneBufferGeometry( 16000, 16000 );
- var gm = new THREE.MeshPhongMaterial( { color: 0xffffff, map: gt } );
- var ground = new THREE.Mesh( gg, gm );
- ground.rotation.x = - Math.PI / 2;
- ground.material.map.repeat.set( 64, 64 );
- ground.material.map.wrapS = THREE.RepeatWrapping;
- ground.material.map.wrapT = THREE.RepeatWrapping;
- ground.material.map.encoding = THREE.sRGBEncoding;
- // note that because the ground does not cast a shadow, .castShadow is left false
- ground.receiveShadow = true;
- scene.add( ground );
- // RENDERER
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- container.appendChild( renderer.domElement );
- //
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFSoftShadowMap;
- // STATS
- stats = new Stats();
- container.appendChild( stats.dom );
- // EVENTS
- window.addEventListener( 'resize', onWindowResize, false );
- document.addEventListener( 'keydown', onKeyDown, false );
- document.addEventListener( 'keyup', onKeyUp, false );
- // CONTROLS
- cameraControls = new OrbitControls( camera, renderer.domElement );
- cameraControls.target.set( 0, 50, 0 );
- cameraControls.update();
- // CHARACTER
- var configOgro = {
- baseUrl: "models/md2/ogro/",
- body: "ogro.md2",
- skins: [ "grok.jpg", "ogrobase.png", "arboshak.png", "ctf_r.png", "ctf_b.png", "darkam.png", "freedom.png",
- "gib.png", "gordogh.png", "igdosh.png", "khorne.png", "nabogro.png",
- "sharokh.png" ],
- weapons: [[ "weapon.md2", "weapon.jpg" ]],
- animations: {
- move: "run",
- idle: "stand",
- jump: "jump",
- attack: "attack",
- crouchMove: "cwalk",
- crouchIdle: "cstand",
- crouchAttach: "crattack"
- },
- walkSpeed: 350,
- crouchSpeed: 175
- };
- var nRows = 1;
- var nSkins = configOgro.skins.length;
- nCharacters = nSkins * nRows;
- for ( var i = 0; i < nCharacters; i ++ ) {
- var character = new MD2CharacterComplex();
- character.scale = 3;
- character.controls = controls;
- characters.push( character );
- }
- var baseCharacter = new MD2CharacterComplex();
- baseCharacter.scale = 3;
- baseCharacter.onLoadComplete = function () {
- var k = 0;
- for ( var j = 0; j < nRows; j ++ ) {
- for ( var i = 0; i < nSkins; i ++ ) {
- var cloneCharacter = characters[ k ];
- cloneCharacter.shareParts( baseCharacter );
- // cast and receive shadows
- cloneCharacter.enableShadows( true );
- cloneCharacter.setWeapon( 0 );
- cloneCharacter.setSkin( i );
- cloneCharacter.root.position.x = ( i - nSkins / 2 ) * 150;
- cloneCharacter.root.position.z = j * 250;
- scene.add( cloneCharacter.root );
- k ++;
- }
- }
- var gyro = new Gyroscope();
- gyro.add( camera );
- gyro.add( light, light.target );
- characters[ Math.floor( nSkins / 2 ) ].root.add( gyro );
- };
- baseCharacter.loadParts( configOgro );
- }
- // EVENT HANDLERS
- function onWindowResize() {
- SCREEN_WIDTH = window.innerWidth;
- SCREEN_HEIGHT = window.innerHeight;
- renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
- camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
- camera.updateProjectionMatrix();
- }
- function onKeyDown( event ) {
- event.stopPropagation();
- switch ( event.keyCode ) {
- case 38: /*up*/
- case 87: /*W*/ controls.moveForward = true; break;
- case 40: /*down*/
- case 83: /*S*/ controls.moveBackward = true; break;
- case 37: /*left*/
- case 65: /*A*/ controls.moveLeft = true; break;
- case 39: /*right*/
- case 68: /*D*/ controls.moveRight = true; break;
- //case 67: /*C*/ controls.crouch = true; break;
- //case 32: /*space*/ controls.jump = true; break;
- //case 17: /*ctrl*/ controls.attack = true; break;
- }
- }
- function onKeyUp( event ) {
- event.stopPropagation();
- switch ( event.keyCode ) {
- case 38: /*up*/
- case 87: /*W*/ controls.moveForward = false; break;
- case 40: /*down*/
- case 83: /*S*/ controls.moveBackward = false; break;
- case 37: /*left*/
- case 65: /*A*/ controls.moveLeft = false; break;
- case 39: /*right*/
- case 68: /*D*/ controls.moveRight = false; break;
- //case 67: /*C*/ controls.crouch = false; break;
- //case 32: /*space*/ controls.jump = false; break;
- //case 17: /*ctrl*/ controls.attack = false; break;
- }
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- var delta = clock.getDelta();
- for ( var i = 0; i < nCharacters; i ++ ) {
- characters[ i ].update( delta );
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|