MD2CharacterComplex.js 12 KB

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