webgl_loader_md2_control.html 7.4 KB

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