MD2Character.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. var boundingBox = new THREE.Box3();
  29. boundingBox.setFromBufferAttribute( geo.attributes.position );
  30. scope.root.position.y = - scope.scale * boundingBox.min.y;
  31. var mesh = createPart( geo, scope.skinsBody[ 0 ] );
  32. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  33. scope.root.add( mesh );
  34. scope.meshBody = mesh;
  35. scope.meshBody.clipOffset = 0;
  36. scope.activeAnimationClipName = mesh.geometry.animations[ 0 ].name;
  37. scope.mixer = new THREE.AnimationMixer( mesh );
  38. checkLoadingComplete();
  39. } );
  40. // WEAPONS
  41. var generateCallback = function ( index, name ) {
  42. return function ( geo ) {
  43. var mesh = createPart( geo, scope.skinsWeapon[ index ] );
  44. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  45. mesh.visible = false;
  46. mesh.name = name;
  47. scope.root.add( mesh );
  48. scope.weapons[ index ] = mesh;
  49. scope.meshWeapon = mesh;
  50. checkLoadingComplete();
  51. };
  52. };
  53. for ( var i = 0; i < config.weapons.length; i ++ ) {
  54. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  55. }
  56. };
  57. this.setPlaybackRate = function ( rate ) {
  58. if ( rate !== 0 ) {
  59. this.mixer.timeScale = 1 / rate;
  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 action = this.mixer.clipAction( clipName, this.meshWeapon );
  109. if ( action ) {
  110. this.meshWeapon.activeAction = action.syncWith( this.meshBody.activeAction ).play();
  111. }
  112. }
  113. };
  114. this.update = function ( delta ) {
  115. if ( this.mixer ) this.mixer.update( delta );
  116. };
  117. function loadTextures( baseUrl, textureUrls ) {
  118. var textureLoader = new THREE.TextureLoader();
  119. var textures = [];
  120. for ( var i = 0; i < textureUrls.length; i ++ ) {
  121. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  122. textures[ i ].mapping = THREE.UVMapping;
  123. textures[ i ].name = textureUrls[ i ];
  124. textures[ i ].encoding = THREE.sRGBEncoding;
  125. }
  126. return textures;
  127. }
  128. function createPart( geometry, skinMap ) {
  129. var materialWireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, morphTargets: true, morphNormals: true } );
  130. var materialTexture = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, map: skinMap, morphTargets: true, morphNormals: true } );
  131. //
  132. var mesh = new THREE.Mesh( geometry, materialTexture );
  133. mesh.rotation.y = - Math.PI / 2;
  134. mesh.castShadow = true;
  135. mesh.receiveShadow = true;
  136. //
  137. mesh.materialTexture = materialTexture;
  138. mesh.materialWireframe = materialWireframe;
  139. return mesh;
  140. }
  141. function checkLoadingComplete() {
  142. scope.loadCounter -= 1;
  143. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  144. }
  145. };