MD2Character.js 5.0 KB

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