MD2CharacterComplex.js 12 KB

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