MD2CharacterComplex.js 12 KB

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