webgl_loader_md2_control.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morphtargets - MD2 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. body {
  10. background-color: #fff;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MD2 Loader<br />
  21. use arrows to control characters, mouse for camera
  22. </div>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../build/three.module.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three';
  36. import Stats from 'three/addons/libs/stats.module.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { MD2CharacterComplex } from 'three/addons/misc/MD2CharacterComplex.js';
  39. import { Gyroscope } from 'three/addons/misc/Gyroscope.js';
  40. let SCREEN_WIDTH = window.innerWidth;
  41. let SCREEN_HEIGHT = window.innerHeight;
  42. let container, stats;
  43. let camera, scene, renderer;
  44. const characters = [];
  45. let nCharacters = 0;
  46. let cameraControls;
  47. const controls = {
  48. moveForward: false,
  49. moveBackward: false,
  50. moveLeft: false,
  51. moveRight: false
  52. };
  53. const clock = new THREE.Clock();
  54. init();
  55. animate();
  56. function init() {
  57. container = document.createElement( 'div' );
  58. document.body.appendChild( container );
  59. // CAMERA
  60. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 4000 );
  61. camera.position.set( 0, 150, 1300 );
  62. // SCENE
  63. scene = new THREE.Scene();
  64. scene.background = new THREE.Color( 0xffffff );
  65. scene.fog = new THREE.Fog( 0xffffff, 1000, 4000 );
  66. scene.add( camera );
  67. // LIGHTS
  68. scene.add( new THREE.AmbientLight( 0x222222 ) );
  69. const light = new THREE.DirectionalLight( 0xffffff, 2.25 );
  70. light.position.set( 200, 450, 500 );
  71. light.castShadow = true;
  72. light.shadow.mapSize.width = 1024;
  73. light.shadow.mapSize.height = 512;
  74. light.shadow.camera.near = 100;
  75. light.shadow.camera.far = 1200;
  76. light.shadow.camera.left = - 1000;
  77. light.shadow.camera.right = 1000;
  78. light.shadow.camera.top = 350;
  79. light.shadow.camera.bottom = - 350;
  80. scene.add( light );
  81. // scene.add( new THREE.CameraHelper( light.shadow.camera ) );
  82. // GROUND
  83. const gt = new THREE.TextureLoader().load( 'textures/terrain/grasslight-big.jpg' );
  84. const gg = new THREE.PlaneGeometry( 16000, 16000 );
  85. const gm = new THREE.MeshPhongMaterial( { color: 0xffffff, map: gt } );
  86. const ground = new THREE.Mesh( gg, gm );
  87. ground.rotation.x = - Math.PI / 2;
  88. ground.material.map.repeat.set( 64, 64 );
  89. ground.material.map.wrapS = THREE.RepeatWrapping;
  90. ground.material.map.wrapT = THREE.RepeatWrapping;
  91. ground.material.map.encoding = THREE.sRGBEncoding;
  92. // note that because the ground does not cast a shadow, .castShadow is left false
  93. ground.receiveShadow = true;
  94. scene.add( ground );
  95. // RENDERER
  96. renderer = new THREE.WebGLRenderer( { antialias: true } );
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  99. container.appendChild( renderer.domElement );
  100. //
  101. renderer.outputEncoding = THREE.sRGBEncoding;
  102. renderer.shadowMap.enabled = true;
  103. renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  104. // STATS
  105. stats = new Stats();
  106. container.appendChild( stats.dom );
  107. // EVENTS
  108. window.addEventListener( 'resize', onWindowResize );
  109. document.addEventListener( 'keydown', onKeyDown );
  110. document.addEventListener( 'keyup', onKeyUp );
  111. // CONTROLS
  112. cameraControls = new OrbitControls( camera, renderer.domElement );
  113. cameraControls.target.set( 0, 50, 0 );
  114. cameraControls.update();
  115. // CHARACTER
  116. const configOgro = {
  117. baseUrl: 'models/md2/ogro/',
  118. body: 'ogro.md2',
  119. skins: [ 'grok.jpg', 'ogrobase.png', 'arboshak.png', 'ctf_r.png', 'ctf_b.png', 'darkam.png', 'freedom.png',
  120. 'gib.png', 'gordogh.png', 'igdosh.png', 'khorne.png', 'nabogro.png',
  121. 'sharokh.png' ],
  122. weapons: [[ 'weapon.md2', 'weapon.jpg' ]],
  123. animations: {
  124. move: 'run',
  125. idle: 'stand',
  126. jump: 'jump',
  127. attack: 'attack',
  128. crouchMove: 'cwalk',
  129. crouchIdle: 'cstand',
  130. crouchAttach: 'crattack'
  131. },
  132. walkSpeed: 350,
  133. crouchSpeed: 175
  134. };
  135. const nRows = 1;
  136. const nSkins = configOgro.skins.length;
  137. nCharacters = nSkins * nRows;
  138. for ( let i = 0; i < nCharacters; i ++ ) {
  139. const character = new MD2CharacterComplex();
  140. character.scale = 3;
  141. character.controls = controls;
  142. characters.push( character );
  143. }
  144. const baseCharacter = new MD2CharacterComplex();
  145. baseCharacter.scale = 3;
  146. baseCharacter.onLoadComplete = function () {
  147. let k = 0;
  148. for ( let j = 0; j < nRows; j ++ ) {
  149. for ( let i = 0; i < nSkins; i ++ ) {
  150. const cloneCharacter = characters[ k ];
  151. cloneCharacter.shareParts( baseCharacter );
  152. // cast and receive shadows
  153. cloneCharacter.enableShadows( true );
  154. cloneCharacter.setWeapon( 0 );
  155. cloneCharacter.setSkin( i );
  156. cloneCharacter.root.position.x = ( i - nSkins / 2 ) * 150;
  157. cloneCharacter.root.position.z = j * 250;
  158. scene.add( cloneCharacter.root );
  159. k ++;
  160. }
  161. }
  162. const gyro = new Gyroscope();
  163. gyro.add( camera );
  164. gyro.add( light, light.target );
  165. characters[ Math.floor( nSkins / 2 ) ].root.add( gyro );
  166. };
  167. baseCharacter.loadParts( configOgro );
  168. }
  169. // EVENT HANDLERS
  170. function onWindowResize() {
  171. SCREEN_WIDTH = window.innerWidth;
  172. SCREEN_HEIGHT = window.innerHeight;
  173. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  174. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  175. camera.updateProjectionMatrix();
  176. }
  177. function onKeyDown( event ) {
  178. switch ( event.code ) {
  179. case 'ArrowUp':
  180. case 'KeyW': controls.moveForward = true; break;
  181. case 'ArrowDown':
  182. case 'KeyS': controls.moveBackward = true; break;
  183. case 'ArrowLeft':
  184. case 'KeyA': controls.moveLeft = true; break;
  185. case 'ArrowRight':
  186. case 'KeyD': controls.moveRight = true; break;
  187. // case 'KeyC': controls.crouch = true; break;
  188. // case 'Space': controls.jump = true; break;
  189. // case 'ControlLeft':
  190. // case 'ControlRight': controls.attack = true; break;
  191. }
  192. }
  193. function onKeyUp( event ) {
  194. switch ( event.code ) {
  195. case 'ArrowUp':
  196. case 'KeyW': controls.moveForward = false; break;
  197. case 'ArrowDown':
  198. case 'KeyS': controls.moveBackward = false; break;
  199. case 'ArrowLeft':
  200. case 'KeyA': controls.moveLeft = false; break;
  201. case 'ArrowRight':
  202. case 'KeyD': controls.moveRight = false; break;
  203. // case 'KeyC': controls.crouch = false; break;
  204. // case 'Space': controls.jump = false; break;
  205. // case 'ControlLeft':
  206. // case 'ControlRight': controls.attack = false; break;
  207. }
  208. }
  209. //
  210. function animate() {
  211. requestAnimationFrame( animate );
  212. render();
  213. stats.update();
  214. }
  215. function render() {
  216. const delta = clock.getDelta();
  217. for ( let i = 0; i < nCharacters; i ++ ) {
  218. characters[ i ].update( delta );
  219. }
  220. renderer.render( scene, camera );
  221. }
  222. </script>
  223. </body>
  224. </html>