MD2CharacterComplex.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. console.warn( "THREE.MD2CharacterComplex: 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.MD2CharacterComplex = function () {
  3. var scope = this;
  4. this.scale = 1;
  5. // animation parameters
  6. this.animationFPS = 6;
  7. this.transitionFrames = 15;
  8. // movement model parameters
  9. this.maxSpeed = 275;
  10. this.maxReverseSpeed = - 275;
  11. this.frontAcceleration = 600;
  12. this.backAcceleration = 600;
  13. this.frontDecceleration = 600;
  14. this.angularSpeed = 2.5;
  15. // rig
  16. this.root = new THREE.Object3D();
  17. this.meshBody = null;
  18. this.meshWeapon = null;
  19. this.controls = null;
  20. // skins
  21. this.skinsBody = [];
  22. this.skinsWeapon = [];
  23. this.weapons = [];
  24. this.currentSkin = undefined;
  25. //
  26. this.onLoadComplete = function () {};
  27. // internals
  28. this.meshes = [];
  29. this.animations = {};
  30. this.loadCounter = 0;
  31. // internal movement control variables
  32. this.speed = 0;
  33. this.bodyOrientation = 0;
  34. this.walkSpeed = this.maxSpeed;
  35. this.crouchSpeed = this.maxSpeed * 0.5;
  36. // internal animation parameters
  37. this.activeAnimation = null;
  38. this.oldAnimation = null;
  39. // API
  40. this.enableShadows = function ( enable ) {
  41. for ( var i = 0; i < this.meshes.length; i ++ ) {
  42. this.meshes[ i ].castShadow = enable;
  43. this.meshes[ i ].receiveShadow = enable;
  44. }
  45. };
  46. this.setVisible = function ( enable ) {
  47. for ( var i = 0; i < this.meshes.length; i ++ ) {
  48. this.meshes[ i ].visible = enable;
  49. this.meshes[ i ].visible = enable;
  50. }
  51. };
  52. this.shareParts = function ( original ) {
  53. this.animations = original.animations;
  54. this.walkSpeed = original.walkSpeed;
  55. this.crouchSpeed = original.crouchSpeed;
  56. this.skinsBody = original.skinsBody;
  57. this.skinsWeapon = original.skinsWeapon;
  58. // BODY
  59. var mesh = createPart( original.meshBody.geometry, this.skinsBody[ 0 ] );
  60. mesh.scale.set( this.scale, this.scale, this.scale );
  61. this.root.position.y = original.root.position.y;
  62. this.root.add( mesh );
  63. this.meshBody = mesh;
  64. this.meshes.push( mesh );
  65. // WEAPONS
  66. for ( var i = 0; i < original.weapons.length; i ++ ) {
  67. var meshWeapon = createPart( original.weapons[ i ].geometry, this.skinsWeapon[ i ] );
  68. meshWeapon.scale.set( this.scale, this.scale, this.scale );
  69. meshWeapon.visible = false;
  70. meshWeapon.name = original.weapons[ i ].name;
  71. this.root.add( meshWeapon );
  72. this.weapons[ i ] = meshWeapon;
  73. this.meshWeapon = meshWeapon;
  74. this.meshes.push( meshWeapon );
  75. }
  76. };
  77. this.loadParts = function ( config ) {
  78. this.animations = config.animations;
  79. this.walkSpeed = config.walkSpeed;
  80. this.crouchSpeed = config.crouchSpeed;
  81. this.loadCounter = config.weapons.length * 2 + config.skins.length + 1;
  82. var weaponsTextures = [];
  83. for ( var i = 0; i < config.weapons.length; i ++ ) weaponsTextures[ i ] = config.weapons[ i ][ 1 ];
  84. // SKINS
  85. this.skinsBody = loadTextures( config.baseUrl + "skins/", config.skins );
  86. this.skinsWeapon = loadTextures( config.baseUrl + "skins/", weaponsTextures );
  87. // BODY
  88. var loader = new THREE.MD2Loader();
  89. loader.load( config.baseUrl + config.body, function ( geo ) {
  90. var boundingBox = new THREE.Box3();
  91. boundingBox.setFromBufferAttribute( geo.attributes.position );
  92. scope.root.position.y = - scope.scale * boundingBox.min.y;
  93. var mesh = createPart( geo, scope.skinsBody[ 0 ] );
  94. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  95. scope.root.add( mesh );
  96. scope.meshBody = mesh;
  97. scope.meshes.push( mesh );
  98. checkLoadingComplete();
  99. } );
  100. // WEAPONS
  101. var generateCallback = function ( index, name ) {
  102. return function ( geo ) {
  103. var mesh = createPart( geo, scope.skinsWeapon[ index ] );
  104. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  105. mesh.visible = false;
  106. mesh.name = name;
  107. scope.root.add( mesh );
  108. scope.weapons[ index ] = mesh;
  109. scope.meshWeapon = mesh;
  110. scope.meshes.push( mesh );
  111. checkLoadingComplete();
  112. };
  113. };
  114. for ( var i = 0; i < config.weapons.length; i ++ ) {
  115. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  116. }
  117. };
  118. this.setPlaybackRate = function ( rate ) {
  119. if ( this.meshBody ) this.meshBody.duration = this.meshBody.baseDuration / rate;
  120. if ( this.meshWeapon ) this.meshWeapon.duration = this.meshWeapon.baseDuration / rate;
  121. };
  122. this.setWireframe = function ( wireframeEnabled ) {
  123. if ( wireframeEnabled ) {
  124. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  125. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  126. } else {
  127. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  128. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  129. }
  130. };
  131. this.setSkin = function ( index ) {
  132. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  133. this.meshBody.material.map = this.skinsBody[ index ];
  134. this.currentSkin = index;
  135. }
  136. };
  137. this.setWeapon = function ( index ) {
  138. for ( var i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  139. var activeWeapon = this.weapons[ index ];
  140. if ( activeWeapon ) {
  141. activeWeapon.visible = true;
  142. this.meshWeapon = activeWeapon;
  143. if ( this.activeAnimation ) {
  144. activeWeapon.playAnimation( this.activeAnimation );
  145. this.meshWeapon.setAnimationTime( this.activeAnimation, this.meshBody.getAnimationTime( this.activeAnimation ) );
  146. }
  147. }
  148. };
  149. this.setAnimation = function ( animationName ) {
  150. if ( animationName === this.activeAnimation || ! animationName ) return;
  151. if ( this.meshBody ) {
  152. this.meshBody.setAnimationWeight( animationName, 0 );
  153. this.meshBody.playAnimation( animationName );
  154. this.oldAnimation = this.activeAnimation;
  155. this.activeAnimation = animationName;
  156. this.blendCounter = this.transitionFrames;
  157. }
  158. if ( this.meshWeapon ) {
  159. this.meshWeapon.setAnimationWeight( animationName, 0 );
  160. this.meshWeapon.playAnimation( animationName );
  161. }
  162. };
  163. this.update = function ( delta ) {
  164. if ( this.controls ) this.updateMovementModel( delta );
  165. if ( this.animations ) {
  166. this.updateBehaviors();
  167. this.updateAnimations( delta );
  168. }
  169. };
  170. this.updateAnimations = function ( delta ) {
  171. var mix = 1;
  172. if ( this.blendCounter > 0 ) {
  173. mix = ( this.transitionFrames - this.blendCounter ) / this.transitionFrames;
  174. this.blendCounter -= 1;
  175. }
  176. if ( this.meshBody ) {
  177. this.meshBody.update( delta );
  178. this.meshBody.setAnimationWeight( this.activeAnimation, mix );
  179. this.meshBody.setAnimationWeight( this.oldAnimation, 1 - mix );
  180. }
  181. if ( this.meshWeapon ) {
  182. this.meshWeapon.update( delta );
  183. this.meshWeapon.setAnimationWeight( this.activeAnimation, mix );
  184. this.meshWeapon.setAnimationWeight( this.oldAnimation, 1 - mix );
  185. }
  186. };
  187. this.updateBehaviors = function () {
  188. var controls = this.controls;
  189. var animations = this.animations;
  190. var moveAnimation, idleAnimation;
  191. // crouch vs stand
  192. if ( controls.crouch ) {
  193. moveAnimation = animations[ "crouchMove" ];
  194. idleAnimation = animations[ "crouchIdle" ];
  195. } else {
  196. moveAnimation = animations[ "move" ];
  197. idleAnimation = animations[ "idle" ];
  198. }
  199. // actions
  200. if ( controls.jump ) {
  201. moveAnimation = animations[ "jump" ];
  202. idleAnimation = animations[ "jump" ];
  203. }
  204. if ( controls.attack ) {
  205. if ( controls.crouch ) {
  206. moveAnimation = animations[ "crouchAttack" ];
  207. idleAnimation = animations[ "crouchAttack" ];
  208. } else {
  209. moveAnimation = animations[ "attack" ];
  210. idleAnimation = animations[ "attack" ];
  211. }
  212. }
  213. // set animations
  214. if ( controls.moveForward || controls.moveBackward || controls.moveLeft || controls.moveRight ) {
  215. if ( this.activeAnimation !== moveAnimation ) {
  216. this.setAnimation( moveAnimation );
  217. }
  218. }
  219. if ( Math.abs( this.speed ) < 0.2 * this.maxSpeed && ! ( controls.moveLeft || controls.moveRight || controls.moveForward || controls.moveBackward ) ) {
  220. if ( this.activeAnimation !== idleAnimation ) {
  221. this.setAnimation( idleAnimation );
  222. }
  223. }
  224. // set animation direction
  225. if ( controls.moveForward ) {
  226. if ( this.meshBody ) {
  227. this.meshBody.setAnimationDirectionForward( this.activeAnimation );
  228. this.meshBody.setAnimationDirectionForward( this.oldAnimation );
  229. }
  230. if ( this.meshWeapon ) {
  231. this.meshWeapon.setAnimationDirectionForward( this.activeAnimation );
  232. this.meshWeapon.setAnimationDirectionForward( this.oldAnimation );
  233. }
  234. }
  235. if ( controls.moveBackward ) {
  236. if ( this.meshBody ) {
  237. this.meshBody.setAnimationDirectionBackward( this.activeAnimation );
  238. this.meshBody.setAnimationDirectionBackward( this.oldAnimation );
  239. }
  240. if ( this.meshWeapon ) {
  241. this.meshWeapon.setAnimationDirectionBackward( this.activeAnimation );
  242. this.meshWeapon.setAnimationDirectionBackward( this.oldAnimation );
  243. }
  244. }
  245. };
  246. this.updateMovementModel = function ( delta ) {
  247. var controls = this.controls;
  248. // speed based on controls
  249. if ( controls.crouch ) this.maxSpeed = this.crouchSpeed;
  250. else this.maxSpeed = this.walkSpeed;
  251. this.maxReverseSpeed = - this.maxSpeed;
  252. if ( controls.moveForward ) this.speed = THREE.MathUtils.clamp( this.speed + delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  253. if ( controls.moveBackward ) this.speed = THREE.MathUtils.clamp( this.speed - delta * this.backAcceleration, this.maxReverseSpeed, this.maxSpeed );
  254. // orientation based on controls
  255. // (don't just stand while turning)
  256. var dir = 1;
  257. if ( controls.moveLeft ) {
  258. this.bodyOrientation += delta * this.angularSpeed;
  259. this.speed = THREE.MathUtils.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  260. }
  261. if ( controls.moveRight ) {
  262. this.bodyOrientation -= delta * this.angularSpeed;
  263. this.speed = THREE.MathUtils.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  264. }
  265. // speed decay
  266. if ( ! ( controls.moveForward || controls.moveBackward ) ) {
  267. if ( this.speed > 0 ) {
  268. var k = exponentialEaseOut( this.speed / this.maxSpeed );
  269. this.speed = THREE.MathUtils.clamp( this.speed - k * delta * this.frontDecceleration, 0, this.maxSpeed );
  270. } else {
  271. var k = exponentialEaseOut( this.speed / this.maxReverseSpeed );
  272. this.speed = THREE.MathUtils.clamp( this.speed + k * delta * this.backAcceleration, this.maxReverseSpeed, 0 );
  273. }
  274. }
  275. // displacement
  276. var forwardDelta = this.speed * delta;
  277. this.root.position.x += Math.sin( this.bodyOrientation ) * forwardDelta;
  278. this.root.position.z += Math.cos( this.bodyOrientation ) * forwardDelta;
  279. // steering
  280. this.root.rotation.y = this.bodyOrientation;
  281. };
  282. // internal helpers
  283. function loadTextures( baseUrl, textureUrls ) {
  284. var textureLoader = new THREE.TextureLoader();
  285. var textures = [];
  286. for ( var i = 0; i < textureUrls.length; i ++ ) {
  287. textures[ i ] = textureLoader.load( baseUrl + textureUrls[ i ], checkLoadingComplete );
  288. textures[ i ].mapping = THREE.UVMapping;
  289. textures[ i ].name = textureUrls[ i ];
  290. textures[ i ].encoding = THREE.sRGBEncoding;
  291. }
  292. return textures;
  293. }
  294. function createPart( geometry, skinMap ) {
  295. var materialWireframe = new THREE.MeshLambertMaterial( { color: 0xffaa00, wireframe: true, morphTargets: true, morphNormals: true } );
  296. var materialTexture = new THREE.MeshLambertMaterial( { color: 0xffffff, wireframe: false, map: skinMap, morphTargets: true, morphNormals: true } );
  297. //
  298. var mesh = new THREE.MorphBlendMesh( geometry, materialTexture );
  299. mesh.rotation.y = - Math.PI / 2;
  300. //
  301. mesh.materialTexture = materialTexture;
  302. mesh.materialWireframe = materialWireframe;
  303. //
  304. mesh.autoCreateAnimations( scope.animationFPS );
  305. return mesh;
  306. }
  307. function checkLoadingComplete() {
  308. scope.loadCounter -= 1;
  309. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  310. }
  311. function exponentialEaseOut( k ) {
  312. return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1;
  313. }
  314. };