MD2CharacterComplex.js 12 KB

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