| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858 |
- #include "rig.h"
- #include "../../game/core/component.h"
- #include "../../game/core/entity.h"
- #include "../../game/core/world.h"
- #include "../../game/map/terrain_service.h"
- #include "../../game/systems/nation_id.h"
- #include "../../game/units/spawn_type.h"
- #include "../../game/units/troop_config.h"
- #include "../../game/visuals/team_colors.h"
- #include "../entity/registry.h"
- #include "../geom/affine_matrix.h"
- #include "../geom/math_utils.h"
- #include "../geom/transforms.h"
- #include "../gl/backend.h"
- #include "../gl/camera.h"
- #include "../gl/humanoid/animation/animation_inputs.h"
- #include "../gl/humanoid/animation/gait.h"
- #include "../gl/humanoid/humanoid_constants.h"
- #include "../gl/primitives.h"
- #include "../gl/render_constants.h"
- #include "../gl/resources.h"
- #include "../palette.h"
- #include "../pose_palette_cache.h"
- #include "../scene_renderer.h"
- #include "../submitter.h"
- #include "../template_cache.h"
- #include "../visibility_budget.h"
- #include "formation_calculator.h"
- #include "humanoid_math.h"
- #include "pose_controller.h"
- #include <QMatrix4x4>
- #include <QVector2D>
- #include <QVector4D>
- #include <QtMath>
- #include <algorithm>
- #include <array>
- #include <cmath>
- #include <cstdint>
- #include <functional>
- #include <limits>
- #include <numbers>
- #include <unordered_map>
- #include <vector>
- namespace Render::GL {
- using namespace Render::GL::Geometry;
- using Render::Geom::capsule_between;
- using Render::Geom::cone_from_to;
- using Render::Geom::cylinder_between;
- using Render::Geom::sphere_at;
- namespace {
- constexpr float k_shadow_size_infantry = 0.16F;
- constexpr float k_shadow_size_mounted = 0.35F;
- constexpr float k_run_extra_foot_lift = 0.08F;
- constexpr float k_run_stride_enhancement = 0.15F;
- struct CachedPoseEntry {
- HumanoidPose pose;
- VariationParams variation;
- uint32_t frame_number{0};
- bool was_moving{false};
- };
- using PoseCacheKey = uint64_t;
- static std::unordered_map<PoseCacheKey, CachedPoseEntry> s_pose_cache;
- static uint32_t s_current_frame = 0;
- constexpr uint32_t k_pose_cache_max_age = 300;
- struct SoldierLayout {
- float offset_x{0.0F};
- float offset_z{0.0F};
- float vertical_jitter{0.0F};
- float yaw_offset{0.0F};
- float phase_offset{0.0F};
- std::uint32_t inst_seed{0};
- };
- struct UnitLayoutCacheEntry {
- std::vector<SoldierLayout> soldiers;
- FormationParams formation{};
- FormationCalculatorFactory::Nation nation{
- FormationCalculatorFactory::Nation::Roman};
- FormationCalculatorFactory::UnitCategory category{
- FormationCalculatorFactory::UnitCategory::Infantry};
- int rows{0};
- int cols{0};
- std::uint32_t seed{0};
- std::uint32_t frame_number{0};
- };
- static std::unordered_map<std::uintptr_t, UnitLayoutCacheEntry> s_layout_cache;
- constexpr uint32_t k_layout_cache_max_age = 600;
- inline auto make_pose_cache_key(uintptr_t entity_ptr,
- int soldier_idx) -> PoseCacheKey {
- return (static_cast<uint64_t>(entity_ptr) << 16) |
- static_cast<uint64_t>(soldier_idx & 0xFFFF);
- }
- static HumanoidRenderStats s_render_stats;
- constexpr float k_shadow_ground_offset = 0.02F;
- constexpr float k_shadow_base_alpha = 0.24F;
- constexpr QVector3D k_shadow_light_dir(0.4F, 1.0F, 0.25F);
- constexpr float k_temporal_skip_distance_reduced = 35.0F;
- constexpr float k_temporal_skip_distance_minimal = 45.0F;
- constexpr uint32_t k_temporal_skip_period_reduced = 2;
- constexpr uint32_t k_temporal_skip_period_minimal = 3;
- inline auto should_render_temporal(uint32_t frame, uint32_t seed,
- uint32_t period) -> bool {
- if (period <= 1) {
- return true;
- }
- return ((frame + seed) % period) == 0U;
- }
- inline auto make_yaw_scale_model(float cos_yaw, float sin_yaw, float scale_x,
- float scale_y, float scale_z,
- const QVector3D &translation) -> QMatrix4x4 {
- QMatrix4x4 m;
- float *d = m.data();
- d[0] = cos_yaw * scale_x;
- d[1] = 0.0F;
- d[2] = -sin_yaw * scale_x;
- d[3] = 0.0F;
- d[4] = 0.0F;
- d[5] = scale_y;
- d[6] = 0.0F;
- d[7] = 0.0F;
- d[8] = sin_yaw * scale_z;
- d[9] = 0.0F;
- d[10] = cos_yaw * scale_z;
- d[11] = 0.0F;
- d[12] = translation.x();
- d[13] = translation.y();
- d[14] = translation.z();
- d[15] = 1.0F;
- return m;
- }
- inline auto
- resolve_layout_seed(const Engine::Core::UnitComponent *unit_comp,
- const Engine::Core::Entity *entity) -> std::uint32_t {
- std::uint32_t seed = 0U;
- if (unit_comp != nullptr) {
- seed ^= std::uint32_t(unit_comp->owner_id * 2654435761U);
- }
- if (entity != nullptr) {
- seed ^= std::uint32_t(reinterpret_cast<uintptr_t>(entity) & 0xFFFFFFFFU);
- }
- return seed;
- }
- inline auto resolve_variant_key_from_seed(std::uint32_t seed) -> std::uint8_t {
- std::uint32_t v = seed ^ (seed >> 16);
- return static_cast<std::uint8_t>(v % k_template_variant_count);
- }
- inline auto
- resolve_attack_variant_from_seed(std::uint32_t seed) -> std::uint8_t {
- return static_cast<std::uint8_t>(seed % 3U);
- }
- inline auto resolve_variant_seed(const Engine::Core::UnitComponent *unit_comp,
- std::uint8_t variant_key) -> std::uint32_t {
- std::uint32_t seed = 0U;
- if (unit_comp != nullptr) {
- seed ^= static_cast<std::uint32_t>(unit_comp->spawn_type) * 2654435761U;
- seed ^= static_cast<std::uint32_t>(unit_comp->owner_id) * 1013904223U;
- }
- seed ^= static_cast<std::uint32_t>(variant_key) * 2246822519U;
- return seed;
- }
- inline auto resolve_formation_category(
- const Engine::Core::UnitComponent *unit_comp,
- const AnimationInputs &anim) -> FormationCalculatorFactory::UnitCategory {
- using UnitCategory = FormationCalculatorFactory::UnitCategory;
- if ((unit_comp != nullptr) &&
- unit_comp->spawn_type == Game::Units::SpawnType::Builder &&
- anim.is_constructing) {
- return UnitCategory::BuilderConstruction;
- }
- bool is_mounted = false;
- if (unit_comp != nullptr) {
- using Game::Units::SpawnType;
- auto const st = unit_comp->spawn_type;
- is_mounted =
- (st == SpawnType::MountedKnight || st == SpawnType::HorseArcher ||
- st == SpawnType::HorseSpearman);
- }
- return is_mounted ? UnitCategory::Cavalry : UnitCategory::Infantry;
- }
- auto get_or_build_unit_layout(std::uintptr_t key,
- const FormationParams &formation,
- FormationCalculatorFactory::Nation nation,
- FormationCalculatorFactory::UnitCategory category,
- std::uint32_t seed, int rows,
- int cols) -> UnitLayoutCacheEntry & {
- auto &entry = s_layout_cache[key];
- bool rebuild =
- entry.soldiers.empty() || entry.rows != rows || entry.cols != cols ||
- entry.seed != seed || entry.nation != nation ||
- entry.category != category ||
- std::abs(entry.formation.spacing - formation.spacing) > 1e-6F ||
- entry.formation.individuals_per_unit != formation.individuals_per_unit ||
- entry.formation.max_per_row != formation.max_per_row;
- if (rebuild) {
- entry.soldiers.clear();
- entry.soldiers.reserve(static_cast<std::size_t>(rows * cols));
- entry.rows = rows;
- entry.cols = cols;
- entry.seed = seed;
- entry.nation = nation;
- entry.category = category;
- entry.formation = formation;
- const IFormationCalculator *formation_calculator =
- FormationCalculatorFactory::get_calculator(nation, category);
- auto fast_random = [](uint32_t &state) -> float {
- state = state * 1664525U + 1013904223U;
- return float(state & 0x7FFFFFU) / float(0x7FFFFFU);
- };
- for (int idx = 0; idx < rows * cols; ++idx) {
- int const r = idx / cols;
- int const c = idx % cols;
- FormationOffset const formation_offset =
- formation_calculator->calculate_offset(idx, r, c, rows, cols,
- formation.spacing, seed);
- std::uint32_t const inst_seed = seed ^ std::uint32_t(idx * 9176U);
- std::uint32_t rng_state = inst_seed;
- SoldierLayout layout{};
- layout.offset_x = formation_offset.offset_x;
- layout.offset_z = formation_offset.offset_z;
- layout.vertical_jitter = (fast_random(rng_state) - 0.5F) * 0.03F;
- layout.yaw_offset = (fast_random(rng_state) - 0.5F) * 5.0F;
- layout.phase_offset = fast_random(rng_state) * 0.25F;
- layout.inst_seed = inst_seed;
- entry.soldiers.push_back(layout);
- }
- }
- entry.frame_number = s_current_frame;
- return entry;
- }
- } // namespace
- void advance_pose_cache_frame() {
- ++s_current_frame;
- if ((s_current_frame & 0x1FF) == 0) {
- auto it = s_pose_cache.begin();
- while (it != s_pose_cache.end()) {
- if (s_current_frame - it->second.frame_number >
- k_pose_cache_max_age * 2) {
- it = s_pose_cache.erase(it);
- } else {
- ++it;
- }
- }
- auto layout_it = s_layout_cache.begin();
- while (layout_it != s_layout_cache.end()) {
- if (s_current_frame - layout_it->second.frame_number >
- k_layout_cache_max_age * 2) {
- layout_it = s_layout_cache.erase(layout_it);
- } else {
- ++layout_it;
- }
- }
- }
- }
- void clear_humanoid_caches() {
- s_pose_cache.clear();
- s_layout_cache.clear();
- s_current_frame = 0;
- }
- auto torso_mesh_without_bottom_cap() -> Mesh * {
- static std::unique_ptr<Mesh> s_mesh;
- if (s_mesh != nullptr) {
- return s_mesh.get();
- }
- Mesh *base = get_unit_torso();
- if (base == nullptr) {
- return nullptr;
- }
- auto filtered = base->clone_with_filtered_indices(
- [](unsigned int a, unsigned int b, unsigned int c,
- const std::vector<Vertex> &verts) -> bool {
- auto sample = [&](unsigned int idx) -> QVector3D {
- return {verts[idx].position[0], verts[idx].position[1],
- verts[idx].position[2]};
- };
- QVector3D pa = sample(a);
- QVector3D pb = sample(b);
- QVector3D pc = sample(c);
- float min_y = std::min({pa.y(), pb.y(), pc.y()});
- float max_y = std::max({pa.y(), pb.y(), pc.y()});
- QVector3D n(
- verts[a].normal[0] + verts[b].normal[0] + verts[c].normal[0],
- verts[a].normal[1] + verts[b].normal[1] + verts[c].normal[1],
- verts[a].normal[2] + verts[b].normal[2] + verts[c].normal[2]);
- if (n.lengthSquared() > 0.0F) {
- n.normalize();
- }
- constexpr float k_band_height = 0.02F;
- constexpr float k_bottom_threshold = 0.45F;
- bool is_flat = (max_y - min_y) < k_band_height;
- bool is_at_bottom = min_y > k_bottom_threshold;
- bool facing_down = (n.y() > 0.35F);
- return is_flat && is_at_bottom && facing_down;
- });
- s_mesh =
- (filtered != nullptr) ? std::move(filtered) : std::unique_ptr<Mesh>(base);
- return s_mesh.get();
- }
- auto HumanoidRendererBase::frame_local_position(
- const AttachmentFrame &frame, const QVector3D &local) -> QVector3D {
- float const lx = local.x() * frame.radius;
- float const ly = local.y() * frame.radius;
- float const lz = local.z() * frame.radius;
- return frame.origin + frame.right * lx + frame.up * ly + frame.forward * lz;
- }
- auto HumanoidRendererBase::make_frame_local_transform(
- const QMatrix4x4 &parent, const AttachmentFrame &frame,
- const QVector3D &local_offset, float uniform_scale) -> QMatrix4x4 {
- float scale = frame.radius * uniform_scale;
- if (scale == 0.0F) {
- scale = uniform_scale;
- }
- QVector3D const origin = frame_local_position(frame, local_offset);
- QMatrix4x4 local;
- local.setColumn(0, QVector4D(frame.right * scale, 0.0F));
- local.setColumn(1, QVector4D(frame.up * scale, 0.0F));
- local.setColumn(2, QVector4D(frame.forward * scale, 0.0F));
- local.setColumn(3, QVector4D(origin, 1.0F));
- return parent * local;
- }
- auto HumanoidRendererBase::head_local_position(
- const HeadFrame &frame, const QVector3D &local) -> QVector3D {
- return frame_local_position(frame, local);
- }
- auto HumanoidRendererBase::make_head_local_transform(
- const QMatrix4x4 &parent, const HeadFrame &frame,
- const QVector3D &local_offset, float uniform_scale) -> QMatrix4x4 {
- return make_frame_local_transform(parent, frame, local_offset, uniform_scale);
- }
- void HumanoidRendererBase::get_variant(const DrawContext &ctx, uint32_t seed,
- HumanoidVariant &v) const {
- QVector3D const team_tint = resolve_team_tint(ctx);
- v.palette = make_humanoid_palette(team_tint, seed);
- }
- void HumanoidRendererBase::customize_pose(const DrawContext &,
- const HumanoidAnimationContext &,
- uint32_t, HumanoidPose &) const {}
- void HumanoidRendererBase::add_attachments(const DrawContext &,
- const HumanoidVariant &,
- const HumanoidPose &,
- const HumanoidAnimationContext &,
- ISubmitter &) const {}
- auto HumanoidRendererBase::resolve_entity_ground_offset(
- const DrawContext &, Engine::Core::UnitComponent *unit_comp,
- Engine::Core::TransformComponent *transform_comp) const -> float {
- (void)unit_comp;
- (void)transform_comp;
- return 0.0F;
- }
- auto HumanoidRendererBase::resolve_team_tint(const DrawContext &ctx)
- -> QVector3D {
- QVector3D tunic(0.8F, 0.9F, 1.0F);
- Engine::Core::UnitComponent *unit = nullptr;
- Engine::Core::RenderableComponent *rc = nullptr;
- if (ctx.entity != nullptr) {
- unit = ctx.entity->get_component<Engine::Core::UnitComponent>();
- rc = ctx.entity->get_component<Engine::Core::RenderableComponent>();
- }
- if ((unit != nullptr) && unit->owner_id > 0) {
- tunic = Game::Visuals::team_colorForOwner(unit->owner_id);
- } else if (rc != nullptr) {
- tunic = QVector3D(rc->color[0], rc->color[1], rc->color[2]);
- }
- return tunic;
- }
- auto HumanoidRendererBase::resolve_formation(const DrawContext &ctx)
- -> FormationParams {
- FormationParams params{};
- params.individuals_per_unit = 1;
- params.max_per_row = 1;
- params.spacing = 0.75F;
- if (ctx.entity != nullptr) {
- auto *unit = ctx.entity->get_component<Engine::Core::UnitComponent>();
- if (unit != nullptr) {
- params.individuals_per_unit =
- Game::Units::TroopConfig::instance().getIndividualsPerUnit(
- unit->spawn_type);
- params.max_per_row =
- Game::Units::TroopConfig::instance().getMaxUnitsPerRow(
- unit->spawn_type);
- if (unit->spawn_type == Game::Units::SpawnType::MountedKnight) {
- params.spacing = 1.05F;
- }
- }
- }
- return params;
- }
- void HumanoidRendererBase::compute_locomotion_pose(
- uint32_t seed, float time, bool is_moving, const VariationParams &variation,
- HumanoidPose &pose) {
- using HP = HumanProportions;
- float const h_scale = variation.height_scale;
- pose.head_pos = QVector3D(0.0F, HP::HEAD_CENTER_Y * h_scale, 0.0F);
- pose.head_r = HP::HEAD_RADIUS * h_scale;
- pose.neck_base = QVector3D(0.0F, HP::NECK_BASE_Y * h_scale, 0.0F);
- float const b_scale = variation.bulk_scale;
- float const s_width = variation.stance_width;
- float const half_shoulder_span = 0.5F * HP::SHOULDER_WIDTH * b_scale;
- pose.shoulder_l =
- QVector3D(-half_shoulder_span, HP::SHOULDER_Y * h_scale, 0.0F);
- pose.shoulder_r =
- QVector3D(half_shoulder_span, HP::SHOULDER_Y * h_scale, 0.0F);
- pose.pelvis_pos = QVector3D(0.0F, HP::WAIST_Y * h_scale, 0.0F);
- float const rest_stride = 0.06F + (variation.arm_swing_amp - 1.0F) * 0.045F;
- float const foot_x_span = HP::SHOULDER_WIDTH * 0.62F * s_width;
- pose.foot_y_offset = HP::FOOT_Y_OFFSET_DEFAULT;
- pose.foot_l =
- QVector3D(-foot_x_span, HP::GROUND_Y + pose.foot_y_offset, rest_stride);
- pose.foot_r =
- QVector3D(foot_x_span, HP::GROUND_Y + pose.foot_y_offset, -rest_stride);
- pose.shoulder_l.setY(pose.shoulder_l.y() + variation.shoulder_tilt);
- pose.shoulder_r.setY(pose.shoulder_r.y() - variation.shoulder_tilt);
- float const slouch_offset = variation.posture_slump * 0.15F;
- pose.shoulder_l.setZ(pose.shoulder_l.z() + slouch_offset);
- pose.shoulder_r.setZ(pose.shoulder_r.z() + slouch_offset);
- float const foot_inward_jitter = (hash_01(seed ^ 0x5678U) - 0.5F) * 0.02F;
- float const foot_forward_jitter = (hash_01(seed ^ 0x9ABCU) - 0.5F) * 0.035F;
- pose.foot_l.setX(pose.foot_l.x() + foot_inward_jitter);
- pose.foot_r.setX(pose.foot_r.x() - foot_inward_jitter);
- pose.foot_l.setZ(pose.foot_l.z() + foot_forward_jitter);
- pose.foot_r.setZ(pose.foot_r.z() - foot_forward_jitter);
- float const arm_height_jitter = (hash_01(seed ^ 0xABCDU) - 0.5F) * 0.03F;
- float const arm_asymmetry = (hash_01(seed ^ 0xDEF0U) - 0.5F) * 0.04F;
- pose.hand_l =
- QVector3D(-0.05F + arm_asymmetry,
- HP::SHOULDER_Y * h_scale + 0.05F + arm_height_jitter, 0.55F);
- pose.hand_r = QVector3D(
- 0.15F - arm_asymmetry * 0.5F,
- HP::SHOULDER_Y * h_scale + 0.15F + arm_height_jitter * 0.8F, 0.20F);
- if (is_moving) {
- float const walk_cycle_time = 0.8F / variation.walk_speed_mult;
- float const walk_phase = std::fmod(time * (1.0F / walk_cycle_time), 1.0F);
- float const left_phase = walk_phase;
- float const right_phase = std::fmod(walk_phase + 0.5F, 1.0F);
- const float ground_y = HP::GROUND_Y;
- const float stride_length = 0.35F * variation.arm_swing_amp;
- float const bob_phase = walk_phase * 2.0F;
- float const vertical_bob =
- std::sin(bob_phase * std::numbers::pi_v<float>) * 0.018F;
- float const hip_sway_amount = 0.002F;
- float const sway_raw =
- std::sin(walk_phase * 2.0F * std::numbers::pi_v<float>);
- float const hip_sway = sway_raw * hip_sway_amount;
- float const torso_sway_z = 0.0F;
- auto animate_foot = [ground_y, &pose, stride_length](QVector3D &foot,
- float phase) {
- float const lift_raw = std::sin(phase * 2.0F * std::numbers::pi_v<float>);
- float lift = 0.0F;
- if (lift_raw > 0.0F) {
- float const lift_phase =
- phase < 0.5F ? phase * 2.0F : (1.0F - phase) * 2.0F;
- float const ease_t =
- lift_phase * lift_phase * (3.0F - 2.0F * lift_phase);
- lift = lift_raw * ease_t;
- foot.setY(ground_y + pose.foot_y_offset + lift * 0.15F);
- } else {
- foot.setY(ground_y + pose.foot_y_offset);
- }
- float const stride_phase =
- (phase - 0.25F) * 2.0F * std::numbers::pi_v<float>;
- foot.setZ(foot.z() + std::sin(stride_phase) * stride_length);
- };
- animate_foot(pose.foot_l, left_phase);
- animate_foot(pose.foot_r, right_phase);
- pose.pelvis_pos.setY(pose.pelvis_pos.y() + vertical_bob);
- pose.shoulder_l.setY(pose.shoulder_l.y() + vertical_bob);
- pose.shoulder_r.setY(pose.shoulder_r.y() + vertical_bob);
- pose.neck_base.setY(pose.neck_base.y() + vertical_bob);
- pose.head_pos.setY(pose.head_pos.y() + vertical_bob);
- pose.pelvis_pos.setX(pose.pelvis_pos.x() + hip_sway);
- pose.shoulder_l.setZ(pose.shoulder_l.z() + torso_sway_z);
- pose.shoulder_r.setZ(pose.shoulder_r.z() + torso_sway_z);
- pose.neck_base.setZ(pose.neck_base.z() + torso_sway_z * 0.7F);
- pose.head_pos.setZ(pose.head_pos.z() + torso_sway_z * 0.5F);
- float const arm_swing_amp = 0.04F * variation.arm_swing_amp;
- float const arm_phase_offset = 0.15F;
- constexpr float max_arm_displacement = 0.06F;
- float const left_swing_raw = std::sin((left_phase + arm_phase_offset) *
- 2.0F * std::numbers::pi_v<float>);
- float const left_arm_swing =
- std::clamp(left_swing_raw * arm_swing_amp, -max_arm_displacement,
- max_arm_displacement);
- pose.hand_l.setZ(pose.hand_l.z() - left_arm_swing);
- float const right_swing_raw = std::sin((right_phase + arm_phase_offset) *
- 2.0F * std::numbers::pi_v<float>);
- float const right_arm_swing =
- std::clamp(right_swing_raw * arm_swing_amp, -max_arm_displacement,
- max_arm_displacement);
- pose.hand_r.setZ(pose.hand_r.z() - right_arm_swing);
- auto clamp_hand_reach = [&](const QVector3D &shoulder, QVector3D &hand) {
- float const max_reach =
- (HP::UPPER_ARM_LEN + HP::FORE_ARM_LEN) * h_scale * 0.98F;
- QVector3D diff = hand - shoulder;
- float const len = diff.length();
- if (len > max_reach && len > 1e-6F) {
- hand = shoulder + diff * (max_reach / len);
- }
- };
- clamp_hand_reach(pose.shoulder_l, pose.hand_l);
- clamp_hand_reach(pose.shoulder_r, pose.hand_r);
- }
- QVector3D const hip_l =
- pose.pelvis_pos +
- QVector3D(-HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- QVector3D const hip_r =
- pose.pelvis_pos +
- QVector3D(HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- auto solve_leg = [&](const QVector3D &hip, const QVector3D &foot,
- bool is_left) -> QVector3D {
- QVector3D hip_to_foot = foot - hip;
- float const distance = hip_to_foot.length();
- if (distance < HP::EPSILON_SMALL) {
- return hip;
- }
- float const upper_len = HP::UPPER_LEG_LEN * h_scale;
- float const lower_len = HP::LOWER_LEG_LEN * h_scale;
- float const reach = upper_len + lower_len;
- float const min_reach =
- std::max(std::abs(upper_len - lower_len) + 1e-4F, 1e-3F);
- float const max_reach = std::max(reach - 1e-4F, min_reach + 1e-4F);
- float const clamped_dist = std::clamp(distance, min_reach, max_reach);
- QVector3D const dir = hip_to_foot / distance;
- float cos_theta = (upper_len * upper_len + clamped_dist * clamped_dist -
- lower_len * lower_len) /
- (2.0F * upper_len * clamped_dist);
- cos_theta = std::clamp(cos_theta, -1.0F, 1.0F);
- float const sin_theta =
- std::sqrt(std::max(0.0F, 1.0F - cos_theta * cos_theta));
- QVector3D bend_pref = is_left ? QVector3D(-0.24F, 0.0F, 0.95F)
- : QVector3D(0.24F, 0.0F, 0.95F);
- bend_pref.normalize();
- QVector3D bend_axis =
- bend_pref - dir * QVector3D::dotProduct(dir, bend_pref);
- if (bend_axis.lengthSquared() < 1e-6F) {
- bend_axis = QVector3D::crossProduct(dir, QVector3D(0.0F, 1.0F, 0.0F));
- if (bend_axis.lengthSquared() < 1e-6F) {
- bend_axis = QVector3D::crossProduct(dir, QVector3D(1.0F, 0.0F, 0.0F));
- }
- }
- bend_axis.normalize();
- QVector3D const knee = hip + dir * (cos_theta * upper_len) +
- bend_axis * (sin_theta * upper_len);
- float const knee_floor = HP::GROUND_Y + pose.foot_y_offset * 0.5F;
- if (knee.y() < knee_floor) {
- QVector3D adjusted = knee;
- adjusted.setY(knee_floor);
- return adjusted;
- }
- return knee;
- };
- pose.knee_l = solve_leg(hip_l, pose.foot_l, true);
- pose.knee_r = solve_leg(hip_r, pose.foot_r, false);
- QVector3D right_axis = pose.shoulder_r - pose.shoulder_l;
- right_axis.setY(0.0F);
- if (right_axis.lengthSquared() < 1e-8F) {
- right_axis = QVector3D(1, 0, 0);
- }
- right_axis.normalize();
- if (right_axis.x() < 0.0F) {
- right_axis = -right_axis;
- }
- QVector3D const outward_l = -right_axis;
- QVector3D const outward_r = right_axis;
- pose.elbow_l = elbow_bend_torso(pose.shoulder_l, pose.hand_l, outward_l,
- 0.45F, 0.15F, -0.08F, +1.0F);
- pose.elbow_r = elbow_bend_torso(pose.shoulder_r, pose.hand_r, outward_r,
- 0.48F, 0.12F, 0.02F, +1.0F);
- }
- void HumanoidRendererBase::draw_common_body(const DrawContext &ctx,
- const HumanoidVariant &v,
- HumanoidPose &pose,
- ISubmitter &out) const {
- using HP = HumanProportions;
- QVector3D const scaling = get_proportion_scaling();
- float const width_scale = scaling.x();
- float const height_scale = scaling.y();
- float const torso_scale = get_torso_scale();
- float const head_scale = 1.0F;
- QVector3D right_axis = pose.shoulder_r - pose.shoulder_l;
- right_axis.setY(0.0F);
- right_axis.setZ(0.0F);
- if (right_axis.lengthSquared() < 1e-8F) {
- right_axis = QVector3D(1.0F, 0.0F, 0.0F);
- }
- right_axis.normalize();
- if (right_axis.x() < 0.0F) {
- right_axis = -right_axis;
- }
- QVector3D const up_axis(0.0F, 1.0F, 0.0F);
- QVector3D forward_axis = QVector3D::crossProduct(right_axis, up_axis);
- if (forward_axis.lengthSquared() < 1e-8F) {
- forward_axis = QVector3D(0.0F, 0.0F, 1.0F);
- }
- forward_axis.normalize();
- QVector3D const shoulder_mid = (pose.shoulder_l + pose.shoulder_r) * 0.5F;
- const float y_shoulder = shoulder_mid.y();
- const float y_neck = pose.neck_base.y();
- const float shoulder_half_span =
- 0.5F * std::abs(pose.shoulder_r.x() - pose.shoulder_l.x());
- const float torso_r_base =
- std::max(HP::TORSO_TOP_R, shoulder_half_span * 0.95F);
- const float torso_r = torso_r_base * torso_scale;
- float const depth_scale = scaling.z();
- const float torso_depth_factor =
- std::clamp(HP::TORSO_DEPTH_FACTOR_BASE + (depth_scale - 1.0F) * 0.20F,
- HP::TORSO_DEPTH_FACTOR_MIN, HP::TORSO_DEPTH_FACTOR_MAX);
- float torso_depth = torso_r * torso_depth_factor;
- const float y_top_cover =
- std::max(y_shoulder + 0.00F, y_neck + HP::TORSO_TOP_COVER_OFFSET);
- const float upper_arm_r = HP::UPPER_ARM_R * width_scale;
- const float fore_arm_r = HP::FORE_ARM_R * width_scale;
- const float joint_r = HP::HAND_RADIUS * width_scale * 1.05F;
- const float hand_r = HP::HAND_RADIUS * width_scale * 0.95F;
- const float leg_joint_r = HP::LOWER_LEG_R * width_scale * 0.95F;
- const float thigh_r = HP::UPPER_LEG_R * width_scale;
- const float shin_r = HP::LOWER_LEG_R * width_scale;
- const float foot_radius = shin_r * 1.10F;
- float const torso_x = (shoulder_mid.x() + pose.pelvis_pos.x()) * 0.5F;
- constexpr float torso_z = 0.0F;
- QVector3D const tunic_top{torso_x, y_top_cover - 0.006F, torso_z};
- QVector3D const tunic_bot{torso_x, pose.pelvis_pos.y() - 0.05F, torso_z};
- QMatrix4x4 torso_transform =
- cylinder_between(ctx.model, tunic_top, tunic_bot, 1.0F);
- torso_transform.scale(torso_r, 1.0F, torso_depth);
- Mesh *torso_mesh = torso_mesh_without_bottom_cap();
- if (torso_mesh != nullptr) {
- out.mesh(torso_mesh, torso_transform, v.palette.cloth, nullptr, 1.0F);
- }
- float const head_r = pose.head_r;
- QVector3D head_up;
- QVector3D head_right;
- QVector3D head_forward;
- if (pose.head_frame.radius > 0.001F) {
- head_up = pose.head_frame.up;
- head_right = pose.head_frame.right;
- head_forward = pose.head_frame.forward;
- } else {
- head_up = pose.head_pos - pose.neck_base;
- if (head_up.lengthSquared() < 1e-8F) {
- head_up = up_axis;
- } else {
- head_up.normalize();
- }
- head_right =
- right_axis - head_up * QVector3D::dotProduct(right_axis, head_up);
- if (head_right.lengthSquared() < 1e-8F) {
- head_right = QVector3D::crossProduct(head_up, forward_axis);
- if (head_right.lengthSquared() < 1e-8F) {
- head_right = QVector3D(1.0F, 0.0F, 0.0F);
- }
- }
- head_right.normalize();
- if (QVector3D::dotProduct(head_right, right_axis) < 0.0F) {
- head_right = -head_right;
- }
- head_forward = QVector3D::crossProduct(head_right, head_up);
- if (head_forward.lengthSquared() < 1e-8F) {
- head_forward = forward_axis;
- } else {
- head_forward.normalize();
- }
- if (QVector3D::dotProduct(head_forward, forward_axis) < 0.0F) {
- head_right = -head_right;
- head_forward = -head_forward;
- }
- }
- QVector3D const chin_pos = pose.head_pos - head_up * head_r;
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.neck_base, chin_pos,
- HP::NECK_RADIUS * width_scale),
- v.palette.skin * 0.9F, nullptr, 1.0F);
- QMatrix4x4 head_rot;
- head_rot.setColumn(0, QVector4D(head_right, 0.0F));
- head_rot.setColumn(1, QVector4D(head_up, 0.0F));
- head_rot.setColumn(2, QVector4D(head_forward, 0.0F));
- head_rot.setColumn(3, QVector4D(0.0F, 0.0F, 0.0F, 1.0F));
- QMatrix4x4 head_transform = ctx.model;
- head_transform.translate(pose.head_pos);
- head_transform = head_transform * head_rot;
- head_transform.scale(head_r);
- out.mesh(get_unit_sphere(), head_transform, v.palette.skin, nullptr, 1.0F);
- pose.head_frame.origin = pose.head_pos;
- pose.head_frame.right = head_right;
- pose.head_frame.up = head_up;
- pose.head_frame.forward = head_forward;
- pose.head_frame.radius = head_r;
- pose.body_frames.head = pose.head_frame;
- QVector3D const torso_center = QVector3D(
- (shoulder_mid.x() + pose.pelvis_pos.x()) * 0.5F, y_shoulder, 0.0F);
- pose.body_frames.torso.origin = torso_center;
- pose.body_frames.torso.right = right_axis;
- pose.body_frames.torso.up = up_axis;
- pose.body_frames.torso.forward = forward_axis;
- pose.body_frames.torso.radius = torso_r;
- pose.body_frames.torso.depth = torso_depth;
- pose.body_frames.back.origin = torso_center - forward_axis * torso_depth;
- pose.body_frames.back.right = right_axis;
- pose.body_frames.back.up = up_axis;
- pose.body_frames.back.forward = -forward_axis;
- pose.body_frames.back.radius = torso_r * 0.75F;
- pose.body_frames.back.depth = torso_depth * 0.90F;
- pose.body_frames.waist.origin = pose.pelvis_pos;
- pose.body_frames.waist.right = right_axis;
- pose.body_frames.waist.up = up_axis;
- pose.body_frames.waist.forward = forward_axis;
- pose.body_frames.waist.radius = torso_r * 0.80F;
- pose.body_frames.waist.depth = torso_depth * 0.72F;
- QVector3D shoulder_up = (pose.shoulder_l - pose.pelvis_pos).normalized();
- QVector3D shoulder_forward_l =
- QVector3D::crossProduct(-right_axis, shoulder_up);
- if (shoulder_forward_l.lengthSquared() < 1e-8F) {
- shoulder_forward_l = forward_axis;
- } else {
- shoulder_forward_l.normalize();
- }
- pose.body_frames.shoulder_l.origin = pose.shoulder_l;
- pose.body_frames.shoulder_l.right = -right_axis;
- pose.body_frames.shoulder_l.up = shoulder_up;
- pose.body_frames.shoulder_l.forward = shoulder_forward_l;
- pose.body_frames.shoulder_l.radius = upper_arm_r;
- QVector3D shoulder_forward_r =
- QVector3D::crossProduct(right_axis, shoulder_up);
- if (shoulder_forward_r.lengthSquared() < 1e-8F) {
- shoulder_forward_r = forward_axis;
- } else {
- shoulder_forward_r.normalize();
- }
- pose.body_frames.shoulder_r.origin = pose.shoulder_r;
- pose.body_frames.shoulder_r.right = right_axis;
- pose.body_frames.shoulder_r.up = shoulder_up;
- pose.body_frames.shoulder_r.forward = shoulder_forward_r;
- pose.body_frames.shoulder_r.radius = upper_arm_r;
- QVector3D hand_up_l = (pose.hand_l - pose.elbow_l);
- if (hand_up_l.lengthSquared() > 1e-8F) {
- hand_up_l.normalize();
- } else {
- hand_up_l = up_axis;
- }
- QVector3D hand_forward_l = QVector3D::crossProduct(-right_axis, hand_up_l);
- if (hand_forward_l.lengthSquared() < 1e-8F) {
- hand_forward_l = forward_axis;
- } else {
- hand_forward_l.normalize();
- }
- pose.body_frames.hand_l.origin = pose.hand_l;
- pose.body_frames.hand_l.right = -right_axis;
- pose.body_frames.hand_l.up = hand_up_l;
- pose.body_frames.hand_l.forward = hand_forward_l;
- pose.body_frames.hand_l.radius = hand_r;
- QVector3D hand_up_r = (pose.hand_r - pose.elbow_r);
- if (hand_up_r.lengthSquared() > 1e-8F) {
- hand_up_r.normalize();
- } else {
- hand_up_r = up_axis;
- }
- QVector3D hand_forward_r = QVector3D::crossProduct(right_axis, hand_up_r);
- if (hand_forward_r.lengthSquared() < 1e-8F) {
- hand_forward_r = forward_axis;
- } else {
- hand_forward_r.normalize();
- }
- pose.body_frames.hand_r.origin = pose.hand_r;
- pose.body_frames.hand_r.right = right_axis;
- pose.body_frames.hand_r.up = hand_up_r;
- pose.body_frames.hand_r.forward = hand_forward_r;
- pose.body_frames.hand_r.radius = hand_r;
- QVector3D foot_up_l = up_axis;
- QVector3D foot_forward_l = forward_axis - right_axis * 0.12F;
- if (foot_forward_l.lengthSquared() > 1e-8F) {
- foot_forward_l.normalize();
- } else {
- foot_forward_l = forward_axis;
- }
- pose.body_frames.foot_l.origin = pose.foot_l;
- pose.body_frames.foot_l.right = -right_axis;
- pose.body_frames.foot_l.up = foot_up_l;
- pose.body_frames.foot_l.forward = foot_forward_l;
- pose.body_frames.foot_l.radius = foot_radius;
- QVector3D foot_forward_r = forward_axis + right_axis * 0.12F;
- if (foot_forward_r.lengthSquared() > 1e-8F) {
- foot_forward_r.normalize();
- } else {
- foot_forward_r = forward_axis;
- }
- pose.body_frames.foot_r.origin = pose.foot_r;
- pose.body_frames.foot_r.right = right_axis;
- pose.body_frames.foot_r.up = foot_up_l;
- pose.body_frames.foot_r.forward = foot_forward_r;
- pose.body_frames.foot_r.radius = foot_radius;
- auto compute_shin_frame = [&](const QVector3D &ankle, const QVector3D &knee,
- float right_sign) -> AttachmentFrame {
- AttachmentFrame shin{};
- shin.origin = ankle;
- QVector3D shin_dir = knee - ankle;
- float shin_len = shin_dir.length();
- if (shin_len > 1e-6F) {
- shin.up = shin_dir / shin_len;
- } else {
- shin.up = up_axis;
- }
- QVector3D shin_forward = forward_axis;
- shin_forward =
- shin_forward - shin.up * QVector3D::dotProduct(shin_forward, shin.up);
- if (shin_forward.lengthSquared() > 1e-6F) {
- shin_forward.normalize();
- } else {
- shin_forward = forward_axis;
- }
- shin.forward = shin_forward;
- shin.right = QVector3D::crossProduct(shin.up, shin.forward) * right_sign;
- shin.radius = HP::LOWER_LEG_R;
- return shin;
- };
- pose.body_frames.shin_l = compute_shin_frame(pose.foot_l, pose.knee_l, -1.0F);
- pose.body_frames.shin_r = compute_shin_frame(pose.foot_r, pose.knee_r, 1.0F);
- QVector3D const iris = QVector3D(0.10F, 0.10F, 0.12F);
- auto eye_position = [&](float lateral) {
- QVector3D const local(lateral, 0.12F, 0.92F);
- QVector3D world = frame_local_position(pose.body_frames.head, local);
- world +=
- pose.body_frames.head.forward * (pose.body_frames.head.radius * 0.02F);
- return world;
- };
- QVector3D const left_eye_world = eye_position(-0.32F);
- QVector3D const right_eye_world = eye_position(0.32F);
- float const eye_radius = pose.body_frames.head.radius * 0.17F;
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, left_eye_world, eye_radius),
- iris, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, right_eye_world, eye_radius),
- iris, nullptr, 1.0F);
- out.mesh(
- get_unit_cylinder(),
- cylinder_between(ctx.model, pose.shoulder_l, pose.elbow_l, upper_arm_r),
- v.palette.cloth, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.elbow_l, joint_r),
- v.palette.cloth * 0.95F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.elbow_l, pose.hand_l, fore_arm_r),
- v.palette.skin * 0.95F, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.hand_l, hand_r),
- v.palette.leather_dark * 0.92F, nullptr, 1.0F);
- out.mesh(
- get_unit_cylinder(),
- cylinder_between(ctx.model, pose.shoulder_r, pose.elbow_r, upper_arm_r),
- v.palette.cloth, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.elbow_r, joint_r),
- v.palette.cloth * 0.95F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.elbow_r, pose.hand_r, fore_arm_r),
- v.palette.skin * 0.95F, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.hand_r, hand_r),
- v.palette.leather_dark * 0.92F, nullptr, 1.0F);
- QVector3D const hip_l =
- pose.pelvis_pos +
- QVector3D(-HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- QVector3D const hip_r =
- pose.pelvis_pos +
- QVector3D(HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, hip_l, pose.knee_l, thigh_r),
- v.palette.cloth * 0.92F, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.knee_l, leg_joint_r),
- v.palette.cloth * 0.90F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.knee_l, pose.foot_l, shin_r),
- v.palette.leather * 0.95F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, hip_r, pose.knee_r, thigh_r),
- v.palette.cloth * 0.92F, nullptr, 1.0F);
- out.mesh(get_unit_sphere(), sphere_at(ctx.model, pose.knee_r, leg_joint_r),
- v.palette.cloth * 0.90F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.knee_r, pose.foot_r, shin_r),
- v.palette.leather * 0.95F, nullptr, 1.0F);
- auto draw_foot = [&](const QVector3D &ankle, bool is_left) {
- QVector3D lateral = is_left ? -right_axis : right_axis;
- QVector3D foot_forward =
- forward_axis + lateral * (is_left ? -0.12F : 0.12F);
- if (foot_forward.lengthSquared() < 1e-6F) {
- foot_forward = forward_axis;
- }
- foot_forward.normalize();
- float const heel_span = foot_radius * 3.50F;
- float const toe_span = foot_radius * 5.50F;
- float const sole_y = HP::GROUND_Y;
- QVector3D ankle_ground = ankle;
- ankle_ground.setY(sole_y);
- QVector3D heel = ankle_ground - foot_forward * heel_span;
- QVector3D toe = ankle_ground + foot_forward * toe_span;
- heel.setY(sole_y);
- toe.setY(sole_y);
- QMatrix4x4 foot_mat = capsule_between(ctx.model, heel, toe, foot_radius);
- float const width_at_heel = 1.2F;
- float const width_at_toe = 2.5F;
- float const height_scale = 0.26F;
- float const depth_scale = 1.0F;
- QMatrix4x4 scale_mat;
- scale_mat.setToIdentity();
- scale_mat.scale((width_at_heel + width_at_toe) * 0.5F, height_scale,
- depth_scale);
- QMatrix4x4 shear_mat;
- shear_mat.setToIdentity();
- shear_mat(0, 2) = (width_at_toe - width_at_heel) * 0.5F;
- foot_mat = foot_mat * scale_mat * shear_mat;
- out.mesh(get_unit_capsule(), foot_mat, v.palette.leather_dark * 0.92F,
- nullptr, 1.0F);
- };
- draw_foot(pose.foot_l, true);
- draw_foot(pose.foot_r, false);
- draw_armor_overlay(ctx, v, pose, y_top_cover, torso_r, shoulder_half_span,
- upper_arm_r, right_axis, out);
- draw_shoulder_decorations(ctx, v, pose, y_top_cover, pose.neck_base.y(),
- right_axis, out);
- draw_helmet(ctx, v, pose, out);
- }
- void HumanoidRendererBase::draw_armor_overlay(
- const DrawContext &, const HumanoidVariant &, const HumanoidPose &, float,
- float, float, float, const QVector3D &, ISubmitter &) const {}
- void HumanoidRendererBase::draw_armor(const DrawContext &,
- const HumanoidVariant &,
- const HumanoidPose &,
- const HumanoidAnimationContext &,
- ISubmitter &) const {}
- void HumanoidRendererBase::draw_shoulder_decorations(
- const DrawContext &, const HumanoidVariant &, const HumanoidPose &, float,
- float, const QVector3D &, ISubmitter &) const {}
- void HumanoidRendererBase::draw_helmet(const DrawContext &,
- const HumanoidVariant &,
- const HumanoidPose &,
- ISubmitter &) const {}
- void HumanoidRendererBase::draw_facial_hair(const DrawContext &ctx,
- const HumanoidVariant &v,
- const HumanoidPose &pose,
- ISubmitter &out) const {
- const FacialHairParams &fh = v.facial_hair;
- if (fh.style == FacialHairStyle::None || fh.coverage < 0.01F) {
- return;
- }
- const auto &gfx_settings = Render::GraphicsSettings::instance();
- if (!gfx_settings.features().enable_facial_hair) {
- return;
- }
- constexpr float k_facial_hair_max_distance = 12.0F;
- if (ctx.camera != nullptr) {
- QVector3D const soldier_world_pos =
- ctx.model.map(QVector3D(0.0F, 0.0F, 0.0F));
- float const distance =
- (soldier_world_pos - ctx.camera->get_position()).length();
- if (distance > k_facial_hair_max_distance) {
- ++s_render_stats.facial_hair_skipped_distance;
- return;
- }
- }
- const AttachmentFrame &frame = pose.body_frames.head;
- float const head_r = frame.radius;
- if (head_r <= 0.0F) {
- return;
- }
- auto saturate = [](const QVector3D &c) -> QVector3D {
- return {std::clamp(c.x(), 0.0F, 1.0F), std::clamp(c.y(), 0.0F, 1.0F),
- std::clamp(c.z(), 0.0F, 1.0F)};
- };
- QVector3D hair_color = fh.color * (1.0F - fh.greyness) +
- QVector3D(0.75F, 0.75F, 0.75F) * fh.greyness;
- QVector3D hair_dark = hair_color * 0.80F;
- QVector3D const hair_root = hair_dark * 0.95F;
- QVector3D const hair_tip = hair_color * 1.08F;
- float const chin_y = -head_r * 0.95F;
- float const mouth_y = -head_r * 0.18F;
- float const jaw_z = head_r * 0.68F;
- float const chin_norm = chin_y / head_r;
- float const mouth_norm = mouth_y / head_r;
- float const jaw_forward_norm = jaw_z / head_r;
- uint32_t rand_state = 0x9E3779B9U;
- if (ctx.entity != nullptr) {
- uintptr_t ptr = reinterpret_cast<uintptr_t>(ctx.entity);
- rand_state ^= static_cast<uint32_t>(ptr & 0xFFFFFFFFU);
- rand_state ^= static_cast<uint32_t>((ptr >> 32) & 0xFFFFFFFFU);
- }
- rand_state ^= static_cast<uint32_t>(fh.length * 9973.0F);
- rand_state ^= static_cast<uint32_t>(fh.thickness * 6151.0F);
- rand_state ^= static_cast<uint32_t>(fh.coverage * 4099.0F);
- auto random01 = [&]() -> float {
- rand_state = rand_state * 1664525U + 1013904223U;
- return hash_01(rand_state);
- };
- auto jitter = [&](float amplitude) -> float {
- return (random01() - 0.5F) * 2.0F * amplitude;
- };
- float const beard_forward_tilt_norm = 0.32F;
- constexpr int k_max_facial_hair_strands = 24;
- int total_strands_emitted = 0;
- auto place_strands = [&](int rows, int segments, float jaw_span,
- float row_spacing_norm, float base_length_norm,
- float length_variation, float forward_bias_norm,
- float base_radius_norm) {
- if (rows <= 0 || segments <= 0) {
- return;
- }
- const float phi_half_range = std::max(0.35F, jaw_span * 0.5F);
- const float base_y_norm = chin_norm + 0.10F;
- for (int row = 0; row < rows; ++row) {
- float const row_t = (rows > 1) ? float(row) / float(rows - 1) : 0.0F;
- float const target_y_norm =
- std::clamp(base_y_norm + row_t * row_spacing_norm, -0.92F, 0.10F);
- float const plane_radius =
- std::sqrt(std::max(0.02F, 1.0F - target_y_norm * target_y_norm));
- for (int seg = 0; seg < segments; ++seg) {
- float const seg_t =
- (segments > 1) ? float(seg) / float(segments - 1) : 0.5F;
- float const base_phi = (seg_t - 0.5F) * jaw_span;
- float const phi = std::clamp(base_phi + jitter(0.25F), -phi_half_range,
- phi_half_range);
- float const coverage_falloff =
- 1.0F - std::abs(phi) / std::max(0.001F, phi_half_range);
- float const keep_prob = std::clamp(
- fh.coverage * (0.75F + coverage_falloff * 0.35F), 0.05F, 1.0F);
- if (random01() > keep_prob) {
- continue;
- }
- if (total_strands_emitted >= k_max_facial_hair_strands) {
- return;
- }
- float const wrap_scale = 0.80F + (1.0F - row_t) * 0.20F;
- float lateral_norm = plane_radius * std::sin(phi) * wrap_scale;
- float forward_norm = plane_radius * std::cos(phi);
- lateral_norm += jitter(0.06F);
- forward_norm += jitter(0.08F);
- float const y_norm = target_y_norm + jitter(0.05F);
- QVector3D surface_dir(lateral_norm, y_norm,
- forward_norm *
- (0.75F + forward_bias_norm * 0.45F) +
- (forward_bias_norm - 0.05F));
- float const dir_len = surface_dir.length();
- if (dir_len < 1e-4F) {
- continue;
- }
- surface_dir /= dir_len;
- float const shell = 1.02F + jitter(0.03F);
- QVector3D const root = frame_local_position(frame, surface_dir * shell);
- QVector3D local_dir(jitter(0.15F),
- -(0.55F + row_t * 0.30F) + jitter(0.10F),
- forward_bias_norm + beard_forward_tilt_norm +
- row_t * 0.20F + jitter(0.12F));
- QVector3D strand_dir =
- frame.right * local_dir.x() + frame.up * local_dir.y() +
- frame.forward * local_dir.z() - surface_dir * 0.25F;
- if (strand_dir.lengthSquared() < 1e-6F) {
- continue;
- }
- strand_dir.normalize();
- float const strand_length = base_length_norm * fh.length *
- (1.0F + length_variation * jitter(0.5F)) *
- (1.0F + row_t * 0.25F);
- if (strand_length < 0.05F) {
- continue;
- }
- QVector3D const tip = root + strand_dir * (head_r * strand_length);
- float const base_radius =
- std::max(head_r * base_radius_norm * fh.thickness *
- (0.7F + coverage_falloff * 0.35F),
- head_r * 0.010F);
- float const mid_radius = base_radius * 0.55F;
- float const color_jitter = 0.85F + random01() * 0.30F;
- QVector3D const root_color = saturate(hair_root * color_jitter);
- QVector3D const tip_color = saturate(hair_tip * color_jitter);
- QMatrix4x4 base_blob = sphere_at(ctx.model, root, base_radius * 0.95F);
- out.mesh(get_unit_sphere(), base_blob, root_color, nullptr, 1.0F);
- QVector3D const mid = root + (tip - root) * 0.40F;
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, root, mid, base_radius),
- root_color, nullptr, 1.0F);
- out.mesh(get_unit_cone(), cone_from_to(ctx.model, mid, tip, mid_radius),
- tip_color, nullptr, 1.0F);
- ++total_strands_emitted;
- }
- }
- };
- auto place_mustache = [&](int segments, float base_length_norm,
- float upward_bias_norm) {
- if (segments <= 0) {
- return;
- }
- float const mustache_y_norm = mouth_norm + upward_bias_norm - 0.04F;
- float const phi_half_range = 0.55F;
- for (int side = -1; side <= 1; side += 2) {
- for (int seg = 0; seg < segments; ++seg) {
- float const t =
- (segments > 1) ? float(seg) / float(segments - 1) : 0.5F;
- float const base_phi = (t - 0.5F) * (phi_half_range * 2.0F);
- float const phi = std::clamp(base_phi + jitter(0.18F), -phi_half_range,
- phi_half_range);
- float const plane_radius = std::sqrt(
- std::max(0.02F, 1.0F - mustache_y_norm * mustache_y_norm));
- float lateral_norm = plane_radius * std::sin(phi);
- float forward_norm = plane_radius * std::cos(phi);
- lateral_norm += jitter(0.04F);
- forward_norm += jitter(0.05F);
- if (random01() > fh.coverage) {
- continue;
- }
- if (total_strands_emitted >= k_max_facial_hair_strands) {
- return;
- }
- QVector3D surface_dir(lateral_norm, mustache_y_norm + jitter(0.03F),
- forward_norm * 0.85F + 0.20F);
- float const dir_len = surface_dir.length();
- if (dir_len < 1e-4F) {
- continue;
- }
- surface_dir /= dir_len;
- QVector3D const root =
- frame_local_position(frame, surface_dir * (1.01F + jitter(0.02F)));
- QVector3D const dir_local(side * (0.55F + jitter(0.12F)), jitter(0.06F),
- 0.34F + jitter(0.08F));
- QVector3D strand_dir =
- frame.right * dir_local.x() + frame.up * dir_local.y() +
- frame.forward * dir_local.z() - surface_dir * 0.20F;
- if (strand_dir.lengthSquared() < 1e-6F) {
- continue;
- }
- strand_dir.normalize();
- float const strand_length =
- base_length_norm * fh.length * (1.0F + jitter(0.35F));
- QVector3D const tip = root + strand_dir * (head_r * strand_length);
- float const base_radius =
- std::max(head_r * 0.028F * fh.thickness, head_r * 0.0065F);
- float const mid_radius = base_radius * 0.45F;
- float const color_jitter = 0.92F + random01() * 0.18F;
- QVector3D const root_color =
- saturate(hair_root * (color_jitter * 0.95F));
- QVector3D const tip_color = saturate(hair_tip * (color_jitter * 1.02F));
- out.mesh(get_unit_sphere(),
- sphere_at(ctx.model, root, base_radius * 0.7F), root_color,
- nullptr, 1.0F);
- QVector3D const mid = root + (tip - root) * 0.5F;
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, root, mid, base_radius * 0.85F),
- root_color, nullptr, 1.0F);
- out.mesh(get_unit_cone(), cone_from_to(ctx.model, mid, tip, mid_radius),
- tip_color, nullptr, 1.0F);
- ++total_strands_emitted;
- }
- }
- };
- switch (fh.style) {
- case FacialHairStyle::Stubble: {
- place_strands(1, 11, 2.0F, 0.12F, 0.28F, 0.30F, 0.08F, 0.035F);
- break;
- }
- case FacialHairStyle::ShortBeard: {
- place_strands(3, 14, 2.6F, 0.18F, 0.58F, 0.38F, 0.12F, 0.055F);
- break;
- }
- case FacialHairStyle::FullBeard:
- case FacialHairStyle::LongBeard: {
- bool const is_long = (fh.style == FacialHairStyle::LongBeard);
- if (is_long) {
- place_strands(5, 20, 3.0F, 0.24F, 1.00F, 0.48F, 0.18F, 0.060F);
- } else {
- place_strands(4, 18, 2.8F, 0.22F, 0.85F, 0.42F, 0.16F, 0.058F);
- }
- break;
- }
- case FacialHairStyle::Goatee: {
- place_strands(2, 8, 1.8F, 0.16F, 0.70F, 0.34F, 0.14F, 0.055F);
- break;
- }
- case FacialHairStyle::Mustache: {
- place_mustache(5, 0.32F, 0.05F);
- break;
- }
- case FacialHairStyle::MustacheAndBeard: {
- FacialHairParams mustache_only = fh;
- mustache_only.style = FacialHairStyle::Mustache;
- FacialHairParams beard_only = fh;
- beard_only.style = FacialHairStyle::ShortBeard;
- HumanoidVariant v_mustache = v;
- v_mustache.facial_hair = mustache_only;
- draw_facial_hair(ctx, v_mustache, pose, out);
- HumanoidVariant v_beard = v;
- v_beard.facial_hair = beard_only;
- draw_facial_hair(ctx, v_beard, pose, out);
- break;
- }
- case FacialHairStyle::None:
- default:
- break;
- }
- }
- void HumanoidRendererBase::draw_simplified_body(const DrawContext &ctx,
- const HumanoidVariant &v,
- HumanoidPose &pose,
- ISubmitter &out) const {
- using HP = HumanProportions;
- QVector3D const scaling = get_proportion_scaling();
- float const width_scale = scaling.x();
- float const height_scale = scaling.y();
- float const torso_scale = get_torso_scale();
- QVector3D right_axis = pose.shoulder_r - pose.shoulder_l;
- right_axis.setY(0.0F);
- right_axis.setZ(0.0F);
- if (right_axis.lengthSquared() < HP::EPSILON_VECTOR) {
- right_axis = QVector3D(1.0F, 0.0F, 0.0F);
- }
- right_axis.normalize();
- if (right_axis.x() < 0.0F) {
- right_axis = -right_axis;
- }
- QVector3D const up_axis(0.0F, 1.0F, 0.0F);
- QVector3D forward_axis = QVector3D::crossProduct(right_axis, up_axis);
- if (forward_axis.lengthSquared() < HP::EPSILON_VECTOR) {
- forward_axis = QVector3D(0.0F, 0.0F, 1.0F);
- }
- forward_axis.normalize();
- QVector3D const shoulder_mid = (pose.shoulder_l + pose.shoulder_r) * 0.5F;
- const float y_shoulder = shoulder_mid.y();
- const float y_neck = pose.neck_base.y();
- const float shoulder_half_span =
- 0.5F * std::abs(pose.shoulder_r.x() - pose.shoulder_l.x());
- const float torso_r_base =
- std::max(HP::TORSO_TOP_R, shoulder_half_span * 0.95F);
- const float torso_r = torso_r_base * torso_scale;
- float const depth_scale = scaling.z();
- const float torso_depth_factor =
- std::clamp(HP::TORSO_DEPTH_FACTOR_BASE + (depth_scale - 1.0F) * 0.20F,
- HP::TORSO_DEPTH_FACTOR_MIN, HP::TORSO_DEPTH_FACTOR_MAX);
- float torso_depth = torso_r * torso_depth_factor;
- const float y_top_cover =
- std::max(y_shoulder + 0.00F, y_neck + HP::TORSO_TOP_COVER_OFFSET);
- const float upper_arm_r = HP::UPPER_ARM_R * width_scale;
- const float fore_arm_r = HP::FORE_ARM_R * width_scale;
- const float thigh_r = HP::UPPER_LEG_R * width_scale;
- const float shin_r = HP::LOWER_LEG_R * width_scale;
- float const torso_x = (shoulder_mid.x() + pose.pelvis_pos.x()) * 0.5F;
- constexpr float torso_z = 0.0F;
- QVector3D const tunic_top{torso_x, y_top_cover - 0.006F, torso_z};
- QVector3D const tunic_bot{torso_x, pose.pelvis_pos.y() - 0.05F, torso_z};
- QMatrix4x4 torso_transform =
- cylinder_between(ctx.model, tunic_top, tunic_bot, 1.0F);
- torso_transform.scale(torso_r, 1.0F, torso_depth);
- Mesh *torso_mesh = torso_mesh_without_bottom_cap();
- if (torso_mesh != nullptr) {
- out.mesh(torso_mesh, torso_transform, v.palette.cloth, nullptr, 1.0F);
- }
- float const head_r = pose.head_r;
- QMatrix4x4 head_transform = ctx.model;
- head_transform.translate(pose.head_pos);
- head_transform.scale(head_r);
- out.mesh(get_unit_sphere(), head_transform, v.palette.skin, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.shoulder_l, pose.hand_l,
- (upper_arm_r + fore_arm_r) * 0.5F),
- v.palette.cloth, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, pose.shoulder_r, pose.hand_r,
- (upper_arm_r + fore_arm_r) * 0.5F),
- v.palette.cloth, nullptr, 1.0F);
- QVector3D const hip_l =
- pose.pelvis_pos +
- QVector3D(-HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- QVector3D const hip_r =
- pose.pelvis_pos +
- QVector3D(HP::HIP_LATERAL_OFFSET, HP::HIP_VERTICAL_OFFSET, 0.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, hip_l, pose.foot_l,
- (thigh_r + shin_r) * 0.5F),
- v.palette.cloth * 0.92F, nullptr, 1.0F);
- out.mesh(get_unit_cylinder(),
- cylinder_between(ctx.model, hip_r, pose.foot_r,
- (thigh_r + shin_r) * 0.5F),
- v.palette.cloth * 0.92F, nullptr, 1.0F);
- }
- void HumanoidRendererBase::draw_minimal_body(const DrawContext &ctx,
- const HumanoidVariant &v,
- const HumanoidPose &pose,
- ISubmitter &out) const {
- using HP = HumanProportions;
- QVector3D const top = pose.head_pos + QVector3D(0.0F, pose.head_r, 0.0F);
- QVector3D const bot = (pose.foot_l + pose.foot_r) * 0.5F;
- float const body_radius = HP::TORSO_TOP_R * get_torso_scale();
- out.mesh(get_unit_capsule(),
- capsule_between(ctx.model, top, bot, body_radius), v.palette.cloth,
- nullptr, 1.0F);
- }
- namespace {
- auto resolve_renderer_for_submitter(ISubmitter &out) -> Renderer * {
- if (auto *renderer = dynamic_cast<Renderer *>(&out)) {
- return renderer;
- }
- if (auto *batch = dynamic_cast<BatchingSubmitter *>(&out)) {
- return dynamic_cast<Renderer *>(batch->fallback_submitter());
- }
- return nullptr;
- }
- } // namespace
- void HumanoidRendererBase::render(const DrawContext &ctx,
- ISubmitter &out) const {
- AnimationInputs anim = sample_anim_state(ctx);
- if (ctx.template_prewarm) {
- if (ctx.renderer_id.empty() || ctx.animation_override == nullptr ||
- !ctx.force_humanoid_lod || !ctx.has_variant_override) {
- return;
- }
- auto *unit_comp =
- ctx.entity ? ctx.entity->get_component<Engine::Core::UnitComponent>()
- : nullptr;
- std::uint32_t owner_id =
- (unit_comp != nullptr) ? static_cast<std::uint32_t>(unit_comp->owner_id)
- : 0U;
- bool is_mounted_spawn = false;
- if (unit_comp != nullptr) {
- using Game::Units::SpawnType;
- auto const st = unit_comp->spawn_type;
- is_mounted_spawn =
- (st == SpawnType::MountedKnight || st == SpawnType::HorseArcher ||
- st == SpawnType::HorseSpearman);
- }
- std::uint8_t attack_variant = 0;
- if (ctx.has_attack_variant_override) {
- attack_variant = ctx.attack_variant_override;
- }
- AnimKey anim_key =
- make_anim_key(*ctx.animation_override, 0.0F, attack_variant);
- TemplateKey key;
- key.renderer_id = ctx.renderer_id;
- key.owner_id = owner_id;
- key.lod = static_cast<std::uint8_t>(ctx.forced_humanoid_lod);
- HorseLOD mount_lod = HorseLOD::Full;
- key.mount_lod = 0;
- if (is_mounted_spawn) {
- mount_lod = ctx.force_horse_lod
- ? ctx.forced_horse_lod
- : static_cast<HorseLOD>(ctx.forced_humanoid_lod);
- key.mount_lod = static_cast<std::uint8_t>(mount_lod);
- }
- key.variant = ctx.variant_override;
- key.attack_variant = anim_key.attack_variant;
- key.state = anim_key.state;
- key.combat_phase = anim_key.combat_phase;
- key.frame = anim_key.frame;
- const TemplateCache::DenseDomainHandle dense_domain =
- TemplateCache::instance().get_dense_domain_handle(
- key.renderer_id, key.owner_id, key.lod, key.mount_lod);
- const std::size_t dense_slot =
- TemplateCache::dense_slot_index(key.variant, anim_key);
- (void)TemplateCache::instance().get_or_build_dense(
- dense_domain, dense_slot, key, [&]() -> PoseTemplate {
- thread_local TemplateRecorder recorder;
- recorder.reset(320);
- recorder.set_current_shader(nullptr);
- if (auto *outer = dynamic_cast<Renderer *>(&out)) {
- recorder.set_current_shader(outer->get_current_shader());
- }
- Engine::Core::Entity template_entity(1);
- auto *templ_unit =
- template_entity.add_component<Engine::Core::UnitComponent>();
- if (unit_comp != nullptr) {
- *templ_unit = *unit_comp;
- }
- templ_unit->max_health =
- (templ_unit->max_health > 0) ? templ_unit->max_health : 1;
- templ_unit->health = templ_unit->max_health;
- auto *templ_transform =
- template_entity.add_component<Engine::Core::TransformComponent>();
- templ_transform->position = {0.0F, 0.0F, 0.0F};
- templ_transform->rotation = {0.0F, 0.0F, 0.0F};
- templ_transform->scale = {1.0F, 1.0F, 1.0F};
- if (auto *renderable =
- ctx.entity
- ? ctx.entity
- ->get_component<Engine::Core::RenderableComponent>()
- : nullptr) {
- auto *templ_renderable =
- template_entity
- .add_component<Engine::Core::RenderableComponent>(
- renderable->mesh_path, renderable->texture_path);
- templ_renderable->renderer_id = renderable->renderer_id;
- templ_renderable->mesh = renderable->mesh;
- templ_renderable->visible = true;
- templ_renderable->color = renderable->color;
- }
- DrawContext build_ctx = ctx;
- build_ctx.entity = &template_entity;
- build_ctx.world = nullptr;
- build_ctx.model = QMatrix4x4();
- build_ctx.camera = nullptr;
- build_ctx.selected = false;
- build_ctx.hovered = false;
- build_ctx.allow_template_cache = false;
- build_ctx.force_humanoid_lod = true;
- build_ctx.forced_humanoid_lod = ctx.forced_humanoid_lod;
- build_ctx.force_horse_lod = is_mounted_spawn;
- if (is_mounted_spawn) {
- build_ctx.forced_horse_lod = static_cast<HorseLOD>(key.mount_lod);
- }
- build_ctx.has_seed_override = true;
- build_ctx.seed_override =
- resolve_variant_seed(unit_comp, key.variant);
- build_ctx.has_attack_variant_override = true;
- build_ctx.attack_variant_override = key.attack_variant;
- build_ctx.force_single_soldier = true;
- build_ctx.skip_ground_offset = true;
- AnimationInputs build_anim = make_animation_inputs(anim_key);
- build_ctx.animation_override = &build_anim;
- render_procedural(build_ctx, build_anim, recorder);
- PoseTemplate built;
- built.commands = recorder.take_commands();
- return built;
- });
- return;
- }
- if (!ctx.allow_template_cache || ctx.renderer_id.empty() ||
- ctx.entity == nullptr) {
- render_procedural(ctx, anim, out);
- return;
- }
- auto *unit_comp = ctx.entity->get_component<Engine::Core::UnitComponent>();
- auto *transform_comp =
- ctx.entity->get_component<Engine::Core::TransformComponent>();
- if (unit_comp == nullptr || transform_comp == nullptr) {
- render_procedural(ctx, anim, out);
- return;
- }
- FormationParams const formation = resolve_formation(ctx);
- const int rows =
- (formation.individuals_per_unit + formation.max_per_row - 1) /
- formation.max_per_row;
- int cols = formation.max_per_row;
- int effective_rows = rows;
- if (ctx.force_single_soldier) {
- cols = 1;
- effective_rows = 1;
- }
- int visible_count = rows * cols;
- int max_health = std::max(1, unit_comp->max_health);
- float ratio = std::clamp(unit_comp->health / float(max_health), 0.0F, 1.0F);
- visible_count = std::max(1, (int)std::ceil(ratio * float(rows * cols)));
- if (visible_count <= 0) {
- return;
- }
- s_render_stats.soldiers_total += visible_count;
- bool is_mounted_spawn = false;
- if (unit_comp != nullptr) {
- using Game::Units::SpawnType;
- auto const st = unit_comp->spawn_type;
- is_mounted_spawn =
- (st == SpawnType::MountedKnight || st == SpawnType::HorseArcher ||
- st == SpawnType::HorseSpearman);
- }
- FormationCalculatorFactory::Nation nation =
- FormationCalculatorFactory::Nation::Roman;
- if (unit_comp != nullptr &&
- unit_comp->nation_id == Game::Systems::NationID::Carthage) {
- nation = FormationCalculatorFactory::Nation::Carthage;
- }
- FormationCalculatorFactory::UnitCategory category =
- resolve_formation_category(unit_comp, anim);
- std::uint32_t layout_seed = resolve_layout_seed(unit_comp, ctx.entity);
- auto &layout_entry = get_or_build_unit_layout(
- reinterpret_cast<std::uintptr_t>(ctx.entity), formation, nation, category,
- layout_seed, rows, cols);
- float entity_ground_offset =
- resolve_entity_ground_offset(ctx, unit_comp, transform_comp);
- Renderer *renderer = resolve_renderer_for_submitter(out);
- Shader *last_shader = nullptr;
- const auto &gfx_settings = Render::GraphicsSettings::instance();
- const float base_pos_x = transform_comp->position.x;
- const float base_pos_y = transform_comp->position.y;
- const float base_pos_z = transform_comp->position.z;
- const float scale_x = transform_comp->scale.x;
- const float scale_y = transform_comp->scale.y;
- const float scale_z = transform_comp->scale.z;
- const float base_yaw = transform_comp->rotation.y;
- const float ground_offset_scaled = entity_ground_offset * scale_y;
- const bool has_camera = (ctx.camera != nullptr);
- const QVector3D camera_position =
- has_camera ? ctx.camera->get_position() : QVector3D();
- auto &terrain_service = Game::Map::TerrainService::instance();
- const bool terrain_initialized = terrain_service.is_initialized();
- QVector2D shadow_dir_for_use(0.0F, 1.0F);
- {
- QVector2D light_dir_xz(k_shadow_light_dir.x(), k_shadow_light_dir.z());
- if (light_dir_xz.lengthSquared() >= 1e-6F) {
- light_dir_xz.normalize();
- shadow_dir_for_use = -light_dir_xz;
- if (shadow_dir_for_use.lengthSquared() >= 1e-6F) {
- shadow_dir_for_use.normalize();
- } else {
- shadow_dir_for_use = QVector2D(0.0F, 1.0F);
- }
- }
- }
- Shader *shadow_shader = nullptr;
- Mesh *shadow_quad_mesh = nullptr;
- const bool can_render_shadow_mesh =
- gfx_settings.shadows_enabled() && ctx.backend != nullptr &&
- ctx.resources != nullptr && terrain_initialized;
- if (can_render_shadow_mesh) {
- shadow_shader = ctx.backend->shader(QStringLiteral("troop_shadow"));
- shadow_quad_mesh = ctx.resources->quad();
- }
- auto fast_random = [](uint32_t &state) -> float {
- state = state * 1664525U + 1013904223U;
- return float(state & 0x7FFFFFU) / float(0x7FFFFFU);
- };
- const std::size_t soldier_count = std::min<std::size_t>(
- layout_entry.soldiers.size(), static_cast<std::size_t>(visible_count));
- const std::uint32_t template_owner_id =
- static_cast<std::uint32_t>(unit_comp->owner_id);
- std::array<std::array<TemplateCache::DenseDomainHandle, 4>, 4>
- dense_domains{};
- std::array<std::array<bool, 4>, 4> dense_domains_ready{};
- auto dense_domain_for =
- [&](HumanoidLOD lod,
- HorseLOD mount_lod_val) -> TemplateCache::DenseDomainHandle {
- const std::size_t lod_idx =
- std::min<std::size_t>(static_cast<std::size_t>(lod), 3);
- const std::size_t mount_idx =
- std::min<std::size_t>(static_cast<std::size_t>(mount_lod_val), 3);
- if (!dense_domains_ready[lod_idx][mount_idx]) {
- const std::uint8_t lod_u8 = static_cast<std::uint8_t>(lod_idx);
- const std::uint8_t mount_u8 = static_cast<std::uint8_t>(mount_idx);
- dense_domains[lod_idx][mount_idx] =
- TemplateCache::instance().get_dense_domain_handle(
- ctx.renderer_id, template_owner_id, lod_u8, mount_u8);
- dense_domains_ready[lod_idx][mount_idx] = true;
- }
- return dense_domains[lod_idx][mount_idx];
- };
- for (std::size_t idx = 0; idx < soldier_count; ++idx) {
- const SoldierLayout &slot = layout_entry.soldiers[idx];
- float offset_x = slot.offset_x;
- float offset_z = slot.offset_z;
- float applied_vertical_jitter = slot.vertical_jitter;
- float applied_yaw_offset = slot.yaw_offset;
- float phase_offset = slot.phase_offset;
- uint32_t rng_state = slot.inst_seed;
- if (anim.is_attacking && anim.is_melee) {
- float const combat_jitter_x =
- (fast_random(rng_state) - 0.5F) * formation.spacing * 0.4F;
- float const combat_jitter_z =
- (fast_random(rng_state) - 0.5F) * formation.spacing * 0.3F;
- float const sway_time = anim.time + phase_offset * 2.0F;
- float const sway_x = std::sin(sway_time * 1.5F) * 0.05F;
- float const sway_z = std::cos(sway_time * 1.2F) * 0.04F;
- offset_x += combat_jitter_x + sway_x;
- offset_z += combat_jitter_z + sway_z;
- float const combat_yaw = (fast_random(rng_state) - 0.5F) * 15.0F;
- applied_yaw_offset += combat_yaw;
- }
- const float applied_yaw = base_yaw + applied_yaw_offset;
- const float yaw_rad = qDegreesToRadians(applied_yaw);
- const float cos_yaw = std::cos(yaw_rad);
- const float sin_yaw = std::sin(yaw_rad);
- const float local_x = offset_x * scale_x;
- const float local_z = offset_z * scale_z;
- QVector3D const soldier_world_pos(
- base_pos_x + (cos_yaw * local_x) + (sin_yaw * local_z),
- base_pos_y + (applied_vertical_jitter * scale_y) - ground_offset_scaled,
- base_pos_z - (sin_yaw * local_x) + (cos_yaw * local_z));
- constexpr float k_soldier_cull_radius = 0.6F;
- if (has_camera &&
- !ctx.camera->is_in_frustum(soldier_world_pos, k_soldier_cull_radius)) {
- ++s_render_stats.soldiers_skipped_frustum;
- continue;
- }
- HumanoidLOD soldier_lod = HumanoidLOD::Full;
- float soldier_distance = 0.0F;
- if (ctx.force_humanoid_lod) {
- soldier_lod = ctx.forced_humanoid_lod;
- } else if (has_camera) {
- soldier_distance = (soldier_world_pos - camera_position).length();
- soldier_lod = calculate_humanoid_lod(soldier_distance);
- if (soldier_lod == HumanoidLOD::Billboard) {
- ++s_render_stats.soldiers_skipped_lod;
- continue;
- }
- soldier_lod =
- Render::VisibilityBudgetTracker::instance().request_humanoid_lod(
- soldier_lod);
- }
- if (soldier_distance > 0.0F) {
- if (soldier_lod == HumanoidLOD::Reduced &&
- soldier_distance > k_temporal_skip_distance_reduced) {
- if (!should_render_temporal(s_current_frame, slot.inst_seed,
- k_temporal_skip_period_reduced)) {
- ++s_render_stats.soldiers_skipped_temporal;
- continue;
- }
- }
- if (soldier_lod == HumanoidLOD::Minimal &&
- soldier_distance > k_temporal_skip_distance_minimal) {
- if (!should_render_temporal(s_current_frame, slot.inst_seed,
- k_temporal_skip_period_minimal)) {
- ++s_render_stats.soldiers_skipped_temporal;
- continue;
- }
- }
- }
- ++s_render_stats.soldiers_rendered;
- switch (soldier_lod) {
- case HumanoidLOD::Full:
- ++s_render_stats.lod_full;
- break;
- case HumanoidLOD::Reduced:
- ++s_render_stats.lod_reduced;
- break;
- case HumanoidLOD::Minimal:
- ++s_render_stats.lod_minimal;
- break;
- case HumanoidLOD::Billboard:
- break;
- }
- QMatrix4x4 inst_model = make_yaw_scale_model(
- cos_yaw, sin_yaw, scale_x, scale_y, scale_z, soldier_world_pos);
- std::uint8_t variant_key = resolve_variant_key_from_seed(slot.inst_seed);
- std::uint8_t attack_variant =
- resolve_attack_variant_from_seed(slot.inst_seed);
- AnimKey anim_key = make_anim_key(anim, phase_offset, attack_variant);
- HorseLOD mount_lod = HorseLOD::Full;
- if (is_mounted_spawn) {
- if (ctx.force_horse_lod) {
- mount_lod = ctx.forced_horse_lod;
- } else {
- mount_lod = static_cast<HorseLOD>(soldier_lod);
- }
- }
- TemplateKey key;
- key.renderer_id = ctx.renderer_id;
- key.owner_id = template_owner_id;
- key.lod = static_cast<std::uint8_t>(soldier_lod);
- key.mount_lod = is_mounted_spawn ? static_cast<std::uint8_t>(mount_lod) : 0;
- key.variant = variant_key;
- key.attack_variant = anim_key.attack_variant;
- key.state = anim_key.state;
- key.combat_phase = anim_key.combat_phase;
- key.frame = anim_key.frame;
- const TemplateCache::DenseDomainHandle dense_domain = dense_domain_for(
- soldier_lod, is_mounted_spawn ? mount_lod : HorseLOD::Full);
- const std::size_t dense_slot =
- TemplateCache::dense_slot_index(variant_key, anim_key);
- const PoseTemplate *tpl = TemplateCache::instance().get_or_build_dense(
- dense_domain, dense_slot, key, [&]() -> PoseTemplate {
- thread_local TemplateRecorder recorder;
- recorder.reset(320);
- recorder.set_current_shader(nullptr);
- if (auto *outer = dynamic_cast<Renderer *>(&out)) {
- recorder.set_current_shader(outer->get_current_shader());
- }
- Engine::Core::Entity template_entity(1);
- auto *templ_unit =
- template_entity.add_component<Engine::Core::UnitComponent>();
- *templ_unit = *unit_comp;
- templ_unit->max_health =
- (templ_unit->max_health > 0) ? templ_unit->max_health : 1;
- templ_unit->health = templ_unit->max_health;
- auto *templ_transform =
- template_entity.add_component<Engine::Core::TransformComponent>();
- templ_transform->position = {0.0F, 0.0F, 0.0F};
- templ_transform->rotation = {0.0F, 0.0F, 0.0F};
- templ_transform->scale = {1.0F, 1.0F, 1.0F};
- if (auto *renderable =
- ctx.entity
- ->get_component<Engine::Core::RenderableComponent>()) {
- auto *templ_renderable =
- template_entity
- .add_component<Engine::Core::RenderableComponent>(
- renderable->mesh_path, renderable->texture_path);
- templ_renderable->renderer_id = renderable->renderer_id;
- templ_renderable->mesh = renderable->mesh;
- templ_renderable->visible = true;
- templ_renderable->color = renderable->color;
- }
- DrawContext build_ctx = ctx;
- build_ctx.entity = &template_entity;
- build_ctx.world = nullptr;
- build_ctx.model = QMatrix4x4();
- build_ctx.camera = nullptr;
- build_ctx.selected = false;
- build_ctx.hovered = false;
- build_ctx.allow_template_cache = false;
- build_ctx.force_humanoid_lod = true;
- build_ctx.forced_humanoid_lod = soldier_lod;
- build_ctx.force_horse_lod = is_mounted_spawn;
- if (is_mounted_spawn) {
- build_ctx.forced_horse_lod = mount_lod;
- }
- build_ctx.has_seed_override = true;
- build_ctx.seed_override =
- resolve_variant_seed(unit_comp, variant_key);
- build_ctx.has_attack_variant_override = true;
- build_ctx.attack_variant_override = anim_key.attack_variant;
- build_ctx.force_single_soldier = true;
- build_ctx.skip_ground_offset = true;
- AnimationInputs build_anim = make_animation_inputs(anim_key);
- build_ctx.animation_override = &build_anim;
- render_procedural(build_ctx, build_anim, recorder);
- PoseTemplate built;
- built.commands = recorder.take_commands();
- return built;
- });
- if (tpl == nullptr || tpl->commands.empty()) {
- render_procedural(ctx, anim, out);
- return;
- }
- for (const auto &cmd : tpl->commands) {
- if (renderer != nullptr && cmd.shader != last_shader) {
- renderer->set_current_shader(cmd.shader);
- last_shader = cmd.shader;
- }
- QMatrix4x4 world_model =
- Render::Geom::multiply_affine(inst_model, cmd.local_model);
- out.mesh(cmd.mesh, world_model, cmd.color, cmd.texture, cmd.alpha,
- cmd.material_id);
- }
- const bool should_render_shadow =
- (shadow_shader != nullptr) && (shadow_quad_mesh != nullptr) &&
- (soldier_lod == HumanoidLOD::Full ||
- soldier_lod == HumanoidLOD::Reduced) &&
- soldier_distance < gfx_settings.shadow_max_distance();
- if (should_render_shadow) {
- float const shadow_size =
- is_mounted_spawn ? k_shadow_size_mounted : k_shadow_size_infantry;
- float depth_boost = 1.0F;
- float width_boost = 1.0F;
- using Game::Units::SpawnType;
- switch (unit_comp->spawn_type) {
- case SpawnType::Spearman:
- depth_boost = 1.8F;
- width_boost = 0.95F;
- break;
- case SpawnType::HorseSpearman:
- depth_boost = 2.1F;
- width_boost = 1.05F;
- break;
- case SpawnType::Archer:
- case SpawnType::HorseArcher:
- depth_boost = 1.2F;
- width_boost = 0.95F;
- break;
- default:
- break;
- }
- float const shadow_width =
- shadow_size * (is_mounted_spawn ? 1.05F : 1.0F) * width_boost;
- float const shadow_depth =
- shadow_size * (is_mounted_spawn ? 1.30F : 1.10F) * depth_boost;
- QVector3D const inst_pos = soldier_world_pos;
- float const shadow_y =
- terrain_service.get_terrain_height(inst_pos.x(), inst_pos.z());
- float const shadow_offset = shadow_depth * 1.25F;
- QVector2D const offset_2d = shadow_dir_for_use * shadow_offset;
- float const light_yaw_deg = qRadiansToDegrees(std::atan2(
- double(shadow_dir_for_use.x()), double(shadow_dir_for_use.y())));
- QMatrix4x4 shadow_model;
- shadow_model.translate(inst_pos.x() + offset_2d.x(),
- shadow_y + k_shadow_ground_offset,
- inst_pos.z() + offset_2d.y());
- shadow_model.rotate(light_yaw_deg, 0.0F, 1.0F, 0.0F);
- shadow_model.rotate(-90.0F, 1.0F, 0.0F, 0.0F);
- shadow_model.scale(shadow_width, shadow_depth, 1.0F);
- if (renderer != nullptr) {
- Shader *previous_shader = renderer->get_current_shader();
- renderer->set_current_shader(shadow_shader);
- shadow_shader->set_uniform(QStringLiteral("u_lightDir"),
- shadow_dir_for_use);
- out.mesh(shadow_quad_mesh, shadow_model, QVector3D(0.0F, 0.0F, 0.0F),
- nullptr, k_shadow_base_alpha, 0);
- renderer->set_current_shader(previous_shader);
- last_shader = previous_shader;
- }
- }
- }
- if (renderer != nullptr) {
- renderer->set_current_shader(nullptr);
- }
- }
- void HumanoidRendererBase::render_procedural(const DrawContext &ctx,
- const AnimationInputs &anim,
- ISubmitter &out) const {
- FormationParams const formation = resolve_formation(ctx);
- Engine::Core::UnitComponent *unit_comp = nullptr;
- if (ctx.entity != nullptr) {
- unit_comp = ctx.entity->get_component<Engine::Core::UnitComponent>();
- }
- Engine::Core::MovementComponent *movement_comp = nullptr;
- Engine::Core::TransformComponent *transform_comp = nullptr;
- if (ctx.entity != nullptr) {
- movement_comp =
- ctx.entity->get_component<Engine::Core::MovementComponent>();
- transform_comp =
- ctx.entity->get_component<Engine::Core::TransformComponent>();
- }
- float entity_ground_offset =
- resolve_entity_ground_offset(ctx, unit_comp, transform_comp);
- uint32_t seed = 0U;
- if (ctx.has_seed_override) {
- seed = ctx.seed_override;
- } else {
- if (unit_comp != nullptr) {
- seed ^= uint32_t(unit_comp->owner_id * 2654435761U);
- }
- if (ctx.entity != nullptr) {
- seed ^= uint32_t(reinterpret_cast<uintptr_t>(ctx.entity) & 0xFFFFFFFFU);
- }
- }
- const int rows =
- (formation.individuals_per_unit + formation.max_per_row - 1) /
- formation.max_per_row;
- int cols = formation.max_per_row;
- int effective_rows = rows;
- if (ctx.force_single_soldier) {
- cols = 1;
- effective_rows = 1;
- }
- bool is_mounted_spawn = false;
- if (unit_comp != nullptr) {
- using Game::Units::SpawnType;
- auto const st = unit_comp->spawn_type;
- is_mounted_spawn =
- (st == SpawnType::MountedKnight || st == SpawnType::HorseArcher ||
- st == SpawnType::HorseSpearman);
- }
- int visible_count = effective_rows * cols;
- if (!ctx.force_single_soldier && unit_comp != nullptr) {
- int const mh = std::max(1, unit_comp->max_health);
- float const ratio = std::clamp(unit_comp->health / float(mh), 0.0F, 1.0F);
- visible_count = std::max(1, (int)std::ceil(ratio * float(rows * cols)));
- }
- HumanoidVariant variant;
- get_variant(ctx, seed, variant);
- if (!m_proportion_scale_cached) {
- m_cached_proportion_scale = get_proportion_scaling();
- m_proportion_scale_cached = true;
- }
- const QVector3D prop_scale = m_cached_proportion_scale;
- const float height_scale = prop_scale.y();
- const bool needs_height_scaling = std::abs(height_scale - 1.0F) > 0.001F;
- const QMatrix4x4 k_identity_matrix;
- const IFormationCalculator *formation_calculator = nullptr;
- {
- using Nation = FormationCalculatorFactory::Nation;
- using UnitCategory = FormationCalculatorFactory::UnitCategory;
- Nation nation = Nation::Roman;
- if (unit_comp != nullptr) {
- if (unit_comp->nation_id == Game::Systems::NationID::Carthage) {
- nation = Nation::Carthage;
- }
- }
- UnitCategory category =
- is_mounted_spawn ? UnitCategory::Cavalry : UnitCategory::Infantry;
- if (unit_comp != nullptr &&
- unit_comp->spawn_type == Game::Units::SpawnType::Builder &&
- anim.is_constructing) {
- category = UnitCategory::BuilderConstruction;
- }
- formation_calculator =
- FormationCalculatorFactory::get_calculator(nation, category);
- }
- auto fast_random = [](uint32_t &state) -> float {
- state = state * 1664525U + 1013904223U;
- return float(state & 0x7FFFFFU) / float(0x7FFFFFU);
- };
- s_render_stats.soldiers_total += visible_count;
- for (int idx = 0; idx < visible_count; ++idx) {
- int const r = idx / cols;
- int const c = idx % cols;
- float offset_x = 0.0F;
- float offset_z = 0.0F;
- uint32_t inst_seed = seed ^ uint32_t(idx * 9176U);
- uint32_t rng_state = inst_seed;
- float vertical_jitter = 0.0F;
- float yaw_offset = 0.0F;
- float phase_offset = 0.0F;
- if (!ctx.force_single_soldier) {
- FormationOffset const formation_offset =
- formation_calculator->calculate_offset(idx, r, c, rows, cols,
- formation.spacing, seed);
- offset_x = formation_offset.offset_x;
- offset_z = formation_offset.offset_z;
- vertical_jitter = (fast_random(rng_state) - 0.5F) * 0.03F;
- yaw_offset = (fast_random(rng_state) - 0.5F) * 5.0F;
- phase_offset = fast_random(rng_state) * 0.25F;
- }
- if (anim.is_attacking && anim.is_melee) {
- float const combat_jitter_x =
- (fast_random(rng_state) - 0.5F) * formation.spacing * 0.4F;
- float const combat_jitter_z =
- (fast_random(rng_state) - 0.5F) * formation.spacing * 0.3F;
- float const sway_time = anim.time + phase_offset * 2.0F;
- float const sway_x = std::sin(sway_time * 1.5F) * 0.05F;
- float const sway_z = std::cos(sway_time * 1.2F) * 0.04F;
- offset_x += combat_jitter_x + sway_x;
- offset_z += combat_jitter_z + sway_z;
- }
- float applied_vertical_jitter = vertical_jitter;
- float applied_yaw_offset = yaw_offset;
- if (anim.is_attacking && anim.is_melee) {
- float const combat_yaw = (fast_random(rng_state) - 0.5F) * 15.0F;
- applied_yaw_offset += combat_yaw;
- }
- QMatrix4x4 inst_model;
- float applied_yaw = applied_yaw_offset;
- if (transform_comp != nullptr) {
- applied_yaw = transform_comp->rotation.y + applied_yaw_offset;
- QMatrix4x4 m = k_identity_matrix;
- m.translate(transform_comp->position.x, transform_comp->position.y,
- transform_comp->position.z);
- m.rotate(applied_yaw, 0.0F, 1.0F, 0.0F);
- m.scale(transform_comp->scale.x, transform_comp->scale.y,
- transform_comp->scale.z);
- m.translate(offset_x, applied_vertical_jitter, offset_z);
- if (!ctx.skip_ground_offset && entity_ground_offset != 0.0F) {
- m.translate(0.0F, -entity_ground_offset, 0.0F);
- }
- inst_model = m;
- } else {
- inst_model = ctx.model;
- inst_model.rotate(applied_yaw, 0.0F, 1.0F, 0.0F);
- inst_model.translate(offset_x, applied_vertical_jitter, offset_z);
- if (!ctx.skip_ground_offset && entity_ground_offset != 0.0F) {
- inst_model.translate(0.0F, -entity_ground_offset, 0.0F);
- }
- }
- QVector3D const soldier_world_pos =
- inst_model.map(QVector3D(0.0F, 0.0F, 0.0F));
- constexpr float k_soldier_cull_radius = 0.6F;
- if (ctx.camera != nullptr &&
- !ctx.camera->is_in_frustum(soldier_world_pos, k_soldier_cull_radius)) {
- ++s_render_stats.soldiers_skipped_frustum;
- continue;
- }
- HumanoidLOD soldier_lod = HumanoidLOD::Full;
- float soldier_distance = 0.0F;
- if (ctx.force_humanoid_lod) {
- soldier_lod = ctx.forced_humanoid_lod;
- } else if (ctx.camera != nullptr) {
- soldier_distance =
- (soldier_world_pos - ctx.camera->get_position()).length();
- soldier_lod = calculate_humanoid_lod(soldier_distance);
- if (soldier_lod == HumanoidLOD::Billboard) {
- ++s_render_stats.soldiers_skipped_lod;
- continue;
- }
- soldier_lod =
- Render::VisibilityBudgetTracker::instance().request_humanoid_lod(
- soldier_lod);
- }
- if (soldier_distance > 0.0F) {
- if (soldier_lod == HumanoidLOD::Reduced &&
- soldier_distance > k_temporal_skip_distance_reduced) {
- if (!should_render_temporal(s_current_frame, inst_seed,
- k_temporal_skip_period_reduced)) {
- ++s_render_stats.soldiers_skipped_temporal;
- continue;
- }
- }
- if (soldier_lod == HumanoidLOD::Minimal &&
- soldier_distance > k_temporal_skip_distance_minimal) {
- if (!should_render_temporal(s_current_frame, inst_seed,
- k_temporal_skip_period_minimal)) {
- ++s_render_stats.soldiers_skipped_temporal;
- continue;
- }
- }
- }
- ++s_render_stats.soldiers_rendered;
- DrawContext inst_ctx{ctx.resources, ctx.entity, ctx.world, inst_model};
- inst_ctx.selected = ctx.selected;
- inst_ctx.hovered = ctx.hovered;
- inst_ctx.animation_time = ctx.animation_time;
- inst_ctx.renderer_id = ctx.renderer_id;
- inst_ctx.backend = ctx.backend;
- inst_ctx.camera = ctx.camera;
- VariationParams variation = VariationParams::fromSeed(inst_seed);
- adjust_variation(inst_ctx, inst_seed, variation);
- float const combined_height_scale = height_scale * variation.height_scale;
- if (needs_height_scaling ||
- std::abs(variation.height_scale - 1.0F) > 0.001F) {
- QMatrix4x4 scale_matrix;
- scale_matrix.scale(variation.bulk_scale, combined_height_scale, 1.0F);
- inst_ctx.model = inst_ctx.model * scale_matrix;
- }
- HumanoidPose pose;
- bool used_cached_pose = false;
- PoseCacheKey cache_key =
- make_pose_cache_key(reinterpret_cast<uintptr_t>(ctx.entity), idx);
- auto cache_it = s_pose_cache.find(cache_key);
- const bool allow_cached_pose = (!anim.is_moving) || ctx.animation_throttled;
- if (allow_cached_pose && cache_it != s_pose_cache.end()) {
- CachedPoseEntry &cached = cache_it->second;
- if ((ctx.animation_throttled || !cached.was_moving) &&
- s_current_frame - cached.frame_number < k_pose_cache_max_age) {
- pose = cached.pose;
- variation = cached.variation;
- cached.frame_number = s_current_frame;
- used_cached_pose = true;
- ++s_render_stats.poses_cached;
- }
- }
- if (!used_cached_pose) {
- bool used_palette = false;
- if (!anim.is_moving && !anim.is_attacking &&
- PosePaletteCache::instance().is_generated() &&
- (soldier_lod == HumanoidLOD::Reduced ||
- soldier_lod == HumanoidLOD::Minimal)) {
- PosePaletteKey palette_key;
- palette_key.state = AnimState::Idle;
- palette_key.frame = 0;
- palette_key.is_moving = false;
- const auto *palette_entry =
- PosePaletteCache::instance().get(palette_key);
- if (palette_entry != nullptr) {
- pose = palette_entry->pose;
- used_palette = true;
- ++s_render_stats.poses_cached;
- }
- }
- if (!used_palette) {
- compute_locomotion_pose(inst_seed, anim.time + phase_offset,
- anim.is_moving, variation, pose);
- ++s_render_stats.poses_computed;
- }
- CachedPoseEntry &entry = s_pose_cache[cache_key];
- entry.pose = pose;
- entry.variation = variation;
- entry.frame_number = s_current_frame;
- entry.was_moving = anim.is_moving;
- }
- HumanoidAnimationContext anim_ctx{};
- anim_ctx.inputs = anim;
- anim_ctx.variation = variation;
- anim_ctx.formation = formation;
- anim_ctx.jitter_seed = phase_offset;
- anim_ctx.instance_position =
- inst_ctx.model.map(QVector3D(0.0F, 0.0F, 0.0F));
- float yaw_rad = qDegreesToRadians(applied_yaw);
- QVector3D forward(std::sin(yaw_rad), 0.0F, std::cos(yaw_rad));
- if (forward.lengthSquared() > 1e-8F) {
- forward.normalize();
- } else {
- forward = QVector3D(0.0F, 0.0F, 1.0F);
- }
- QVector3D up(0.0F, 1.0F, 0.0F);
- QVector3D right = QVector3D::crossProduct(up, forward);
- if (right.lengthSquared() > 1e-8F) {
- right.normalize();
- } else {
- right = QVector3D(1.0F, 0.0F, 0.0F);
- }
- anim_ctx.entity_forward = forward;
- anim_ctx.entity_right = right;
- anim_ctx.entity_up = up;
- anim_ctx.locomotion_direction = forward;
- anim_ctx.yaw_degrees = applied_yaw;
- anim_ctx.yaw_radians = yaw_rad;
- anim_ctx.has_movement_target = false;
- anim_ctx.move_speed = 0.0F;
- anim_ctx.movement_target = QVector3D(0.0F, 0.0F, 0.0F);
- if (movement_comp != nullptr) {
- QVector3D velocity(movement_comp->vx, 0.0F, movement_comp->vz);
- float speed = velocity.length();
- anim_ctx.move_speed = speed;
- if (speed > 1e-4F) {
- anim_ctx.locomotion_direction = velocity.normalized();
- }
- anim_ctx.has_movement_target = movement_comp->has_target;
- anim_ctx.movement_target =
- QVector3D(movement_comp->target_x, 0.0F, movement_comp->target_y);
- }
- anim_ctx.locomotion_velocity =
- anim_ctx.locomotion_direction * anim_ctx.move_speed;
- anim_ctx.motion_state = classify_motion_state(anim, anim_ctx.move_speed);
- anim_ctx.gait.state = anim_ctx.motion_state;
- anim_ctx.gait.speed = anim_ctx.move_speed;
- anim_ctx.gait.velocity = anim_ctx.locomotion_velocity;
- anim_ctx.gait.has_target = anim_ctx.has_movement_target;
- anim_ctx.gait.is_airborne = false;
- float reference_speed = (anim_ctx.gait.state == HumanoidMotionState::Run)
- ? k_reference_run_speed
- : k_reference_walk_speed;
- if (reference_speed > 0.0001F) {
- anim_ctx.gait.normalized_speed =
- std::clamp(anim_ctx.gait.speed / reference_speed, 0.0F, 1.0F);
- } else {
- anim_ctx.gait.normalized_speed = 0.0F;
- }
- if (!anim.is_moving) {
- anim_ctx.gait.normalized_speed = 0.0F;
- }
- if (anim.is_moving) {
- float stride_base = 0.8F;
- if (anim_ctx.motion_state == HumanoidMotionState::Run) {
- stride_base = 0.45F;
- }
- float const base_cycle =
- stride_base / std::max(0.1F, variation.walk_speed_mult);
- anim_ctx.locomotion_cycle_time = base_cycle;
- anim_ctx.locomotion_phase = std::fmod(
- (anim.time + phase_offset) / std::max(0.001F, base_cycle), 1.0F);
- anim_ctx.gait.cycle_time = anim_ctx.locomotion_cycle_time;
- anim_ctx.gait.cycle_phase = anim_ctx.locomotion_phase;
- anim_ctx.gait.stride_distance =
- anim_ctx.gait.speed * anim_ctx.gait.cycle_time;
- } else {
- anim_ctx.locomotion_cycle_time = 0.0F;
- anim_ctx.locomotion_phase = 0.0F;
- anim_ctx.gait.cycle_time = 0.0F;
- anim_ctx.gait.cycle_phase = 0.0F;
- anim_ctx.gait.stride_distance = 0.0F;
- }
- if (anim.is_attacking) {
- float const attack_offset = phase_offset * 1.5F;
- anim_ctx.attack_phase = std::fmod(anim.time + attack_offset, 1.0F);
- if (ctx.has_attack_variant_override) {
- anim_ctx.inputs.attack_variant = ctx.attack_variant_override;
- } else {
- anim_ctx.inputs.attack_variant =
- static_cast<std::uint8_t>(inst_seed % 3);
- }
- }
- customize_pose(inst_ctx, anim_ctx, inst_seed, pose);
- if (!anim.is_moving && !anim.is_attacking) {
- HumanoidPoseController pose_ctrl(pose, anim_ctx);
- pose_ctrl.apply_micro_idle(anim.time + phase_offset, inst_seed);
- if (visible_count > 0) {
- auto hash_u32 = [](std::uint32_t x) -> std::uint32_t {
- x ^= x >> 16;
- x *= 0x7feb352dU;
- x ^= x >> 15;
- x *= 0x846ca68bU;
- x ^= x >> 16;
- return x;
- };
- constexpr float k_ambient_anim_duration = 6.0F;
- constexpr float k_unit_cycle_base = 15.0F;
- constexpr float k_unit_cycle_range = 10.0F;
- float const unit_cycle_period =
- k_unit_cycle_base +
- static_cast<float>(seed % 1000U) / (1000.0F / k_unit_cycle_range);
- float const unit_time_in_cycle =
- std::fmod(anim.time, std::max(0.001F, unit_cycle_period));
- std::uint32_t const unit_cycle_number = static_cast<std::uint32_t>(
- anim.time / std::max(0.001F, unit_cycle_period));
- int const max_slots = std::min(2, visible_count);
- std::uint32_t const cycle_rng =
- hash_u32(seed ^ (unit_cycle_number * 0x9e3779b9U));
- int const slot_count =
- (max_slots <= 0)
- ? 0
- : (1 + static_cast<int>(cycle_rng %
- static_cast<std::uint32_t>(max_slots)));
- int const idx_a =
- static_cast<int>(hash_u32(cycle_rng ^ 0xA341316CU) %
- static_cast<std::uint32_t>(visible_count));
- int idx_b = static_cast<int>(hash_u32(cycle_rng ^ 0xC8013EA4U) %
- static_cast<std::uint32_t>(visible_count));
- if (visible_count > 1 && idx_b == idx_a) {
- idx_b = (idx_a + 1 +
- static_cast<int>(cycle_rng % static_cast<std::uint32_t>(
- visible_count - 1))) %
- visible_count;
- }
- auto pick_idle_type = [&](std::uint32_t v) -> AmbientIdleType {
- std::uint32_t const roll = v % 100U;
- if (roll < 18U) {
- return AmbientIdleType::SitDown;
- }
- if (roll < 36U) {
- return AmbientIdleType::ShiftWeight;
- }
- if (roll < 52U) {
- return AmbientIdleType::ShuffleFeet;
- }
- if (roll < 66U) {
- return AmbientIdleType::TapFoot;
- }
- if (roll < 78U) {
- return AmbientIdleType::StepInPlace;
- }
- if (roll < 90U) {
- return AmbientIdleType::BendKnee;
- }
- if (roll < 98U) {
- return AmbientIdleType::RaiseWeapon;
- }
- return AmbientIdleType::Jump;
- };
- AmbientIdleType const unit_idle_type =
- pick_idle_type(hash_u32(cycle_rng ^ 0x3C6EF372U));
- if (slot_count > 0 && unit_time_in_cycle <= k_ambient_anim_duration) {
- bool const is_active =
- (idx == idx_a) || (slot_count > 1 && idx == idx_b);
- if (is_active) {
- float const phase = unit_time_in_cycle / k_ambient_anim_duration;
- pose_ctrl.apply_ambient_idle_explicit(unit_idle_type, phase);
- }
- }
- }
- }
- if (anim_ctx.motion_state == HumanoidMotionState::Run) {
- float const run_lean = 0.10F;
- pose.pelvis_pos.setZ(pose.pelvis_pos.z() - 0.05F);
- pose.shoulder_l.setZ(pose.shoulder_l.z() + run_lean);
- pose.shoulder_r.setZ(pose.shoulder_r.z() + run_lean);
- pose.neck_base.setZ(pose.neck_base.z() + run_lean * 0.7F);
- pose.head_pos.setZ(pose.head_pos.z() + run_lean * 0.5F);
- pose.pelvis_pos.setY(pose.pelvis_pos.y() - 0.03F);
- pose.shoulder_l.setY(pose.shoulder_l.y() - 0.04F);
- pose.shoulder_r.setY(pose.shoulder_r.y() - 0.04F);
- float const run_stride_mult = 1.5F;
- float const phase = anim_ctx.locomotion_phase;
- float const left_phase = phase;
- float const right_phase = std::fmod(phase + 0.5F, 1.0F);
- auto enhance_run_foot = [&](QVector3D &foot, float foot_phase) {
- float const lift_raw =
- std::sin(foot_phase * 2.0F * std::numbers::pi_v<float>);
- if (lift_raw > 0.0F) {
- float const extra_lift = lift_raw * k_run_extra_foot_lift;
- foot.setY(foot.y() + extra_lift);
- float const stride_enhance = std::sin((foot_phase - 0.25F) * 2.0F *
- std::numbers::pi_v<float>) *
- k_run_stride_enhancement;
- foot.setZ(foot.z() + stride_enhance);
- }
- };
- enhance_run_foot(pose.foot_l, left_phase);
- enhance_run_foot(pose.foot_r, right_phase);
- float const run_arm_swing = 0.06F;
- constexpr float max_run_arm_displacement = 0.08F;
- float const left_swing_raw =
- std::sin((left_phase + 0.1F) * 2.0F * std::numbers::pi_v<float>);
- float const left_arm_phase =
- std::clamp(left_swing_raw * run_arm_swing, -max_run_arm_displacement,
- max_run_arm_displacement);
- float const right_swing_raw =
- std::sin((right_phase + 0.1F) * 2.0F * std::numbers::pi_v<float>);
- float const right_arm_phase =
- std::clamp(right_swing_raw * run_arm_swing, -max_run_arm_displacement,
- max_run_arm_displacement);
- pose.hand_l.setZ(pose.hand_l.z() - left_arm_phase);
- pose.hand_r.setZ(pose.hand_r.z() - right_arm_phase);
- pose.hand_l.setY(pose.hand_l.y() + 0.02F);
- pose.hand_r.setY(pose.hand_r.y() + 0.02F);
- {
- using HP = HumanProportions;
- float const h_scale = variation.height_scale;
- float const max_reach =
- (HP::UPPER_ARM_LEN + HP::FORE_ARM_LEN) * h_scale * 0.98F;
- auto clamp_hand = [&](const QVector3D &shoulder, QVector3D &hand) {
- QVector3D diff = hand - shoulder;
- float const len = diff.length();
- if (len > max_reach && len > 1e-6F) {
- hand = shoulder + diff * (max_reach / len);
- }
- };
- clamp_hand(pose.shoulder_l, pose.hand_l);
- clamp_hand(pose.shoulder_r, pose.hand_r);
- QVector3D right_axis = pose.shoulder_r - pose.shoulder_l;
- right_axis.setY(0.0F);
- if (right_axis.lengthSquared() < 1e-8F) {
- right_axis = QVector3D(1.0F, 0.0F, 0.0F);
- }
- right_axis.normalize();
- if (right_axis.x() < 0.0F) {
- right_axis = -right_axis;
- }
- QVector3D const outward_l = -right_axis;
- QVector3D const outward_r = right_axis;
- pose.elbow_l = elbow_bend_torso(pose.shoulder_l, pose.hand_l, outward_l,
- 0.45F, 0.15F, -0.08F, +1.0F);
- pose.elbow_r = elbow_bend_torso(pose.shoulder_r, pose.hand_r, outward_r,
- 0.48F, 0.12F, 0.02F, +1.0F);
- }
- float const hip_rotation_raw =
- std::sin(phase * 2.0F * std::numbers::pi_v<float>);
- float const hip_rotation = hip_rotation_raw * 0.003F;
- pose.pelvis_pos.setX(pose.pelvis_pos.x() + hip_rotation);
- float const torso_sway_z = 0.0F;
- pose.shoulder_l.setZ(pose.shoulder_l.z() + torso_sway_z);
- pose.shoulder_r.setZ(pose.shoulder_r.z() + torso_sway_z);
- pose.neck_base.setZ(pose.neck_base.z() + torso_sway_z * 0.7F);
- pose.head_pos.setZ(pose.head_pos.z() + torso_sway_z * 0.5F);
- if (pose.head_frame.radius > 0.001F) {
- QVector3D head_up = pose.head_pos - pose.neck_base;
- if (head_up.lengthSquared() < 1e-8F) {
- head_up = pose.head_frame.up;
- } else {
- head_up.normalize();
- }
- QVector3D head_right =
- pose.head_frame.right -
- head_up * QVector3D::dotProduct(pose.head_frame.right, head_up);
- if (head_right.lengthSquared() < 1e-8F) {
- head_right =
- QVector3D::crossProduct(head_up, anim_ctx.entity_forward);
- if (head_right.lengthSquared() < 1e-8F) {
- head_right = QVector3D(1.0F, 0.0F, 0.0F);
- }
- }
- head_right.normalize();
- QVector3D head_forward =
- QVector3D::crossProduct(head_right, head_up).normalized();
- pose.head_frame.origin = pose.head_pos;
- pose.head_frame.up = head_up;
- pose.head_frame.right = head_right;
- pose.head_frame.forward = head_forward;
- pose.body_frames.head = pose.head_frame;
- }
- }
- const auto &gfx_settings = Render::GraphicsSettings::instance();
- const bool should_render_shadow =
- ctx.allow_template_cache && gfx_settings.shadows_enabled() &&
- (soldier_lod == HumanoidLOD::Full ||
- soldier_lod == HumanoidLOD::Reduced) &&
- soldier_distance < gfx_settings.shadow_max_distance();
- if (should_render_shadow && inst_ctx.backend != nullptr &&
- inst_ctx.resources != nullptr) {
- auto *shadow_shader =
- inst_ctx.backend->shader(QStringLiteral("troop_shadow"));
- auto *quad_mesh = inst_ctx.resources->quad();
- if (shadow_shader != nullptr && quad_mesh != nullptr) {
- float const shadow_size =
- is_mounted_spawn ? k_shadow_size_mounted : k_shadow_size_infantry;
- float depth_boost = 1.0F;
- float width_boost = 1.0F;
- if (unit_comp != nullptr) {
- using Game::Units::SpawnType;
- switch (unit_comp->spawn_type) {
- case SpawnType::Spearman:
- depth_boost = 1.8F;
- width_boost = 0.95F;
- break;
- case SpawnType::HorseSpearman:
- depth_boost = 2.1F;
- width_boost = 1.05F;
- break;
- case SpawnType::Archer:
- case SpawnType::HorseArcher:
- depth_boost = 1.2F;
- width_boost = 0.95F;
- break;
- default:
- break;
- }
- }
- float const shadow_width =
- shadow_size * (is_mounted_spawn ? 1.05F : 1.0F) * width_boost;
- float const shadow_depth =
- shadow_size * (is_mounted_spawn ? 1.30F : 1.10F) * depth_boost;
- auto &terrain_service = Game::Map::TerrainService::instance();
- if (terrain_service.is_initialized()) {
- QVector3D const inst_pos =
- inst_ctx.model.map(QVector3D(0.0F, 0.0F, 0.0F));
- float const shadow_y =
- terrain_service.get_terrain_height(inst_pos.x(), inst_pos.z());
- QVector3D light_dir = k_shadow_light_dir.normalized();
- QVector2D light_dir_xz(light_dir.x(), light_dir.z());
- if (light_dir_xz.lengthSquared() < 1e-6F) {
- light_dir_xz = QVector2D(0.0F, 1.0F);
- } else {
- light_dir_xz.normalize();
- }
- QVector2D const shadow_dir = -light_dir_xz;
- QVector2D dir_for_use = shadow_dir;
- if (dir_for_use.lengthSquared() < 1e-6F) {
- dir_for_use = QVector2D(0.0F, 1.0F);
- } else {
- dir_for_use.normalize();
- }
- float const shadow_offset = shadow_depth * 1.25F;
- QVector2D const offset_2d = dir_for_use * shadow_offset;
- float const light_yaw_deg = qRadiansToDegrees(
- std::atan2(double(dir_for_use.x()), double(dir_for_use.y())));
- QMatrix4x4 shadow_model;
- shadow_model.translate(inst_pos.x() + offset_2d.x(),
- shadow_y + k_shadow_ground_offset,
- inst_pos.z() + offset_2d.y());
- shadow_model.rotate(light_yaw_deg, 0.0F, 1.0F, 0.0F);
- shadow_model.rotate(-90.0F, 1.0F, 0.0F, 0.0F);
- shadow_model.scale(shadow_width, shadow_depth, 1.0F);
- if (auto *renderer = dynamic_cast<Renderer *>(&out)) {
- Shader *previous_shader = renderer->get_current_shader();
- renderer->set_current_shader(shadow_shader);
- shadow_shader->set_uniform(QStringLiteral("u_lightDir"),
- dir_for_use);
- out.mesh(quad_mesh, shadow_model, QVector3D(0.0F, 0.0F, 0.0F),
- nullptr, k_shadow_base_alpha, 0);
- renderer->set_current_shader(previous_shader);
- }
- }
- }
- }
- switch (soldier_lod) {
- case HumanoidLOD::Full:
- ++s_render_stats.lod_full;
- draw_common_body(inst_ctx, variant, pose, out);
- draw_facial_hair(inst_ctx, variant, pose, out);
- draw_armor(inst_ctx, variant, pose, anim_ctx, out);
- add_attachments(inst_ctx, variant, pose, anim_ctx, out);
- break;
- case HumanoidLOD::Reduced:
- ++s_render_stats.lod_reduced;
- draw_simplified_body(inst_ctx, variant, pose, out);
- draw_armor(inst_ctx, variant, pose, anim_ctx, out);
- add_attachments(inst_ctx, variant, pose, anim_ctx, out);
- break;
- case HumanoidLOD::Minimal:
- ++s_render_stats.lod_minimal;
- draw_minimal_body(inst_ctx, variant, pose, out);
- break;
- case HumanoidLOD::Billboard:
- break;
- }
- }
- }
- auto get_humanoid_render_stats() -> const HumanoidRenderStats & {
- return s_render_stats;
- }
- void reset_humanoid_render_stats() { s_render_stats.reset(); }
- } // namespace Render::GL
|