MD2CharacterComplex.js 12 KB

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