MD2Character.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. }
  60. else {
  61. this.mixer.timeScale = 0;
  62. }
  63. };
  64. this.setWireframe = function ( wireframeEnabled ) {
  65. if ( wireframeEnabled ) {
  66. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  67. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  68. } else {
  69. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  70. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  71. }
  72. };
  73. this.setSkin = function( index ) {
  74. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  75. this.meshBody.material.map = this.skinsBody[ index ];
  76. }
  77. };
  78. this.setWeapon = function ( index ) {
  79. for ( var i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  80. var activeWeapon = this.weapons[ index ];
  81. if ( activeWeapon ) {
  82. activeWeapon.visible = true;
  83. this.meshWeapon = activeWeapon;
  84. scope.syncWeaponAnimation();
  85. }
  86. };
  87. this.setAnimation = function ( clipName ) {
  88. if ( this.meshBody ) {
  89. if( this.meshBody.activeAction ) {
  90. this.meshBody.activeAction.stop();
  91. this.meshBody.activeAction = null;
  92. }
  93. var action = this.mixer.clipAction( clipName, this.meshBody );
  94. if( action ) {
  95. this.meshBody.activeAction = action.play();
  96. }
  97. }
  98. scope.activeClipName = clipName;
  99. scope.syncWeaponAnimation();
  100. };
  101. this.syncWeaponAnimation = function() {
  102. var clipName = scope.activeClipName;
  103. if ( scope.meshWeapon ) {
  104. if( this.meshWeapon.activeAction ) {
  105. this.meshWeapon.activeAction.stop();
  106. this.meshWeapon.activeAction = null;
  107. }
  108. var geometry = this.meshWeapon.geometry,
  109. animations = geometry.animations;
  110. var action = this.mixer.clipAction( clipName, this.meshWeapon );
  111. if( action ) {
  112. this.meshWeapon.activeAction =
  113. action.syncWith( this.meshBody.activeAction ).play();
  114. }
  115. }
  116. }
  117. this.update = function ( delta ) {
  118. if( this.mixer ) this.mixer.update( delta );
  119. };
  120. function loadTextures( baseUrl, textureUrls ) {
  121. var textureLoader = new THREE.TextureLoader();
  122. var textures = [];
  123. for ( var i = 0; i < textureUrls.length; i ++ ) {
  124. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  125. textures[ i ].mapping = THREE.UVMapping;
  126. textures[ i ].name = textureUrls[ i ];
  127. }
  128. return textures;
  129. }
  130. function createPart( geometry, skinMap ) {
  131. var materialWireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, morphTargets: true, morphNormals: true } );
  132. var materialTexture = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, map: skinMap, morphTargets: true, morphNormals: true } );
  133. //
  134. var mesh = new THREE.Mesh( geometry, materialTexture );
  135. mesh.rotation.y = - Math.PI / 2;
  136. mesh.castShadow = true;
  137. mesh.receiveShadow = true;
  138. //
  139. mesh.materialTexture = materialTexture;
  140. mesh.materialWireframe = materialWireframe;
  141. return mesh;
  142. }
  143. function checkLoadingComplete() {
  144. scope.loadCounter -= 1;
  145. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  146. }
  147. };