webgl_loader_md2.html 7.3 KB

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