MD2Character.js 5.3 KB

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