MD2Character.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ( function () {
  2. class MD2Character {
  3. constructor() {
  4. this.scale = 1;
  5. this.animationFPS = 6;
  6. this.root = new THREE.Object3D();
  7. this.meshBody = null;
  8. this.meshWeapon = null;
  9. this.skinsBody = [];
  10. this.skinsWeapon = [];
  11. this.weapons = [];
  12. this.activeAnimation = null;
  13. this.mixer = null;
  14. this.onLoadComplete = function () {};
  15. this.loadCounter = 0;
  16. }
  17. loadParts( config ) {
  18. const scope = this;
  19. function createPart( geometry, skinMap ) {
  20. const materialWireframe = new THREE.MeshLambertMaterial( {
  21. color: 0xffaa00,
  22. wireframe: true
  23. } );
  24. const materialTexture = new THREE.MeshLambertMaterial( {
  25. color: 0xffffff,
  26. wireframe: false,
  27. map: skinMap
  28. } ); //
  29. const mesh = new THREE.Mesh( geometry, materialTexture );
  30. mesh.rotation.y = - Math.PI / 2;
  31. mesh.castShadow = true;
  32. mesh.receiveShadow = true; //
  33. mesh.materialTexture = materialTexture;
  34. mesh.materialWireframe = materialWireframe;
  35. return mesh;
  36. }
  37. function loadTextures( baseUrl, textureUrls ) {
  38. const textureLoader = new THREE.TextureLoader();
  39. const textures = [];
  40. for ( let i = 0; i < textureUrls.length; i ++ ) {
  41. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  42. textures[ i ].mapping = THREE.UVMapping;
  43. textures[ i ].name = textureUrls[ i ];
  44. textures[ i ].encoding = THREE.sRGBEncoding;
  45. }
  46. return textures;
  47. }
  48. function checkLoadingComplete() {
  49. scope.loadCounter -= 1;
  50. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  51. }
  52. this.loadCounter = config.weapons.length * 2 + config.skins.length + 1;
  53. const weaponsTextures = [];
  54. for ( let i = 0; i < config.weapons.length; i ++ ) weaponsTextures[ i ] = config.weapons[ i ][ 1 ]; // SKINS
  55. this.skinsBody = loadTextures( config.baseUrl + 'skins/', config.skins );
  56. this.skinsWeapon = loadTextures( config.baseUrl + 'skins/', weaponsTextures ); // BODY
  57. const loader = new THREE.MD2Loader();
  58. loader.load( config.baseUrl + config.body, function ( geo ) {
  59. const boundingBox = new THREE.Box3();
  60. boundingBox.setFromBufferAttribute( geo.attributes.position );
  61. scope.root.position.y = - scope.scale * boundingBox.min.y;
  62. const mesh = createPart( geo, scope.skinsBody[ 0 ] );
  63. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  64. scope.root.add( mesh );
  65. scope.meshBody = mesh;
  66. scope.meshBody.clipOffset = 0;
  67. scope.activeAnimationClipName = mesh.geometry.animations[ 0 ].name;
  68. scope.mixer = new THREE.AnimationMixer( mesh );
  69. checkLoadingComplete();
  70. } ); // WEAPONS
  71. const generateCallback = function ( index, name ) {
  72. return function ( geo ) {
  73. const mesh = createPart( geo, scope.skinsWeapon[ index ] );
  74. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  75. mesh.visible = false;
  76. mesh.name = name;
  77. scope.root.add( mesh );
  78. scope.weapons[ index ] = mesh;
  79. scope.meshWeapon = mesh;
  80. checkLoadingComplete();
  81. };
  82. };
  83. for ( let i = 0; i < config.weapons.length; i ++ ) {
  84. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  85. }
  86. }
  87. setPlaybackRate( rate ) {
  88. if ( rate !== 0 ) {
  89. this.mixer.timeScale = 1 / rate;
  90. } else {
  91. this.mixer.timeScale = 0;
  92. }
  93. }
  94. setWireframe( wireframeEnabled ) {
  95. if ( wireframeEnabled ) {
  96. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  97. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  98. } else {
  99. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  100. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  101. }
  102. }
  103. setSkin( index ) {
  104. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  105. this.meshBody.material.map = this.skinsBody[ index ];
  106. }
  107. }
  108. setWeapon( index ) {
  109. for ( let i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  110. const activeWeapon = this.weapons[ index ];
  111. if ( activeWeapon ) {
  112. activeWeapon.visible = true;
  113. this.meshWeapon = activeWeapon;
  114. this.syncWeaponAnimation();
  115. }
  116. }
  117. setAnimation( clipName ) {
  118. if ( this.meshBody ) {
  119. if ( this.meshBody.activeAction ) {
  120. this.meshBody.activeAction.stop();
  121. this.meshBody.activeAction = null;
  122. }
  123. const action = this.mixer.clipAction( clipName, this.meshBody );
  124. if ( action ) {
  125. this.meshBody.activeAction = action.play();
  126. }
  127. }
  128. this.activeClipName = clipName;
  129. this.syncWeaponAnimation();
  130. }
  131. syncWeaponAnimation() {
  132. const clipName = this.activeClipName;
  133. if ( this.meshWeapon ) {
  134. if ( this.meshWeapon.activeAction ) {
  135. this.meshWeapon.activeAction.stop();
  136. this.meshWeapon.activeAction = null;
  137. }
  138. const action = this.mixer.clipAction( clipName, this.meshWeapon );
  139. if ( action ) {
  140. this.meshWeapon.activeAction = action.syncWith( this.meshBody.activeAction ).play();
  141. }
  142. }
  143. }
  144. update( delta ) {
  145. if ( this.mixer ) this.mixer.update( delta );
  146. }
  147. }
  148. THREE.MD2Character = MD2Character;
  149. } )();