MD2Character.js 5.1 KB

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