2
0

MD2Character.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. // the animation system requires unique names, so append the
  50. // uuid of the source geometry:
  51. var geometry = mesh.geometry,
  52. animations = geometry.animations;
  53. for ( var i = 0, n = animations.length; i !== n; ++ i ) {
  54. var animation = animations[ i ];
  55. animation.name += geometry.uuid;
  56. }
  57. checkLoadingComplete();
  58. }
  59. };
  60. for ( var i = 0; i < config.weapons.length; i ++ ) {
  61. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  62. }
  63. };
  64. this.setPlaybackRate = function ( rate ) {
  65. if( rate !== 0 ) {
  66. this.mixer.timeScale = 1 / rate;
  67. }
  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 clip = THREE.AnimationClip.findByName( this.meshBody.geometry.animations, clipName );
  102. if( clip ) {
  103. this.meshBody.activeAction =
  104. this.mixer.clipAction( clip, this.meshBody ).play();
  105. }
  106. }
  107. scope.activeClipName = clipName;
  108. scope.syncWeaponAnimation();
  109. };
  110. this.syncWeaponAnimation = function() {
  111. var clipName = scope.activeClipName;
  112. if ( scope.meshWeapon ) {
  113. if( this.meshWeapon.activeAction ) {
  114. this.meshWeapon.activeAction.stop();
  115. this.meshWeapon.activeAction = null;
  116. }
  117. var geometry = this.meshWeapon.geometry,
  118. animations = geometry.animations;
  119. var clip = THREE.AnimationClip.findByName( animations, clipName + geometry.uuid );
  120. if( clip ) {
  121. this.meshWeapon.activeAction =
  122. this.mixer.clipAction( clip, this.meshWeapon ).
  123. syncWith( this.meshBody.activeAction ).play();
  124. }
  125. }
  126. }
  127. this.update = function ( delta ) {
  128. if( this.mixer ) this.mixer.update( delta );
  129. };
  130. function loadTextures( baseUrl, textureUrls ) {
  131. var textureLoader = new THREE.TextureLoader();
  132. var textures = [];
  133. for ( var i = 0; i < textureUrls.length; i ++ ) {
  134. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  135. textures[ i ].mapping = THREE.UVMapping;
  136. textures[ i ].name = textureUrls[ i ];
  137. }
  138. return textures;
  139. }
  140. function createPart( geometry, skinMap ) {
  141. var materialWireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, morphTargets: true, morphNormals: true } );
  142. var materialTexture = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, map: skinMap, morphTargets: true, morphNormals: true } );
  143. //
  144. var mesh = new THREE.Mesh( geometry, materialTexture );
  145. mesh.rotation.y = - Math.PI / 2;
  146. mesh.castShadow = true;
  147. mesh.receiveShadow = true;
  148. //
  149. mesh.materialTexture = materialTexture;
  150. mesh.materialWireframe = materialWireframe;
  151. return mesh;
  152. }
  153. function checkLoadingComplete() {
  154. scope.loadCounter -= 1;
  155. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  156. }
  157. };