MD2CharacterComplex.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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 mesh = createPart( original.weapons[ i ].geometry, this.skinsWeapon[ i ] );
  70. mesh.scale.set( this.scale, this.scale, this.scale );
  71. mesh.visible = false;
  72. mesh.name = name;
  73. this.root.add( mesh );
  74. this.weapons[ i ] = mesh;
  75. this.meshWeapon = mesh;
  76. this.meshes.push( mesh );
  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.JSONLoader();
  91. loader.load( config.baseUrl + config.body, function( geo ) {
  92. geo.computeBoundingBox();
  93. scope.root.position.y = - scope.scale * geo.boundingBox.min.y;
  94. var mesh = createPart( geo, scope.skinsBody[ 0 ] );
  95. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  96. scope.root.add( mesh );
  97. scope.meshBody = mesh;
  98. scope.meshes.push( mesh );
  99. checkLoadingComplete();
  100. } );
  101. // WEAPONS
  102. var generateCallback = function ( index, name ) {
  103. return function( geo ) {
  104. var mesh = createPart( geo, scope.skinsWeapon[ index ] );
  105. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  106. mesh.visible = false;
  107. mesh.name = name;
  108. scope.root.add( mesh );
  109. scope.weapons[ index ] = mesh;
  110. scope.meshWeapon = mesh;
  111. scope.meshes.push( mesh );
  112. checkLoadingComplete();
  113. }
  114. }
  115. for ( var i = 0; i < config.weapons.length; i ++ ) {
  116. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  117. }
  118. };
  119. this.setPlaybackRate = function ( rate ) {
  120. if ( this.meshBody ) this.meshBody.duration = this.meshBody.baseDuration / rate;
  121. if ( this.meshWeapon ) this.meshWeapon.duration = this.meshWeapon.baseDuration / rate;
  122. };
  123. this.setWireframe = function ( wireframeEnabled ) {
  124. if ( wireframeEnabled ) {
  125. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  126. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  127. } else {
  128. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  129. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  130. }
  131. };
  132. this.setSkin = function( index ) {
  133. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  134. this.meshBody.material.map = this.skinsBody[ index ];
  135. this.currentSkin = index;
  136. }
  137. };
  138. this.setWeapon = function ( index ) {
  139. for ( var i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  140. var activeWeapon = this.weapons[ index ];
  141. if ( activeWeapon ) {
  142. activeWeapon.visible = true;
  143. this.meshWeapon = activeWeapon;
  144. activeWeapon.playAnimation( this.activeAnimation );
  145. this.meshWeapon.setAnimationTime( this.activeAnimation, this.meshBody.getAnimationTime( this.activeAnimation ) );
  146. }
  147. };
  148. this.setAnimation = function ( animationName ) {
  149. if ( animationName === this.activeAnimation ) return;
  150. if ( this.meshBody ) {
  151. this.meshBody.setAnimationWeight( animationName, 0 );
  152. this.meshBody.playAnimation( animationName );
  153. this.oldAnimation = this.activeAnimation;
  154. this.activeAnimation = animationName;
  155. this.blendCounter = this.transitionFrames;
  156. }
  157. if ( this.meshWeapon ) {
  158. this.meshWeapon.setAnimationWeight( animationName, 0 );
  159. this.meshWeapon.playAnimation( animationName );
  160. }
  161. };
  162. this.update = function ( delta ) {
  163. if ( this.controls ) this.updateMovementModel( delta );
  164. if ( this.animations ) {
  165. this.updateBehaviors( delta );
  166. this.updateAnimations( delta );
  167. }
  168. };
  169. this.updateAnimations = function ( delta ) {
  170. var mix = 1;
  171. if ( this.blendCounter > 0 ) {
  172. mix = ( this.transitionFrames - this.blendCounter ) / this.transitionFrames;
  173. this.blendCounter -= 1;
  174. }
  175. if ( this.meshBody ) {
  176. this.meshBody.update( delta );
  177. this.meshBody.setAnimationWeight( this.activeAnimation, mix );
  178. this.meshBody.setAnimationWeight( this.oldAnimation, 1 - mix );
  179. }
  180. if ( this.meshWeapon ) {
  181. this.meshWeapon.update( delta );
  182. this.meshWeapon.setAnimationWeight( this.activeAnimation, mix );
  183. this.meshWeapon.setAnimationWeight( this.oldAnimation, 1 - mix );
  184. }
  185. };
  186. this.updateBehaviors = function ( delta ) {
  187. var controls = this.controls;
  188. var animations = this.animations;
  189. var moveAnimation, idleAnimation;
  190. // crouch vs stand
  191. if ( controls.crouch ) {
  192. moveAnimation = animations[ "crouchMove" ];
  193. idleAnimation = animations[ "crouchIdle" ];
  194. } else {
  195. moveAnimation = animations[ "move" ];
  196. idleAnimation = animations[ "idle" ];
  197. }
  198. // actions
  199. if ( controls.jump ) {
  200. moveAnimation = animations[ "jump" ];
  201. idleAnimation = animations[ "jump" ];
  202. }
  203. if ( controls.attack ) {
  204. if ( controls.crouch ) {
  205. moveAnimation = animations[ "crouchAttack" ];
  206. idleAnimation = animations[ "crouchAttack" ];
  207. } else {
  208. moveAnimation = animations[ "attack" ];
  209. idleAnimation = animations[ "attack" ];
  210. }
  211. }
  212. // set animations
  213. if ( controls.moveForward || controls.moveBackward || controls.moveLeft || controls.moveRight ) {
  214. if ( this.activeAnimation !== moveAnimation ) {
  215. this.setAnimation( moveAnimation );
  216. }
  217. }
  218. if ( Math.abs( this.speed ) < 0.2 * this.maxSpeed && !( controls.moveLeft || controls.moveRight ) ) {
  219. if ( this.activeAnimation !== idleAnimation ) {
  220. this.setAnimation( idleAnimation );
  221. }
  222. }
  223. // set animation direction
  224. if ( controls.moveForward ) {
  225. if ( this.meshBody ) {
  226. this.meshBody.setAnimationDirectionForward( this.activeAnimation );
  227. this.meshBody.setAnimationDirectionForward( this.oldAnimation );
  228. }
  229. if ( this.meshWeapon ) {
  230. this.meshWeapon.setAnimationDirectionForward( this.activeAnimation );
  231. this.meshWeapon.setAnimationDirectionForward( this.oldAnimation );
  232. }
  233. }
  234. if ( controls.moveBackward ) {
  235. if ( this.meshBody ) {
  236. this.meshBody.setAnimationDirectionBackward( this.activeAnimation );
  237. this.meshBody.setAnimationDirectionBackward( this.oldAnimation );
  238. }
  239. if ( this.meshWeapon ) {
  240. this.meshWeapon.setAnimationDirectionBackward( this.activeAnimation );
  241. this.meshWeapon.setAnimationDirectionBackward( this.oldAnimation );
  242. }
  243. }
  244. };
  245. this.updateMovementModel = function ( delta ) {
  246. var controls = this.controls;
  247. // speed based on controls
  248. if ( controls.crouch ) this.maxSpeed = this.crouchSpeed;
  249. else this.maxSpeed = this.walkSpeed;
  250. this.maxReverseSpeed = -this.maxSpeed;
  251. if ( controls.moveForward ) this.speed = THREE.Math.clamp( this.speed + delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  252. if ( controls.moveBackward ) this.speed = THREE.Math.clamp( this.speed - delta * this.backAcceleration, this.maxReverseSpeed, this.maxSpeed );
  253. // orientation based on controls
  254. // (don't just stand while turning)
  255. var dir = 1;
  256. if ( controls.moveLeft ) {
  257. this.bodyOrientation += delta * this.angularSpeed;
  258. this.speed = THREE.Math.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  259. }
  260. if ( controls.moveRight ) {
  261. this.bodyOrientation -= delta * this.angularSpeed;
  262. this.speed = THREE.Math.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  263. }
  264. // speed decay
  265. if ( ! ( controls.moveForward || controls.moveBackward ) ) {
  266. if ( this.speed > 0 ) {
  267. var k = exponentialEaseOut( this.speed / this.maxSpeed );
  268. this.speed = THREE.Math.clamp( this.speed - k * delta * this.frontDecceleration, 0, this.maxSpeed );
  269. } else {
  270. var k = exponentialEaseOut( this.speed / this.maxReverseSpeed );
  271. this.speed = THREE.Math.clamp( this.speed + k * delta * this.backAcceleration, this.maxReverseSpeed, 0 );
  272. }
  273. }
  274. // displacement
  275. var forwardDelta = this.speed * delta;
  276. this.root.position.x += Math.sin( this.bodyOrientation ) * forwardDelta;
  277. this.root.position.z += Math.cos( this.bodyOrientation ) * forwardDelta;
  278. // steering
  279. this.root.rotation.y = this.bodyOrientation;
  280. };
  281. // internal helpers
  282. function loadTextures( baseUrl, textureUrls ) {
  283. var mapping = new THREE.UVMapping();
  284. var textures = [];
  285. for ( var i = 0; i < textureUrls.length; i ++ ) {
  286. textures[ i ] = THREE.ImageUtils.loadTexture( baseUrl + textureUrls[ i ], mapping, checkLoadingComplete );
  287. textures[ i ].name = textureUrls[ i ];
  288. }
  289. return textures;
  290. };
  291. function createPart( geometry, skinMap ) {
  292. geometry.computeMorphNormals();
  293. var whiteMap = THREE.ImageUtils.generateDataTexture( 1, 1, new THREE.Color( 0xffffff ) );
  294. var materialWireframe = new THREE.MeshPhongMaterial( { color: 0xffaa00, specular: 0x111111, shininess: 50, wireframe: true, shading: THREE.SmoothShading, map: whiteMap, morphTargets: true, morphNormals: true, perPixel: true, metal: true } );
  295. var materialTexture = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 50, wireframe: false, shading: THREE.SmoothShading, map: skinMap, morphTargets: true, morphNormals: true, perPixel: true, metal: true } );
  296. materialTexture.wrapAround = 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 ) { return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1; }
  312. };