MD2Character.js 5.3 KB

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