swordsman_renderer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #include "swordsman_renderer.h"
  2. #include "../../../../game/core/component.h"
  3. #include "../../../../game/systems/nation_id.h"
  4. #include "../../../equipment/equipment_registry.h"
  5. #include "../../../equipment/weapons/shield_renderer.h"
  6. #include "../../../equipment/weapons/sword_renderer.h"
  7. #include "../../../geom/math_utils.h"
  8. #include "../../../geom/transforms.h"
  9. #include "../../../gl/backend.h"
  10. #include "../../../gl/primitives.h"
  11. #include "../../../gl/shader.h"
  12. #include "../../../humanoid/humanoid_math.h"
  13. #include "../../../humanoid/humanoid_specs.h"
  14. #include "../../../humanoid/pose_controller.h"
  15. #include "../../../humanoid/rig.h"
  16. #include "../../../humanoid/style_palette.h"
  17. #include "../../../palette.h"
  18. #include "../../../scene_renderer.h"
  19. #include "../../../submitter.h"
  20. #include "../../registry.h"
  21. #include "../../renderer_constants.h"
  22. #include "swordsman_style.h"
  23. #include <numbers>
  24. #include <qmatrix4x4.h>
  25. #include <qstringliteral.h>
  26. #include <qvectornd.h>
  27. #include <unordered_map>
  28. #include <QMatrix4x4>
  29. #include <QString>
  30. #include <QVector3D>
  31. #include <algorithm>
  32. #include <cmath>
  33. #include <cstdint>
  34. #include <optional>
  35. #include <string>
  36. #include <string_view>
  37. namespace Render::GL::Roman {
  38. namespace {
  39. constexpr std::string_view k_swordsman_default_style_key = "default";
  40. constexpr float k_swordsman_team_mix_weight = 0.6F;
  41. constexpr float k_swordsman_style_mix_weight = 0.4F;
  42. auto swordsman_style_registry()
  43. -> std::unordered_map<std::string, KnightStyleConfig> & {
  44. static std::unordered_map<std::string, KnightStyleConfig> styles;
  45. return styles;
  46. }
  47. void ensure_swordsman_styles_registered() {
  48. static const bool registered = []() {
  49. register_roman_swordsman_style();
  50. return true;
  51. }();
  52. (void)registered;
  53. }
  54. } // namespace
  55. void register_swordsman_style(const std::string &nation_id,
  56. const KnightStyleConfig &style) {
  57. swordsman_style_registry()[nation_id] = style;
  58. }
  59. using Render::Geom::clamp01;
  60. using Render::Geom::clamp_f;
  61. using Render::Geom::cone_from_to;
  62. using Render::Geom::cylinder_between;
  63. using Render::Geom::ease_in_out_cubic;
  64. using Render::Geom::lerp;
  65. using Render::Geom::nlerp;
  66. using Render::Geom::smoothstep;
  67. using Render::Geom::sphere_at;
  68. using Render::GL::Humanoid::mix_palette_color;
  69. using Render::GL::Humanoid::saturate_color;
  70. struct KnightExtras {
  71. QVector3D metal_color;
  72. QVector3D shield_color;
  73. QVector3D shield_trim_color;
  74. float sword_length = 0.80F;
  75. float swordWidth = 0.065F;
  76. float shieldRadius = 0.18F;
  77. float shield_aspect = 1.0F;
  78. float guard_half_width = 0.12F;
  79. float handle_radius = 0.016F;
  80. float pommel_radius = 0.045F;
  81. float blade_ricasso = 0.16F;
  82. float blade_taper_bias = 0.65F;
  83. bool shieldCrossDecal = false;
  84. bool has_scabbard = true;
  85. };
  86. class KnightRenderer : public HumanoidRendererBase {
  87. public:
  88. static constexpr float kLimbWidthScale = 1.00F;
  89. static constexpr float kTorsoWidthScale = 0.55F;
  90. static constexpr float kHeightScale = 0.78F;
  91. static constexpr float kDepthScale = 0.26F;
  92. auto get_proportion_scaling() const -> QVector3D override {
  93. return {1.00F, kHeightScale, kDepthScale};
  94. }
  95. auto get_torso_scale() const -> float override { return kTorsoWidthScale; }
  96. private:
  97. mutable std::unordered_map<uint32_t, KnightExtras> m_extrasCache;
  98. public:
  99. void get_variant(const DrawContext &ctx, uint32_t seed,
  100. HumanoidVariant &v) const override {
  101. QVector3D const team_tint = resolve_team_tint(ctx);
  102. v.palette = make_humanoid_palette(team_tint, seed);
  103. auto const &style = resolve_style(ctx);
  104. apply_palette_overrides(style, team_tint, v);
  105. }
  106. void customize_pose(const DrawContext &,
  107. const HumanoidAnimationContext &anim_ctx, uint32_t seed,
  108. HumanoidPose &pose) const override {
  109. using HP = HumanProportions;
  110. const AnimationInputs &anim = anim_ctx.inputs;
  111. HumanoidPoseController controller(pose, anim_ctx);
  112. float const arm_height_jitter = (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.03F;
  113. float const arm_asymmetry = (hash_01(seed ^ 0xDEF0U) - 0.5F) * 0.04F;
  114. if (anim.is_attacking && anim.is_melee) {
  115. float const attack_phase =
  116. std::fmod(anim_ctx.attack_phase * KNIGHT_INV_ATTACK_CYCLE_TIME, 1.0F);
  117. controller.sword_slash_variant(attack_phase, anim.attack_variant);
  118. } else {
  119. QVector3D const idle_hand_r(0.30F + arm_asymmetry,
  120. HP::SHOULDER_Y - 0.02F + arm_height_jitter,
  121. 0.35F);
  122. QVector3D const idle_hand_l(-0.22F - 0.5F * arm_asymmetry,
  123. HP::SHOULDER_Y + 0.5F * arm_height_jitter,
  124. 0.18F);
  125. controller.place_hand_at(false, idle_hand_r);
  126. controller.place_hand_at(true, idle_hand_l);
  127. }
  128. }
  129. void add_attachments(const DrawContext &ctx, const HumanoidVariant &v,
  130. const HumanoidPose &pose,
  131. const HumanoidAnimationContext &anim_ctx,
  132. ISubmitter &out) const override {
  133. const AnimationInputs &anim = anim_ctx.inputs;
  134. uint32_t const seed = reinterpret_cast<uintptr_t>(ctx.entity) & 0xFFFFFFFFU;
  135. auto const &style = resolve_style(ctx);
  136. QVector3D const team_tint = resolve_team_tint(ctx);
  137. KnightExtras extras;
  138. auto it = m_extrasCache.find(seed);
  139. if (it != m_extrasCache.end()) {
  140. extras = it->second;
  141. } else {
  142. extras = computeKnightExtras(seed, v);
  143. apply_extras_overrides(style, team_tint, v, extras);
  144. m_extrasCache[seed] = extras;
  145. if (m_extrasCache.size() > MAX_EXTRAS_CACHE_SIZE) {
  146. m_extrasCache.clear();
  147. }
  148. }
  149. apply_extras_overrides(style, team_tint, v, extras);
  150. bool const is_attacking = anim.is_attacking && anim.is_melee;
  151. auto &registry = EquipmentRegistry::instance();
  152. auto sword = registry.get(EquipmentCategory::Weapon, "sword_roman");
  153. if (sword) {
  154. SwordRenderConfig sword_config;
  155. sword_config.metal_color = extras.metal_color;
  156. sword_config.sword_length = extras.sword_length;
  157. sword_config.sword_width = extras.swordWidth;
  158. sword_config.guard_half_width = extras.guard_half_width;
  159. sword_config.handle_radius = extras.handle_radius;
  160. sword_config.pommel_radius = extras.pommel_radius;
  161. sword_config.blade_ricasso = extras.blade_ricasso;
  162. sword_config.blade_taper_bias = extras.blade_taper_bias;
  163. sword_config.has_scabbard = extras.has_scabbard;
  164. auto *sword_renderer = dynamic_cast<SwordRenderer *>(sword.get());
  165. if (sword_renderer) {
  166. sword_renderer->set_config(sword_config);
  167. }
  168. sword->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  169. }
  170. auto shield = registry.get(EquipmentCategory::Weapon, "shield_roman");
  171. if (shield) {
  172. shield->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  173. }
  174. if (!is_attacking && extras.has_scabbard) {
  175. drawScabbard(ctx, pose, v, extras, out);
  176. }
  177. }
  178. void draw_helmet(const DrawContext &ctx, const HumanoidVariant &v,
  179. const HumanoidPose &pose, ISubmitter &out) const override {
  180. auto &registry = EquipmentRegistry::instance();
  181. auto helmet = registry.get(EquipmentCategory::Helmet, "roman_heavy");
  182. if (helmet) {
  183. HumanoidAnimationContext anim_ctx{};
  184. helmet->render(ctx, pose.body_frames, v.palette, anim_ctx, out);
  185. }
  186. }
  187. void draw_armor(const DrawContext &ctx, const HumanoidVariant &v,
  188. const HumanoidPose &pose,
  189. const HumanoidAnimationContext &anim,
  190. ISubmitter &out) const override {
  191. auto &registry = EquipmentRegistry::instance();
  192. auto armor = registry.get(EquipmentCategory::Armor, "roman_heavy_armor");
  193. if (armor) {
  194. armor->render(ctx, pose.body_frames, v.palette, anim, out);
  195. }
  196. auto shoulder_cover =
  197. registry.get(EquipmentCategory::Armor, "roman_shoulder_cover");
  198. if (shoulder_cover) {
  199. shoulder_cover->render(ctx, pose.body_frames, v.palette, anim, out);
  200. }
  201. auto greaves = registry.get(EquipmentCategory::Armor, "roman_greaves");
  202. if (greaves) {
  203. greaves->render(ctx, pose.body_frames, v.palette, anim, out);
  204. }
  205. }
  206. private:
  207. static auto computeKnightExtras(uint32_t seed,
  208. const HumanoidVariant &v) -> KnightExtras {
  209. KnightExtras e;
  210. e.metal_color = QVector3D(0.72F, 0.73F, 0.78F);
  211. float const shield_hue = hash_01(seed ^ 0x12345U);
  212. if (shield_hue < 0.45F) {
  213. e.shield_color = v.palette.cloth * 1.10F;
  214. } else if (shield_hue < 0.90F) {
  215. e.shield_color = v.palette.leather * 1.25F;
  216. } else {
  217. e.shield_color = e.metal_color * 0.95F;
  218. }
  219. e.sword_length = 0.80F + (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.16F;
  220. e.swordWidth = 0.060F + (hash_01(seed ^ 0x7777U) - 0.5F) * 0.010F;
  221. e.shieldRadius = 0.16F + (hash_01(seed ^ 0xDEF0U) - 0.5F) * 0.04F;
  222. e.guard_half_width = 0.120F + (hash_01(seed ^ 0x3456U) - 0.5F) * 0.020F;
  223. e.handle_radius = 0.016F + (hash_01(seed ^ 0x88AAU) - 0.5F) * 0.003F;
  224. e.pommel_radius = 0.045F + (hash_01(seed ^ 0x19C3U) - 0.5F) * 0.006F;
  225. e.blade_ricasso =
  226. clamp_f(0.14F + (hash_01(seed ^ 0xBEEFU) - 0.5F) * 0.04F, 0.10F, 0.20F);
  227. e.blade_taper_bias =
  228. clamp01(0.6F + (hash_01(seed ^ 0xFACEU) - 0.5F) * 0.2F);
  229. e.shieldCrossDecal = (hash_01(seed ^ 0xA11CU) > 0.55F);
  230. e.has_scabbard = (hash_01(seed ^ 0x5CABU) > 0.15F);
  231. e.shield_trim_color = e.metal_color * 0.95F;
  232. e.shield_aspect = 1.0F;
  233. return e;
  234. }
  235. static void drawScabbard(const DrawContext &ctx, const HumanoidPose &,
  236. const HumanoidVariant &v, const KnightExtras &extras,
  237. ISubmitter &out) {
  238. using HP = HumanProportions;
  239. QVector3D const hip(0.10F, HP::WAIST_Y - 0.04F, -0.02F);
  240. QVector3D const tip = hip + QVector3D(-0.05F, -0.22F, -0.12F);
  241. float const sheath_r = extras.swordWidth * 0.85F;
  242. out.mesh(get_unit_cylinder(),
  243. cylinder_between(ctx.model, hip, tip, sheath_r),
  244. v.palette.leather * 0.9F, nullptr, 1.0F);
  245. out.mesh(get_unit_cone(),
  246. cone_from_to(ctx.model, tip,
  247. tip + QVector3D(-0.02F, -0.02F, -0.02F), sheath_r),
  248. extras.metal_color, nullptr, 1.0F);
  249. }
  250. auto
  251. resolve_style(const DrawContext &ctx) const -> const KnightStyleConfig & {
  252. ensure_swordsman_styles_registered();
  253. auto &styles = swordsman_style_registry();
  254. std::string nation_id;
  255. if (ctx.entity != nullptr) {
  256. if (auto *unit =
  257. ctx.entity->get_component<Engine::Core::UnitComponent>()) {
  258. nation_id = Game::Systems::nation_id_to_string(unit->nation_id);
  259. }
  260. }
  261. if (!nation_id.empty()) {
  262. auto it = styles.find(nation_id);
  263. if (it != styles.end()) {
  264. return it->second;
  265. }
  266. }
  267. auto it_default = styles.find(std::string(k_swordsman_default_style_key));
  268. if (it_default != styles.end()) {
  269. return it_default->second;
  270. }
  271. static const KnightStyleConfig k_empty{};
  272. return k_empty;
  273. }
  274. public:
  275. auto resolve_shader_key(const DrawContext &ctx) const -> QString {
  276. const KnightStyleConfig &style = resolve_style(ctx);
  277. if (!style.shader_id.empty()) {
  278. return QString::fromStdString(style.shader_id);
  279. }
  280. return QStringLiteral("swordsman");
  281. }
  282. private:
  283. void apply_palette_overrides(const KnightStyleConfig &style,
  284. const QVector3D &team_tint,
  285. HumanoidVariant &variant) const {
  286. auto apply_color = [&](const std::optional<QVector3D> &override_color,
  287. QVector3D &target) {
  288. target = mix_palette_color(target, override_color, team_tint,
  289. k_swordsman_team_mix_weight,
  290. k_swordsman_style_mix_weight);
  291. };
  292. apply_color(style.cloth_color, variant.palette.cloth);
  293. apply_color(style.leather_color, variant.palette.leather);
  294. apply_color(style.leather_dark_color, variant.palette.leather_dark);
  295. apply_color(style.metal_color, variant.palette.metal);
  296. }
  297. void apply_extras_overrides(const KnightStyleConfig &style,
  298. const QVector3D &team_tint,
  299. const HumanoidVariant &variant,
  300. KnightExtras &extras) const {
  301. extras.metal_color = saturate_color(variant.palette.metal);
  302. extras.shield_color = saturate_color(extras.shield_color);
  303. extras.shield_trim_color = saturate_color(extras.shield_trim_color);
  304. auto apply_shield_color =
  305. [&](const std::optional<QVector3D> &override_color, QVector3D &target) {
  306. target = mix_palette_color(target, override_color, team_tint,
  307. k_swordsman_team_mix_weight,
  308. k_swordsman_style_mix_weight);
  309. };
  310. apply_shield_color(style.shield_color, extras.shield_color);
  311. apply_shield_color(style.shield_trim_color, extras.shield_trim_color);
  312. if (style.shield_radius_scale) {
  313. extras.shieldRadius =
  314. std::max(0.10F, extras.shieldRadius * *style.shield_radius_scale);
  315. }
  316. if (style.shield_aspect_ratio) {
  317. extras.shield_aspect = std::max(0.40F, *style.shield_aspect_ratio);
  318. }
  319. if (style.has_scabbard) {
  320. extras.has_scabbard = *style.has_scabbard;
  321. }
  322. if (style.shield_cross_decal) {
  323. extras.shieldCrossDecal = *style.shield_cross_decal;
  324. }
  325. }
  326. };
  327. void register_knight_renderer(Render::GL::EntityRendererRegistry &registry) {
  328. ensure_swordsman_styles_registered();
  329. static KnightRenderer const renderer;
  330. registry.register_renderer(
  331. "troops/roman/swordsman", [](const DrawContext &ctx, ISubmitter &out) {
  332. static KnightRenderer const static_renderer;
  333. Shader *swordsman_shader = nullptr;
  334. if (ctx.backend != nullptr) {
  335. QString shader_key = static_renderer.resolve_shader_key(ctx);
  336. swordsman_shader = ctx.backend->shader(shader_key);
  337. if (swordsman_shader == nullptr) {
  338. swordsman_shader = ctx.backend->shader(QStringLiteral("swordsman"));
  339. }
  340. }
  341. auto *scene_renderer = dynamic_cast<Renderer *>(&out);
  342. if ((scene_renderer != nullptr) && (swordsman_shader != nullptr)) {
  343. scene_renderer->set_current_shader(swordsman_shader);
  344. }
  345. static_renderer.render(ctx, out);
  346. if (scene_renderer != nullptr) {
  347. scene_renderer->set_current_shader(nullptr);
  348. }
  349. });
  350. }
  351. } // namespace Render::GL::Roman