webgl_loader_md2.html 7.6 KB

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