MD2Character.js 5.1 KB

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