webgl_loader_md2.html 7.4 KB

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