1
0

webgl_loader_md2.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - morphtargets - MD2</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - MD2 Loader
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import Stats from 'three/addons/libs/stats.module.js';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { MD2Character } from 'three/addons/misc/MD2Character.js';
  27. let SCREEN_WIDTH = window.innerWidth;
  28. let SCREEN_HEIGHT = window.innerHeight;
  29. let container, camera, scene, renderer;
  30. let character;
  31. let gui;
  32. const playbackConfig = {
  33. speed: 1.0,
  34. wireframe: false
  35. };
  36. let controls;
  37. const clock = new THREE.Clock();
  38. let stats;
  39. init();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. // CAMERA
  44. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  45. camera.position.set( 0, 2, 4 );
  46. // SCENE
  47. scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 0x050505 );
  49. scene.fog = new THREE.Fog( 0x050505, 2.5, 10 );
  50. // LIGHTS
  51. scene.add( new THREE.AmbientLight( 0x666666 ) );
  52. const light1 = new THREE.SpotLight( 0xffffff, 150 );
  53. light1.position.set( 2, 5, 10 );
  54. light1.angle = 0.5;
  55. light1.penumbra = 0.5;
  56. light1.castShadow = true;
  57. light1.shadow.mapSize.width = 1024;
  58. light1.shadow.mapSize.height = 1024;
  59. // scene.add( new THREE.CameraHelper( light1.shadow.camera ) );
  60. scene.add( light1 );
  61. const light2 = new THREE.SpotLight( 0xffffff, 150 );
  62. light2.position.set( - 1, 3.5, 3.5 );
  63. light2.angle = 0.5;
  64. light2.penumbra = 0.5;
  65. light2.castShadow = true;
  66. light2.shadow.mapSize.width = 1024;
  67. light2.shadow.mapSize.height = 1024;
  68. // scene.add( new THREE.CameraHelper( light2.shadow.camera ) );
  69. scene.add( light2 );
  70. // GROUND
  71. const gt = new THREE.TextureLoader().load( 'textures/terrain/grasslight-big.jpg' );
  72. const gg = new THREE.PlaneGeometry( 20, 20 );
  73. const gm = new THREE.MeshPhongMaterial( { color: 0xffffff, map: gt } );
  74. const ground = new THREE.Mesh( gg, gm );
  75. ground.rotation.x = - Math.PI / 2;
  76. ground.material.map.repeat.set( 8, 8 );
  77. ground.material.map.wrapS = ground.material.map.wrapT = THREE.RepeatWrapping;
  78. ground.material.map.colorSpace = THREE.SRGBColorSpace;
  79. ground.receiveShadow = true;
  80. scene.add( ground );
  81. // RENDERER
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  85. renderer.setAnimationLoop( animate );
  86. container.appendChild( renderer.domElement );
  87. //
  88. renderer.shadowMap.enabled = true;
  89. // STATS
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. // EVENTS
  93. window.addEventListener( 'resize', onWindowResize );
  94. // CONTROLS
  95. controls = new OrbitControls( camera, renderer.domElement );
  96. controls.target.set( 0, 0.5, 0 );
  97. controls.update();
  98. // GUI
  99. gui = new GUI();
  100. gui.add( playbackConfig, 'speed', 0, 2 ).onChange( function () {
  101. character.setPlaybackRate( playbackConfig.speed );
  102. } );
  103. gui.add( playbackConfig, 'wireframe' ).onChange( function () {
  104. character.setWireframe( playbackConfig.wireframe );
  105. } );
  106. // CHARACTER
  107. const config = {
  108. baseUrl: 'models/md2/ratamahatta/',
  109. body: 'ratamahatta.md2',
  110. skins: [ 'ratamahatta.png', 'ctf_b.png', 'ctf_r.png', 'dead.png', 'gearwhore.png' ],
  111. weapons: [[ 'weapon.md2', 'weapon.png' ],
  112. [ 'w_bfg.md2', 'w_bfg.png' ],
  113. [ 'w_blaster.md2', 'w_blaster.png' ],
  114. [ 'w_chaingun.md2', 'w_chaingun.png' ],
  115. [ 'w_glauncher.md2', 'w_glauncher.png' ],
  116. [ 'w_hyperblaster.md2', 'w_hyperblaster.png' ],
  117. [ 'w_machinegun.md2', 'w_machinegun.png' ],
  118. [ 'w_railgun.md2', 'w_railgun.png' ],
  119. [ 'w_rlauncher.md2', 'w_rlauncher.png' ],
  120. [ 'w_shotgun.md2', 'w_shotgun.png' ],
  121. [ 'w_sshotgun.md2', 'w_sshotgun.png' ]
  122. ]
  123. };
  124. character = new MD2Character();
  125. character.scale = 0.03;
  126. character.onLoadComplete = function () {
  127. setupSkinsGUI( character );
  128. setupWeaponsGUI( character );
  129. setupGUIAnimations( character );
  130. character.setAnimation( character.meshBody.geometry.animations[ 0 ].name );
  131. };
  132. character.loadParts( config );
  133. scene.add( character.root );
  134. }
  135. // EVENT HANDLERS
  136. function onWindowResize() {
  137. SCREEN_WIDTH = window.innerWidth;
  138. SCREEN_HEIGHT = window.innerHeight;
  139. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  140. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  141. camera.updateProjectionMatrix();
  142. }
  143. // GUI
  144. function labelize( text ) {
  145. const parts = text.split( '.' );
  146. if ( parts.length > 1 ) {
  147. parts.length -= 1;
  148. return parts.join( '.' );
  149. }
  150. return text;
  151. }
  152. //
  153. function setupWeaponsGUI( character ) {
  154. const folder = gui.addFolder( 'Weapons' );
  155. const generateCallback = function ( index ) {
  156. return function () {
  157. character.setWeapon( index );
  158. character.setWireframe( playbackConfig.wireframe );
  159. };
  160. };
  161. const guiItems = [];
  162. for ( let i = 0; i < character.weapons.length; i ++ ) {
  163. const name = character.weapons[ i ].name;
  164. playbackConfig[ name ] = generateCallback( i );
  165. guiItems[ i ] = folder.add( playbackConfig, name ).name( labelize( name ) );
  166. }
  167. }
  168. //
  169. function setupSkinsGUI( character ) {
  170. const folder = gui.addFolder( 'Skins' );
  171. const generateCallback = function ( index ) {
  172. return function () {
  173. character.setSkin( index );
  174. };
  175. };
  176. const guiItems = [];
  177. for ( let i = 0; i < character.skinsBody.length; i ++ ) {
  178. const name = character.skinsBody[ i ].name;
  179. playbackConfig[ name ] = generateCallback( i );
  180. guiItems[ i ] = folder.add( playbackConfig, name ).name( labelize( name ) );
  181. }
  182. }
  183. //
  184. function setupGUIAnimations( character ) {
  185. const folder = gui.addFolder( 'Animations' );
  186. const generateCallback = function ( animationClip ) {
  187. return function () {
  188. character.setAnimation( animationClip.name );
  189. };
  190. };
  191. const guiItems = [];
  192. const animations = character.meshBody.geometry.animations;
  193. for ( let i = 0; i < animations.length; i ++ ) {
  194. const clip = animations[ i ];
  195. playbackConfig[ clip.name ] = generateCallback( clip );
  196. guiItems[ i ] = folder.add( playbackConfig, clip.name, clip.name );
  197. }
  198. }
  199. //
  200. function animate() {
  201. render();
  202. stats.update();
  203. }
  204. function render() {
  205. const delta = clock.getDelta();
  206. character.update( delta );
  207. renderer.render( scene, camera );
  208. }
  209. </script>
  210. </body>
  211. </html>