webgl_loader_md2.html 7.2 KB

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