MD2CharacterComplex.js 12 KB

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