MD2CharacterComplex.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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.loadCounter = 0;
  31. // internal movement control variables
  32. this.speed = 0;
  33. this.bodyOrientation = 0;
  34. // internal animation parameters
  35. this.activeAnimation = null;
  36. this.oldAnimation = null;
  37. // API
  38. this.loadParts = function ( config ) {
  39. this.animations = config.animations;
  40. this.walkSpeed = config.walkSpeed;
  41. this.crouchSpeed = config.crouchSpeed;
  42. this.loadCounter = config.weapons.length * 2 + config.skins.length + 1;
  43. var weaponsTextures = []
  44. for ( var i = 0; i < config.weapons.length; i ++ ) weaponsTextures[ i ] = config.weapons[ i ][ 1 ];
  45. // SKINS
  46. this.skinsBody = loadTextures( config.baseUrl + "skins/", config.skins );
  47. this.skinsWeapon = loadTextures( config.baseUrl + "skins/", weaponsTextures );
  48. // BODY
  49. var loader = new THREE.JSONLoader();
  50. loader.load( config.baseUrl + config.body, function( geo ) {
  51. geo.computeBoundingBox();
  52. scope.root.position.y = - scope.scale * geo.boundingBox.min.y;
  53. var mesh = createPart( geo, scope.skinsBody[ 0 ] );
  54. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  55. scope.root.add( mesh );
  56. scope.meshBody = mesh;
  57. checkLoadingComplete();
  58. } );
  59. // WEAPONS
  60. var generateCallback = function ( index, name ) {
  61. return function( geo ) {
  62. var mesh = createPart( geo, scope.skinsWeapon[ index ] );
  63. mesh.scale.set( scope.scale, scope.scale, scope.scale );
  64. mesh.visible = false;
  65. mesh.name = name;
  66. scope.root.add( mesh );
  67. scope.weapons[ index ] = mesh;
  68. scope.meshWeapon = mesh;
  69. checkLoadingComplete();
  70. }
  71. }
  72. for ( var i = 0; i < config.weapons.length; i ++ ) {
  73. loader.load( config.baseUrl + config.weapons[ i ][ 0 ], generateCallback( i, config.weapons[ i ][ 0 ] ) );
  74. }
  75. };
  76. this.setPlaybackRate = function ( rate ) {
  77. if ( this.meshBody ) this.meshBody.duration = this.meshBody.baseDuration / rate;
  78. if ( this.meshWeapon ) this.meshWeapon.duration = this.meshWeapon.baseDuration / rate;
  79. };
  80. this.setWireframe = function ( wireframeEnabled ) {
  81. if ( wireframeEnabled ) {
  82. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialWireframe;
  83. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialWireframe;
  84. } else {
  85. if ( this.meshBody ) this.meshBody.material = this.meshBody.materialTexture;
  86. if ( this.meshWeapon ) this.meshWeapon.material = this.meshWeapon.materialTexture;
  87. }
  88. };
  89. this.setSkin = function( index ) {
  90. if ( this.meshBody && this.meshBody.material.wireframe === false ) {
  91. this.meshBody.material.map = this.skinsBody[ index ];
  92. this.currentSkin = index;
  93. }
  94. };
  95. this.setWeapon = function ( index ) {
  96. for ( var i = 0; i < this.weapons.length; i ++ ) this.weapons[ i ].visible = false;
  97. var activeWeapon = this.weapons[ index ];
  98. if ( activeWeapon ) {
  99. activeWeapon.visible = true;
  100. this.meshWeapon = activeWeapon;
  101. activeWeapon.playAnimation( this.activeAnimation );
  102. this.meshWeapon.setAnimationTime( this.activeAnimation, this.meshBody.getAnimationTime( this.activeAnimation ) );
  103. }
  104. };
  105. this.setAnimation = function ( animationName ) {
  106. if ( animationName === this.activeAnimation ) return;
  107. if ( this.meshBody ) {
  108. this.meshBody.setAnimationWeight( animationName, 0 );
  109. this.meshBody.playAnimation( animationName );
  110. this.oldAnimation = this.activeAnimation;
  111. this.activeAnimation = animationName;
  112. this.blendCounter = this.transitionFrames;
  113. }
  114. if ( this.meshWeapon ) {
  115. this.meshWeapon.setAnimationWeight( animationName, 0 );
  116. this.meshWeapon.playAnimation( animationName );
  117. }
  118. };
  119. this.update = function ( delta ) {
  120. if ( this.controls ) this.updateMovementModel( delta );
  121. this.updateBehaviors( delta );
  122. this.updateAnimations( delta );
  123. };
  124. this.updateAnimations = function ( delta ) {
  125. var mix = 1;
  126. if ( this.blendCounter > 0 ) {
  127. mix = ( this.transitionFrames - this.blendCounter ) / this.transitionFrames;
  128. this.blendCounter -= 1;
  129. }
  130. if ( this.meshBody ) {
  131. this.meshBody.update( delta );
  132. this.meshBody.setAnimationWeight( this.activeAnimation, mix );
  133. this.meshBody.setAnimationWeight( this.oldAnimation, 1 - mix );
  134. }
  135. if ( this.meshWeapon ) {
  136. this.meshWeapon.update( delta );
  137. this.meshWeapon.setAnimationWeight( this.activeAnimation, mix );
  138. this.meshWeapon.setAnimationWeight( this.oldAnimation, 1 - mix );
  139. }
  140. };
  141. this.updateBehaviors = function ( delta ) {
  142. var controls = this.controls;
  143. var animations = this.animations;
  144. var moveAnimation, idleAnimation;
  145. // crouch vs stand
  146. if ( controls.crouch ) {
  147. moveAnimation = animations[ "crouchMove" ];
  148. idleAnimation = animations[ "crouchIdle" ];
  149. } else {
  150. moveAnimation = animations[ "move" ];
  151. idleAnimation = animations[ "idle" ];
  152. }
  153. // actions
  154. if ( controls.jump ) {
  155. moveAnimation = animations[ "jump" ];
  156. idleAnimation = animations[ "jump" ];
  157. }
  158. if ( controls.attack ) {
  159. if ( controls.crouch ) {
  160. moveAnimation = animations[ "crouchAttack" ];
  161. idleAnimation = animations[ "crouchAttack" ];
  162. } else {
  163. moveAnimation = animations[ "attack" ];
  164. idleAnimation = animations[ "attack" ];
  165. }
  166. }
  167. // set animations
  168. if ( controls.moveForward || controls.moveBackward || controls.moveLeft || controls.moveRight ) {
  169. if ( this.activeAnimation !== moveAnimation ) {
  170. this.setAnimation( moveAnimation );
  171. }
  172. }
  173. if ( Math.abs( this.speed ) < 0.2 * this.maxSpeed && !( controls.moveLeft || controls.moveRight ) ) {
  174. if ( this.activeAnimation !== idleAnimation ) {
  175. this.setAnimation( idleAnimation );
  176. }
  177. }
  178. // set animation direction
  179. if ( controls.moveForward ) {
  180. if ( this.meshBody ) {
  181. this.meshBody.setAnimationDirectionForward( this.activeAnimation );
  182. this.meshBody.setAnimationDirectionForward( this.oldAnimation );
  183. }
  184. if ( this.meshWeapon ) {
  185. this.meshWeapon.setAnimationDirectionForward( this.activeAnimation );
  186. this.meshWeapon.setAnimationDirectionForward( this.oldAnimation );
  187. }
  188. }
  189. if ( controls.moveBackward ) {
  190. if ( this.meshBody ) {
  191. this.meshBody.setAnimationDirectionBackward( this.activeAnimation );
  192. this.meshBody.setAnimationDirectionBackward( this.oldAnimation );
  193. }
  194. if ( this.meshWeapon ) {
  195. this.meshWeapon.setAnimationDirectionBackward( this.activeAnimation );
  196. this.meshWeapon.setAnimationDirectionBackward( this.oldAnimation );
  197. }
  198. }
  199. };
  200. this.updateMovementModel = function ( delta ) {
  201. var controls = this.controls;
  202. // speed based on controls
  203. if ( controls.crouch ) this.maxSpeed = this.crouchSpeed;
  204. else this.maxSpeed = this.walkSpeed;
  205. this.maxReverseSpeed = -this.maxSpeed;
  206. if ( controls.moveForward ) this.speed = THREE.Math.clamp( this.speed + delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  207. if ( controls.moveBackward ) this.speed = THREE.Math.clamp( this.speed - delta * this.backAcceleration, this.maxReverseSpeed, this.maxSpeed );
  208. // orientation based on controls
  209. // (don't just stand while turning)
  210. var dir = 1;
  211. if ( controls.moveLeft ) {
  212. this.bodyOrientation += delta * this.angularSpeed;
  213. this.speed = THREE.Math.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  214. }
  215. if ( controls.moveRight ) {
  216. this.bodyOrientation -= delta * this.angularSpeed;
  217. this.speed = THREE.Math.clamp( this.speed + dir * delta * this.frontAcceleration, this.maxReverseSpeed, this.maxSpeed );
  218. }
  219. // speed decay
  220. if ( ! ( controls.moveForward || controls.moveBackward ) ) {
  221. if ( this.speed > 0 ) {
  222. var k = exponentialEaseOut( this.speed / this.maxSpeed );
  223. this.speed = THREE.Math.clamp( this.speed - k * delta * this.frontDecceleration, 0, this.maxSpeed );
  224. } else {
  225. var k = exponentialEaseOut( this.speed / this.maxReverseSpeed );
  226. this.speed = THREE.Math.clamp( this.speed + k * delta * this.backAcceleration, this.maxReverseSpeed, 0 );
  227. }
  228. }
  229. // displacement
  230. var forwardDelta = this.speed * delta;
  231. this.root.position.x += Math.sin( this.bodyOrientation ) * forwardDelta;
  232. this.root.position.z += Math.cos( this.bodyOrientation ) * forwardDelta;
  233. // steering
  234. this.root.rotation.y = this.bodyOrientation;
  235. };
  236. // internal helpers
  237. function loadTextures( baseUrl, textureUrls ) {
  238. var mapping = new THREE.UVMapping();
  239. var textures = [];
  240. for ( var i = 0; i < textureUrls.length; i ++ ) {
  241. textures[ i ] = THREE.ImageUtils.loadTexture( baseUrl + textureUrls[ i ], mapping, checkLoadingComplete );
  242. textures[ i ].name = textureUrls[ i ];
  243. }
  244. return textures;
  245. };
  246. function createPart( geometry, skinMap ) {
  247. geometry.computeMorphNormals();
  248. var whiteMap = THREE.ImageUtils.generateDataTexture( 1, 1, new THREE.Color( 0xffffff ) );
  249. 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 } );
  250. 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 } );
  251. materialTexture.wrapAround = true;
  252. //
  253. var mesh = new THREE.MorphBlendMesh( geometry, materialTexture );
  254. mesh.rotation.y = -Math.PI/2;
  255. mesh.castShadow = true;
  256. mesh.receiveShadow = true;
  257. //
  258. mesh.materialTexture = materialTexture;
  259. mesh.materialWireframe = materialWireframe;
  260. //
  261. mesh.autoCreateAnimations( scope.animationFPS );
  262. return mesh;
  263. };
  264. function checkLoadingComplete() {
  265. scope.loadCounter -= 1;
  266. if ( scope.loadCounter === 0 ) scope.onLoadComplete();
  267. };
  268. function exponentialEaseOut( k ) { return k === 1 ? 1 : - Math.pow( 2, - 10 * k ) + 1; }
  269. };