humanoid_base.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #include "humanoid_base.h"
  2. #include "../game/core/component.h"
  3. #include "../game/core/entity.h"
  4. #include "../game/core/world.h"
  5. #include "../game/units/troop_config.h"
  6. #include "../game/visuals/team_colors.h"
  7. #include "geom/math_utils.h"
  8. #include "geom/selection_ring.h"
  9. #include "geom/transforms.h"
  10. #include "gl/mesh.h"
  11. #include "gl/primitives.h"
  12. #include "humanoid_math.h"
  13. #include <QMatrix4x4>
  14. #include <algorithm>
  15. #include <cmath>
  16. namespace Render::GL {
  17. using Render::Geom::clamp01;
  18. using Render::Geom::clampf;
  19. using Render::Geom::clampVec01;
  20. using Render::Geom::coneFromTo;
  21. using Render::Geom::cylinderBetween;
  22. using Render::Geom::sphereAt;
  23. void HumanoidRendererBase::getVariant(const DrawContext &ctx, uint32_t seed,
  24. HumanoidVariant &v) const {
  25. QVector3D teamTint = resolveTeamTint(ctx);
  26. v.palette = makeHumanoidPalette(teamTint, seed);
  27. }
  28. void HumanoidRendererBase::customizePose(const DrawContext &ctx,
  29. const AnimationInputs &anim,
  30. uint32_t seed,
  31. HumanoidPose &ioPose) const {}
  32. void HumanoidRendererBase::addAttachments(const DrawContext &ctx,
  33. const HumanoidVariant &v,
  34. const HumanoidPose &pose,
  35. const AnimationInputs &anim,
  36. ISubmitter &out) const {}
  37. QVector3D HumanoidRendererBase::resolveTeamTint(const DrawContext &ctx) {
  38. QVector3D tunic(0.8f, 0.9f, 1.0f);
  39. Engine::Core::UnitComponent *unit = nullptr;
  40. Engine::Core::RenderableComponent *rc = nullptr;
  41. if (ctx.entity) {
  42. unit = ctx.entity->getComponent<Engine::Core::UnitComponent>();
  43. rc = ctx.entity->getComponent<Engine::Core::RenderableComponent>();
  44. }
  45. if (unit && unit->ownerId > 0) {
  46. tunic = Game::Visuals::teamColorForOwner(unit->ownerId);
  47. } else if (rc) {
  48. tunic = QVector3D(rc->color[0], rc->color[1], rc->color[2]);
  49. }
  50. return tunic;
  51. }
  52. FormationParams HumanoidRendererBase::resolveFormation(const DrawContext &ctx) {
  53. FormationParams params;
  54. params.individualsPerUnit = 1;
  55. params.maxPerRow = 1;
  56. params.spacing = 0.75f;
  57. if (ctx.entity) {
  58. auto *unit = ctx.entity->getComponent<Engine::Core::UnitComponent>();
  59. if (unit && !unit->unitType.empty()) {
  60. params.individualsPerUnit =
  61. Game::Units::TroopConfig::instance().getIndividualsPerUnit(
  62. unit->unitType);
  63. params.maxPerRow = Game::Units::TroopConfig::instance().getMaxUnitsPerRow(
  64. unit->unitType);
  65. if (unit->unitType == "mounted_knight") {
  66. params.spacing = 1.35f;
  67. }
  68. }
  69. }
  70. return params;
  71. }
  72. AnimationInputs HumanoidRendererBase::sampleAnimState(const DrawContext &ctx) {
  73. AnimationInputs anim;
  74. anim.time = ctx.animationTime;
  75. anim.isMoving = false;
  76. anim.isAttacking = false;
  77. anim.isMelee = false;
  78. anim.isInHoldMode = false;
  79. anim.isExitingHold = false;
  80. anim.holdExitProgress = 0.0f;
  81. if (!ctx.entity)
  82. return anim;
  83. if (ctx.entity->hasComponent<Engine::Core::PendingRemovalComponent>())
  84. return anim;
  85. auto *movement = ctx.entity->getComponent<Engine::Core::MovementComponent>();
  86. auto *attack = ctx.entity->getComponent<Engine::Core::AttackComponent>();
  87. auto *attackTarget =
  88. ctx.entity->getComponent<Engine::Core::AttackTargetComponent>();
  89. auto *transform =
  90. ctx.entity->getComponent<Engine::Core::TransformComponent>();
  91. auto *holdMode = ctx.entity->getComponent<Engine::Core::HoldModeComponent>();
  92. anim.isInHoldMode = (holdMode && holdMode->active);
  93. if (holdMode && !holdMode->active && holdMode->exitCooldown > 0.0f) {
  94. anim.isExitingHold = true;
  95. anim.holdExitProgress =
  96. 1.0f - (holdMode->exitCooldown / holdMode->standUpDuration);
  97. }
  98. anim.isMoving = (movement && movement->hasTarget);
  99. if (attack && attackTarget && attackTarget->targetId > 0 && transform) {
  100. anim.isMelee = (attack->currentMode ==
  101. Engine::Core::AttackComponent::CombatMode::Melee);
  102. bool stationary = !anim.isMoving;
  103. float currentCooldown =
  104. anim.isMelee ? attack->meleeCooldown : attack->cooldown;
  105. bool recentlyFired =
  106. attack->timeSinceLast < std::min(currentCooldown, 0.45f);
  107. bool targetInRange = false;
  108. if (ctx.world) {
  109. auto *target = ctx.world->getEntity(attackTarget->targetId);
  110. if (target) {
  111. auto *targetTransform =
  112. target->getComponent<Engine::Core::TransformComponent>();
  113. if (targetTransform) {
  114. float dx = targetTransform->position.x - transform->position.x;
  115. float dz = targetTransform->position.z - transform->position.z;
  116. float distSquared = dx * dx + dz * dz;
  117. float targetRadius = 0.0f;
  118. if (target->hasComponent<Engine::Core::BuildingComponent>()) {
  119. targetRadius =
  120. std::max(targetTransform->scale.x, targetTransform->scale.z) *
  121. 0.5f;
  122. } else {
  123. targetRadius =
  124. std::max(targetTransform->scale.x, targetTransform->scale.z) *
  125. 0.5f;
  126. }
  127. float effectiveRange = attack->range + targetRadius + 0.25f;
  128. targetInRange = (distSquared <= effectiveRange * effectiveRange);
  129. }
  130. }
  131. }
  132. anim.isAttacking = stationary && (targetInRange || recentlyFired);
  133. }
  134. return anim;
  135. }
  136. void HumanoidRendererBase::computeLocomotionPose(
  137. uint32_t seed, float time, bool isMoving, const VariationParams &variation,
  138. HumanoidPose &pose) {
  139. using HP = HumanProportions;
  140. float hScale = variation.heightScale;
  141. pose.headPos =
  142. QVector3D(0.0f, (HP::HEAD_TOP_Y + HP::CHIN_Y) * 0.5f * hScale, 0.0f);
  143. pose.headR = HP::HEAD_RADIUS * hScale;
  144. pose.neckBase = QVector3D(0.0f, HP::NECK_BASE_Y * hScale, 0.0f);
  145. float bScale = variation.bulkScale;
  146. float sWidth = variation.stanceWidth;
  147. pose.shoulderL = QVector3D(-HP::TORSO_TOP_R * 0.98f * bScale,
  148. HP::SHOULDER_Y * hScale, 0.0f);
  149. pose.shoulderR = QVector3D(HP::TORSO_TOP_R * 0.98f * bScale,
  150. HP::SHOULDER_Y * hScale, 0.0f);
  151. pose.footYOffset = 0.02f;
  152. pose.footL = QVector3D(-HP::SHOULDER_WIDTH * 0.58f * sWidth,
  153. HP::GROUND_Y + pose.footYOffset, 0.0f);
  154. pose.footR = QVector3D(HP::SHOULDER_WIDTH * 0.58f * sWidth,
  155. HP::GROUND_Y + pose.footYOffset, 0.0f);
  156. pose.pelvisPos = QVector3D(0.0f, HP::WAIST_Y * hScale, 0.0f);
  157. pose.kneeL = QVector3D(pose.footL.x(), HP::KNEE_Y * hScale, pose.footL.z());
  158. pose.kneeR = QVector3D(pose.footR.x(), HP::KNEE_Y * hScale, pose.footR.z());
  159. pose.shoulderL.setY(pose.shoulderL.y() + variation.shoulderTilt);
  160. pose.shoulderR.setY(pose.shoulderR.y() - variation.shoulderTilt);
  161. float slouchOffset = variation.postureSlump * 0.15f;
  162. pose.shoulderL.setZ(pose.shoulderL.z() + slouchOffset);
  163. pose.shoulderR.setZ(pose.shoulderR.z() + slouchOffset);
  164. float footAngleJitter = (hash01(seed ^ 0x5678u) - 0.5f) * 0.12f;
  165. float footDepthJitter = (hash01(seed ^ 0x9ABCu) - 0.5f) * 0.08f;
  166. pose.footL.setX(pose.footL.x() + footAngleJitter);
  167. pose.footR.setX(pose.footR.x() - footAngleJitter);
  168. pose.footL.setZ(pose.footL.z() + footDepthJitter);
  169. pose.footR.setZ(pose.footR.z() - footDepthJitter);
  170. float armHeightJitter = (hash01(seed ^ 0xABCDu) - 0.5f) * 0.03f;
  171. float armAsymmetry = (hash01(seed ^ 0xDEF0u) - 0.5f) * 0.04f;
  172. pose.handL =
  173. QVector3D(-0.05f + armAsymmetry,
  174. HP::SHOULDER_Y * hScale + 0.05f + armHeightJitter, 0.55f);
  175. pose.handR = QVector3D(
  176. 0.15f - armAsymmetry * 0.5f,
  177. HP::SHOULDER_Y * hScale + 0.15f + armHeightJitter * 0.8f, 0.20f);
  178. if (isMoving) {
  179. float walkCycleTime = 0.8f / variation.walkSpeedMult;
  180. float walkPhase = fmod(time * (1.0f / walkCycleTime), 1.0f);
  181. float leftPhase = walkPhase;
  182. float rightPhase = fmod(walkPhase + 0.5f, 1.0f);
  183. const float groundY = HP::GROUND_Y;
  184. const float strideLength = 0.35f * variation.armSwingAmp;
  185. auto animateFoot = [groundY, &pose, strideLength](QVector3D &foot,
  186. float phase) {
  187. float lift = std::sin(phase * 2.0f * 3.14159f);
  188. if (lift > 0.0f) {
  189. foot.setY(groundY + pose.footYOffset + lift * 0.12f);
  190. } else {
  191. foot.setY(groundY + pose.footYOffset);
  192. }
  193. foot.setZ(foot.z() +
  194. std::sin((phase - 0.25f) * 2.0f * 3.14159f) * strideLength);
  195. };
  196. animateFoot(pose.footL, leftPhase);
  197. animateFoot(pose.footR, rightPhase);
  198. float hipSway =
  199. std::sin(walkPhase * 2.0f * 3.14159f) * 0.02f * variation.armSwingAmp;
  200. pose.shoulderL.setX(pose.shoulderL.x() + hipSway);
  201. pose.shoulderR.setX(pose.shoulderR.x() + hipSway);
  202. }
  203. QVector3D rightAxis = pose.shoulderR - pose.shoulderL;
  204. rightAxis.setY(0.0f);
  205. if (rightAxis.lengthSquared() < 1e-8f)
  206. rightAxis = QVector3D(1, 0, 0);
  207. rightAxis.normalize();
  208. QVector3D outwardL = -rightAxis;
  209. QVector3D outwardR = rightAxis;
  210. pose.elbowL = elbowBendTorso(pose.shoulderL, pose.handL, outwardL, 0.45f,
  211. 0.15f, -0.08f, +1.0f);
  212. pose.elbowR = elbowBendTorso(pose.shoulderR, pose.handR, outwardR, 0.48f,
  213. 0.12f, 0.02f, +1.0f);
  214. }
  215. void HumanoidRendererBase::drawCommonBody(const DrawContext &ctx,
  216. const HumanoidVariant &v,
  217. const HumanoidPose &pose,
  218. ISubmitter &out) const {
  219. using HP = HumanProportions;
  220. QVector3D scaling = getProportionScaling();
  221. float widthScale = scaling.x();
  222. float heightScale = scaling.y();
  223. float headScale = scaling.z();
  224. QVector3D rightAxis = pose.shoulderR - pose.shoulderL;
  225. if (rightAxis.lengthSquared() < 1e-8f)
  226. rightAxis = QVector3D(1, 0, 0);
  227. rightAxis.normalize();
  228. const float yShoulder = 0.5f * (pose.shoulderL.y() + pose.shoulderR.y());
  229. const float yNeck = pose.neckBase.y();
  230. const float shoulderHalfSpan =
  231. 0.5f * std::abs(pose.shoulderR.x() - pose.shoulderL.x());
  232. const float torsoR =
  233. std::max(HP::TORSO_TOP_R * widthScale, shoulderHalfSpan * 0.95f);
  234. const float yTopCover = std::max(yShoulder + 0.04f, yNeck + 0.00f);
  235. QVector3D tunicTop{0.0f, yTopCover - 0.006f, 0.0f};
  236. QVector3D tunicBot{0.0f, pose.pelvisPos.y() + 0.03f, 0.0f};
  237. out.mesh(getUnitTorso(),
  238. cylinderBetween(ctx.model, tunicTop, tunicBot, torsoR),
  239. v.palette.cloth, nullptr, 1.0f);
  240. QVector3D chinPos{0.0f, pose.headPos.y() - pose.headR, 0.0f};
  241. out.mesh(getUnitCylinder(),
  242. cylinderBetween(ctx.model, pose.neckBase, chinPos,
  243. HP::NECK_RADIUS * widthScale),
  244. v.palette.skin * 0.9f, nullptr, 1.0f);
  245. out.mesh(getUnitSphere(),
  246. sphereAt(ctx.model, pose.headPos, pose.headR * headScale),
  247. v.palette.skin, nullptr, 1.0f);
  248. QVector3D iris(0.06f, 0.06f, 0.07f);
  249. float eyeZ = pose.headR * headScale * 0.7f;
  250. float eyeY = pose.headPos.y() + pose.headR * headScale * 0.1f;
  251. float eyeSpacing = pose.headR * headScale * 0.35f;
  252. out.mesh(getUnitSphere(),
  253. ctx.model * sphereAt(QVector3D(-eyeSpacing, eyeY, eyeZ),
  254. pose.headR * headScale * 0.15f),
  255. iris, nullptr, 1.0f);
  256. out.mesh(getUnitSphere(),
  257. ctx.model * sphereAt(QVector3D(eyeSpacing, eyeY, eyeZ),
  258. pose.headR * headScale * 0.15f),
  259. iris, nullptr, 1.0f);
  260. const float upperArmR = HP::UPPER_ARM_R * widthScale;
  261. const float foreArmR = HP::FORE_ARM_R * widthScale;
  262. const float jointR = HP::HAND_RADIUS * widthScale * 1.05f;
  263. const float handR = HP::HAND_RADIUS * widthScale * 0.95f;
  264. out.mesh(getUnitCylinder(),
  265. cylinderBetween(ctx.model, pose.shoulderL, pose.elbowL, upperArmR),
  266. v.palette.cloth, nullptr, 1.0f);
  267. out.mesh(getUnitSphere(), sphereAt(ctx.model, pose.elbowL, jointR),
  268. v.palette.cloth * 0.95f, nullptr, 1.0f);
  269. out.mesh(getUnitCylinder(),
  270. cylinderBetween(ctx.model, pose.elbowL, pose.handL, foreArmR),
  271. v.palette.skin * 0.95f, nullptr, 1.0f);
  272. out.mesh(getUnitSphere(), sphereAt(ctx.model, pose.handL, handR),
  273. v.palette.leatherDark * 0.92f, nullptr, 1.0f);
  274. out.mesh(getUnitCylinder(),
  275. cylinderBetween(ctx.model, pose.shoulderR, pose.elbowR, upperArmR),
  276. v.palette.cloth, nullptr, 1.0f);
  277. out.mesh(getUnitSphere(), sphereAt(ctx.model, pose.elbowR, jointR),
  278. v.palette.cloth * 0.95f, nullptr, 1.0f);
  279. out.mesh(getUnitCylinder(),
  280. cylinderBetween(ctx.model, pose.elbowR, pose.handR, foreArmR),
  281. v.palette.skin * 0.95f, nullptr, 1.0f);
  282. out.mesh(getUnitSphere(), sphereAt(ctx.model, pose.handR, handR),
  283. v.palette.leatherDark * 0.92f, nullptr, 1.0f);
  284. const float hipHalf = HP::UPPER_LEG_R * widthScale * 1.7f;
  285. const float maxStance = hipHalf * 2.2f;
  286. const float upperScale = 1.40f * 3.0f * widthScale;
  287. const float lowerScale = 1.35f * 3.0f * widthScale;
  288. const float footLenMul = (5.5f * 0.1f);
  289. const float footRadMul = 0.70f;
  290. const float kneeForward = 0.15f;
  291. const float kneeDrop = 0.02f * HP::LOWER_LEG_LEN;
  292. const QVector3D FWD(0.f, 0.f, 1.f);
  293. const float upperR = HP::UPPER_LEG_R * upperScale;
  294. const float lowerR = HP::LOWER_LEG_R * lowerScale;
  295. const float footR = lowerR * footRadMul;
  296. constexpr float DEG = 3.1415926535f / 180.f;
  297. const QVector3D hipL = pose.pelvisPos + QVector3D(-hipHalf, 0.f, 0.f);
  298. const QVector3D hipR = pose.pelvisPos + QVector3D(+hipHalf, 0.f, 0.f);
  299. const float midX = 0.5f * (hipL.x() + hipR.x());
  300. auto clampX = [&](const QVector3D &v, float mid) {
  301. const float dx = v.x() - mid;
  302. const float mag = std::min(std::abs(dx), maxStance);
  303. return QVector3D(mid + (dx < 0 ? -mag : mag), v.y(), v.z());
  304. };
  305. const QVector3D plantL = clampX(pose.footL, midX);
  306. const QVector3D plantR = clampX(pose.footR, midX);
  307. const float footLen = footLenMul * lowerR;
  308. const float heelBackFrac = 0.15f;
  309. const float ballFrac = 0.72f;
  310. const float toeUpFrac = 0.06f;
  311. const float yawOutDeg = 12.0f;
  312. const float ankleFwdFrac = 0.10f;
  313. const float ankleUpFrac = 0.50f;
  314. const float toeSplayFrac = 0.06f;
  315. const QVector3D fwdL = rotY(FWD, -yawOutDeg * DEG);
  316. const QVector3D fwdR = rotY(FWD, +yawOutDeg * DEG);
  317. const QVector3D rightL = rightOf(fwdL);
  318. const QVector3D rightR = rightOf(fwdR);
  319. const float footCLyL = plantL.y() + footR;
  320. const float footCLyR = plantR.y() + footR;
  321. QVector3D heelCenL(plantL.x(), footCLyL, plantL.z());
  322. QVector3D heelCenR(plantR.x(), footCLyR, plantR.z());
  323. QVector3D ankleL = heelCenL + fwdL * (ankleFwdFrac * footLen);
  324. QVector3D ankleR = heelCenR + fwdR * (ankleFwdFrac * footLen);
  325. ankleL.setY(heelCenL.y() + ankleUpFrac * footR);
  326. ankleR.setY(heelCenR.y() + ankleUpFrac * footR);
  327. const float kneeForwardPush = HP::LOWER_LEG_LEN * kneeForward;
  328. const float kneeDropAbs = kneeDrop;
  329. QVector3D kneeL, kneeR;
  330. bool useCustomKnees = (pose.kneeL.y() < HP::KNEE_Y * 0.9f ||
  331. pose.kneeR.y() < HP::KNEE_Y * 0.9f);
  332. if (useCustomKnees) {
  333. kneeL = pose.kneeL;
  334. kneeR = pose.kneeR;
  335. } else {
  336. auto computeKnee = [&](const QVector3D &hip, const QVector3D &ankle) {
  337. QVector3D dir = ankle - hip;
  338. QVector3D knee = hip + 0.5f * dir;
  339. knee += QVector3D(0, 0, 1) * kneeForwardPush;
  340. knee.setY(knee.y() - kneeDropAbs);
  341. knee.setX((hip.x() + ankle.x()) * 0.5f);
  342. return knee;
  343. };
  344. kneeL = computeKnee(hipL, ankleL);
  345. kneeR = computeKnee(hipR, ankleR);
  346. }
  347. const float heelBack = heelBackFrac * footLen;
  348. const float ballLen = ballFrac * footLen;
  349. const float toeLen = (1.0f - ballFrac) * footLen;
  350. QVector3D ballL = heelCenL + fwdL * ballLen;
  351. QVector3D ballR = heelCenR + fwdR * ballLen;
  352. const float toeUpL = toeUpFrac * footLen;
  353. const float toeUpR = toeUpFrac * footLen;
  354. const float toeSplay = toeSplayFrac * footLen;
  355. QVector3D toeL = ballL + fwdL * toeLen - rightL * toeSplay;
  356. QVector3D toeR = ballR + fwdR * toeLen + rightR * toeSplay;
  357. toeL.setY(ballL.y() + toeUpL);
  358. toeR.setY(ballR.y() + toeUpR);
  359. heelCenL -= fwdL * heelBack;
  360. heelCenR -= fwdR * heelBack;
  361. const float heelRad = footR * 1.05f;
  362. const float toeRad = footR * 0.85f;
  363. out.mesh(getUnitCapsule(8, 1),
  364. Render::Geom::capsuleBetween(ctx.model, hipL, kneeL, upperR),
  365. v.palette.leather, nullptr, 1.0f);
  366. out.mesh(getUnitCapsule(8, 1),
  367. Render::Geom::capsuleBetween(ctx.model, hipR, kneeR, upperR),
  368. v.palette.leather, nullptr, 1.0f);
  369. out.mesh(getUnitCapsule(8, 1),
  370. Render::Geom::capsuleBetween(ctx.model, kneeL, ankleL, lowerR),
  371. v.palette.leatherDark, nullptr, 1.0f);
  372. out.mesh(getUnitCapsule(8, 1),
  373. Render::Geom::capsuleBetween(ctx.model, kneeR, ankleR, lowerR),
  374. v.palette.leatherDark, nullptr, 1.0f);
  375. out.mesh(getUnitCapsule(8, 1),
  376. Render::Geom::capsuleBetween(ctx.model, heelCenL, ballL, heelRad),
  377. v.palette.leatherDark, nullptr, 1.0f);
  378. out.mesh(getUnitCapsule(8, 1),
  379. Render::Geom::capsuleBetween(ctx.model, heelCenR, ballR, heelRad),
  380. v.palette.leatherDark, nullptr, 1.0f);
  381. out.mesh(getUnitCapsule(8, 1),
  382. Render::Geom::capsuleBetween(ctx.model, ballL, toeL, toeRad),
  383. v.palette.leatherDark, nullptr, 1.0f);
  384. out.mesh(getUnitCapsule(8, 1),
  385. Render::Geom::capsuleBetween(ctx.model, ballR, toeR, toeRad),
  386. v.palette.leatherDark, nullptr, 1.0f);
  387. drawHelmet(ctx, v, pose, out);
  388. drawArmorOverlay(ctx, v, pose, yTopCover, torsoR, shoulderHalfSpan, upperArmR,
  389. rightAxis, out);
  390. drawShoulderDecorations(ctx, v, pose, yTopCover, yNeck, rightAxis, out);
  391. }
  392. void HumanoidRendererBase::drawHelmet(const DrawContext &ctx,
  393. const HumanoidVariant &v,
  394. const HumanoidPose &pose,
  395. ISubmitter &out) const {
  396. using HP = HumanProportions;
  397. QVector3D capC = pose.headPos + QVector3D(0, pose.headR * 0.8f, 0);
  398. out.mesh(getUnitSphere(), sphereAt(ctx.model, capC, pose.headR * 0.85f),
  399. v.palette.cloth * 0.9f, nullptr, 1.0f);
  400. }
  401. void HumanoidRendererBase::drawArmorOverlay(
  402. const DrawContext &ctx, const HumanoidVariant &v, const HumanoidPose &pose,
  403. float yTopCover, float torsoR, float shoulderHalfSpan, float upperArmR,
  404. const QVector3D &rightAxis, ISubmitter &out) const {}
  405. void HumanoidRendererBase::drawShoulderDecorations(const DrawContext &ctx,
  406. const HumanoidVariant &v,
  407. const HumanoidPose &pose,
  408. float yTopCover, float yNeck,
  409. const QVector3D &rightAxis,
  410. ISubmitter &out) const {}
  411. void HumanoidRendererBase::drawSelectionFX(const DrawContext &ctx,
  412. ISubmitter &out) {
  413. if (ctx.selected || ctx.hovered) {
  414. float ringSize = 0.5f;
  415. if (ctx.entity) {
  416. auto *unit = ctx.entity->getComponent<Engine::Core::UnitComponent>();
  417. if (unit && !unit->unitType.empty()) {
  418. ringSize = Game::Units::TroopConfig::instance().getSelectionRingSize(
  419. unit->unitType);
  420. }
  421. }
  422. QMatrix4x4 ringM;
  423. QVector3D pos = ctx.model.column(3).toVector3D();
  424. ringM.translate(pos.x(), pos.y() + 0.05f, pos.z());
  425. ringM.scale(ringSize, 1.0f, ringSize);
  426. if (ctx.selected)
  427. out.selectionRing(ringM, 0.6f, 0.25f, QVector3D(0.2f, 0.4f, 1.0f));
  428. else
  429. out.selectionRing(ringM, 0.35f, 0.15f, QVector3D(0.90f, 0.90f, 0.25f));
  430. }
  431. }
  432. void HumanoidRendererBase::render(const DrawContext &ctx,
  433. ISubmitter &out) const {
  434. FormationParams formation = resolveFormation(ctx);
  435. AnimationInputs anim = sampleAnimState(ctx);
  436. Engine::Core::UnitComponent *unitComp = nullptr;
  437. if (ctx.entity) {
  438. unitComp = ctx.entity->getComponent<Engine::Core::UnitComponent>();
  439. }
  440. uint32_t seed = 0u;
  441. if (unitComp) {
  442. seed ^= uint32_t(unitComp->ownerId * 2654435761u);
  443. }
  444. if (ctx.entity) {
  445. seed ^= uint32_t(reinterpret_cast<uintptr_t>(ctx.entity) & 0xFFFFFFFFu);
  446. }
  447. const int rows = (formation.individualsPerUnit + formation.maxPerRow - 1) /
  448. formation.maxPerRow;
  449. const int cols = formation.maxPerRow;
  450. int visibleCount = rows * cols;
  451. if (unitComp) {
  452. int mh = std::max(1, unitComp->maxHealth);
  453. float ratio = std::clamp(unitComp->health / float(mh), 0.0f, 1.0f);
  454. visibleCount = std::max(1, (int)std::ceil(ratio * float(rows * cols)));
  455. }
  456. HumanoidVariant variant;
  457. getVariant(ctx, seed, variant);
  458. if (!m_proportionScaleCached) {
  459. m_cachedProportionScale = getProportionScaling();
  460. m_proportionScaleCached = true;
  461. }
  462. const QVector3D propScale = m_cachedProportionScale;
  463. const float heightScale = propScale.y();
  464. const bool needsHeightScaling = std::abs(heightScale - 1.0f) > 0.001f;
  465. const QMatrix4x4 kIdentityMatrix;
  466. auto fastRandom = [](uint32_t &state) -> float {
  467. state = state * 1664525u + 1013904223u;
  468. return float(state & 0x7FFFFFu) / float(0x7FFFFFu);
  469. };
  470. for (int idx = 0; idx < visibleCount; ++idx) {
  471. int r = idx / cols;
  472. int c = idx % cols;
  473. float offsetX = (c - (cols - 1) * 0.5f) * formation.spacing;
  474. float offsetZ = (r - (rows - 1) * 0.5f) * formation.spacing;
  475. uint32_t instSeed = seed ^ uint32_t(idx * 9176u);
  476. uint32_t rngState = instSeed;
  477. float posJitterX = (fastRandom(rngState) - 0.5f) * 0.05f;
  478. float posJitterZ = (fastRandom(rngState) - 0.5f) * 0.05f;
  479. float verticalJitter = (fastRandom(rngState) - 0.5f) * 0.03f;
  480. float yawOffset = (fastRandom(rngState) - 0.5f) * 5.0f;
  481. float phaseOffset = fastRandom(rngState) * 0.25f;
  482. offsetX += posJitterX;
  483. offsetZ += posJitterZ;
  484. QMatrix4x4 instModel;
  485. if (ctx.entity) {
  486. if (auto *entT =
  487. ctx.entity->getComponent<Engine::Core::TransformComponent>()) {
  488. QMatrix4x4 M = kIdentityMatrix;
  489. M.translate(entT->position.x, entT->position.y, entT->position.z);
  490. float baseYaw = entT->rotation.y;
  491. float appliedYaw = baseYaw + yawOffset;
  492. M.rotate(appliedYaw, 0.0f, 1.0f, 0.0f);
  493. M.scale(entT->scale.x, entT->scale.y, entT->scale.z);
  494. M.translate(offsetX, verticalJitter, offsetZ);
  495. instModel = M;
  496. } else {
  497. instModel = ctx.model;
  498. instModel.rotate(yawOffset, 0.0f, 1.0f, 0.0f);
  499. instModel.translate(offsetX, verticalJitter, offsetZ);
  500. }
  501. } else {
  502. instModel = ctx.model;
  503. instModel.rotate(yawOffset, 0.0f, 1.0f, 0.0f);
  504. instModel.translate(offsetX, verticalJitter, offsetZ);
  505. }
  506. DrawContext instCtx{ctx.resources, ctx.entity, ctx.world, instModel};
  507. VariationParams variation = VariationParams::fromSeed(instSeed);
  508. float combinedHeightScale = heightScale * variation.heightScale;
  509. if (needsHeightScaling || std::abs(variation.heightScale - 1.0f) > 0.001f) {
  510. QMatrix4x4 scaleMatrix;
  511. scaleMatrix.scale(variation.bulkScale, combinedHeightScale, 1.0f);
  512. instCtx.model = instCtx.model * scaleMatrix;
  513. }
  514. HumanoidPose pose;
  515. computeLocomotionPose(instSeed, anim.time + phaseOffset, anim.isMoving,
  516. variation, pose);
  517. customizePose(instCtx, anim, instSeed, pose);
  518. drawCommonBody(instCtx, variant, pose, out);
  519. addAttachments(instCtx, variant, pose, anim, out);
  520. }
  521. drawSelectionFX(ctx, out);
  522. }
  523. } // namespace Render::GL