webgl_loader_md2.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { MD2Character } from 'three/addons/misc/MD2Character.js';
  30. let SCREEN_WIDTH = window.innerWidth;
  31. let SCREEN_HEIGHT = window.innerHeight;
  32. let container, camera, scene, renderer;
  33. let character;
  34. let gui;
  35. const playbackConfig = {
  36. speed: 1.0,
  37. wireframe: false
  38. };
  39. let controls;
  40. const clock = new THREE.Clock();
  41. let stats;
  42. init();
  43. animate();
  44. function init() {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. // CAMERA
  48. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
  49. camera.position.set( 0, 2, 4 );
  50. // SCENE
  51. scene = new THREE.Scene();
  52. scene.background = new THREE.Color( 0x050505 );
  53. scene.fog = new THREE.Fog( 0x050505, 2.5, 10 );
  54. // LIGHTS
  55. scene.add( new THREE.AmbientLight( 0x666666 ) );
  56. const light1 = new THREE.SpotLight( 0xffffff, 150 );
  57. light1.position.set( 2, 5, 10 );
  58. light1.angle = 0.5;
  59. light1.penumbra = 0.5;
  60. light1.castShadow = true;
  61. light1.shadow.mapSize.width = 1024;
  62. light1.shadow.mapSize.height = 1024;
  63. // scene.add( new THREE.CameraHelper( light1.shadow.camera ) );
  64. scene.add( light1 );
  65. const light2 = new THREE.SpotLight( 0xffffff, 150 );
  66. light2.position.set( - 1, 3.5, 3.5 );
  67. light2.angle = 0.5;
  68. light2.penumbra = 0.5;
  69. light2.castShadow = true;
  70. light2.shadow.mapSize.width = 1024;
  71. light2.shadow.mapSize.height = 1024;
  72. // scene.add( new THREE.CameraHelper( light2.shadow.camera ) );
  73. scene.add( light2 );
  74. // GROUND
  75. const gt = new THREE.TextureLoader().load( 'textures/terrain/grasslight-big.jpg' );
  76. const gg = new THREE.PlaneGeometry( 20, 20 );
  77. const gm = new THREE.MeshPhongMaterial( { color: 0xffffff, map: gt } );
  78. const ground = new THREE.Mesh( gg, gm );
  79. ground.rotation.x = - Math.PI / 2;
  80. ground.material.map.repeat.set( 8, 8 );
  81. ground.material.map.wrapS = ground.material.map.wrapT = THREE.RepeatWrapping;
  82. ground.material.map.colorSpace = THREE.SRGBColorSpace;
  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.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, 0.5, 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 = 0.03;
  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. character.setWireframe( playbackConfig.wireframe );
  162. };
  163. };
  164. const guiItems = [];
  165. for ( let i = 0; i < character.weapons.length; i ++ ) {
  166. const name = character.weapons[ i ].name;
  167. playbackConfig[ name ] = generateCallback( i );
  168. guiItems[ i ] = folder.add( playbackConfig, name ).name( labelize( name ) );
  169. }
  170. }
  171. //
  172. function setupSkinsGUI( character ) {
  173. const folder = gui.addFolder( 'Skins' );
  174. const generateCallback = function ( index ) {
  175. return function () {
  176. character.setSkin( index );
  177. };
  178. };
  179. const guiItems = [];
  180. for ( let i = 0; i < character.skinsBody.length; i ++ ) {
  181. const name = character.skinsBody[ i ].name;
  182. playbackConfig[ name ] = generateCallback( i );
  183. guiItems[ i ] = folder.add( playbackConfig, name ).name( labelize( name ) );
  184. }
  185. }
  186. //
  187. function setupGUIAnimations( character ) {
  188. const folder = gui.addFolder( 'Animations' );
  189. const generateCallback = function ( animationClip ) {
  190. return function () {
  191. character.setAnimation( animationClip.name );
  192. };
  193. };
  194. const guiItems = [];
  195. const animations = character.meshBody.geometry.animations;
  196. for ( let i = 0; i < animations.length; i ++ ) {
  197. const clip = animations[ i ];
  198. playbackConfig[ clip.name ] = generateCallback( clip );
  199. guiItems[ i ] = folder.add( playbackConfig, clip.name, clip.name );
  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>