2
0

MD2Character.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. console.warn( "THREE.MD2Character: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. THREE.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 ];
  20. // SKINS
  21. this.skinsBody = loadTextures( config.baseUrl + "skins/", config.skins );
  22. this.skinsWeapon = loadTextures( config.baseUrl + "skins/", weaponsTextures );
  23. // BODY
  24. var loader = new THREE.MD2Loader();
  25. loader.load( config.baseUrl + config.body, function ( geo ) {
  26. var boundingBox = new THREE.Box3();
  27. boundingBox.setFromBufferAttribute( geo.attributes.position );
  28. scope.root.position.y = - scope.scale * boundingBox.min.y;
  29. var mesh = createPart( geo, scope.skinsBody[ 0 ] );
  30. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  31. scope.root.add( mesh );
  32. scope.meshBody = mesh;
  33. scope.meshBody.clipOffset = 0;
  34. scope.activeAnimationClipName = mesh.geometry.animations[ 0 ].name;
  35. scope.mixer = new THREE.AnimationMixer( mesh );
  36. checkLoadingComplete();
  37. } );
  38. // WEAPONS
  39. var generateCallback = function ( index, name ) {
  40. return function ( geo ) {
  41. var mesh = createPart( geo, scope.skinsWeapon[ index ] );
  42. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  43. mesh.visible = false;
  44. mesh.name = name;
  45. scope.root.add( mesh );
  46. scope.weapons[ index ] = mesh;
  47. scope.meshWeapon = mesh;
  48. checkLoadingComplete();
  49. };
  50. };
  51. for ( var i = 0; i < config.weapons.length; i ++ ) {
  52. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  53. }
  54. };
  55. this.setPlaybackRate = function ( rate ) {
  56. if ( rate !== 0 ) {
  57. this.mixer.timeScale = 1 / rate;
  58. } else {
  59. this.mixer.timeScale = 0;
  60. }
  61. };
  62. this.setWireframe = function ( wireframeEnabled ) {
  63. if ( wireframeEnabled ) {
  64. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  65. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  66. } else {
  67. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  68. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  69. }
  70. };
  71. this.setSkin = function ( index ) {
  72. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  73. this.meshBody.material.map = this.skinsBody[ index ];
  74. }
  75. };
  76. this.setWeapon = function ( index ) {
  77. for ( var i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  78. var activeWeapon = this.weapons[ index ];
  79. if ( activeWeapon ) {
  80. activeWeapon.visible = true;
  81. this.meshWeapon = activeWeapon;
  82. scope.syncWeaponAnimation();
  83. }
  84. };
  85. this.setAnimation = function ( clipName ) {
  86. if ( this.meshBody ) {
  87. if ( this.meshBody.activeAction ) {
  88. this.meshBody.activeAction.stop();
  89. this.meshBody.activeAction = null;
  90. }
  91. var action = this.mixer.clipAction( clipName, this.meshBody );
  92. if ( action ) {
  93. this.meshBody.activeAction = action.play();
  94. }
  95. }
  96. scope.activeClipName = clipName;
  97. scope.syncWeaponAnimation();
  98. };
  99. this.syncWeaponAnimation = function () {
  100. var clipName = scope.activeClipName;
  101. if ( scope.meshWeapon ) {
  102. if ( this.meshWeapon.activeAction ) {
  103. this.meshWeapon.activeAction.stop();
  104. this.meshWeapon.activeAction = null;
  105. }
  106. var action = this.mixer.clipAction( clipName, this.meshWeapon );
  107. if ( action ) {
  108. this.meshWeapon.activeAction = action.syncWith( this.meshBody.activeAction ).play();
  109. }
  110. }
  111. };
  112. this.update = function ( delta ) {
  113. if ( this.mixer ) this.mixer.update( delta );
  114. };
  115. function loadTextures( baseUrl, textureUrls ) {
  116. var textureLoader = new THREE.TextureLoader();
  117. var textures = [];
  118. for ( var i = 0; i < textureUrls.length; i ++ ) {
  119. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  120. textures[ i ].mapping = THREE.UVMapping;
  121. textures[ i ].name = textureUrls[ i ];
  122. textures[ i ].encoding = THREE.sRGBEncoding;
  123. }
  124. return textures;
  125. }
  126. function createPart( geometry, skinMap ) {
  127. var materialWireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, morphTargets: true, morphNormals: true } );
  128. var materialTexture = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, map: skinMap, morphTargets: true, morphNormals: true } );
  129. //
  130. var mesh = new THREE.Mesh( geometry, materialTexture );
  131. mesh.rotation.y = - Math.PI / 2;
  132. mesh.castShadow = true;
  133. mesh.receiveShadow = true;
  134. //
  135. mesh.materialTexture = materialTexture;
  136. mesh.materialWireframe = materialWireframe;
  137. return mesh;
  138. }
  139. function checkLoadingComplete() {
  140. scope.loadCounter -= 1;
  141. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  142. }
  143. };