webgl_loader_md2.html 7.3 KB

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