MD2Character.js 5.3 KB

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