From b2c7c6ce7d785279f041459bdb83ce73c5a8d4cb Mon Sep 17 00:00:00 2001 From: shenlei Date: Mon, 12 Oct 2026 17:36:41 +0900 Subject: [PATCH] Animate distant LOD actors at the original reduced animation FPS Port CGrannyLODController::UpdateLODLevel and CGrannyModelInstance::Update(dwAniFPS): actors whose model has a _lod_01.gr2 sibling and stand more than 500 units from the camera target get 30-120 animation FPS by camera distance (LODDISTANCE_ACTOR 5000, LODDISTANCE_BUILDING 25000 for models of 500+ units), and their pose time only advances every 120/fps frames, so the SDK's cached pose is reused in between. Restarted or switched motions re-sample at once. The _lod_ meshes themselves are still not swapped in. Co-Authored-By: Claude Opus 5.5 --- src/app/main.cpp | 1 + src/eter_net/NetworkActorManager.cpp | 8 +++++ src/eter_net/NetworkActorManager.h | 2 ++ src/eter_render/ActorInstance.cpp | 49 ++++++++++++++++++++++++++-- src/eter_render/ActorInstance.h | 15 +++++++++ tests/ava2_render_3d_test.cpp | 24 ++++++++++++++ 6 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/app/main.cpp b/src/app/main.cpp index 2c1abe7e..e011a839 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -2112,6 +2112,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now(); auto char_draws = preview_data ? player_actor.BuildDrawCommands() : std::vector{}; auto dog_draws = preview_data ? mob_dog.BuildDrawCommands() : std::vector{}; auto metin_draws = preview_data ? mob_metin.BuildDrawCommands() : std::vector{}; + ava2::NetworkActorManager::Instance().UpdateLODLevel(eyeX, eyeY, eyeZ, player_world_x, player_world_z); auto net_draws = ava2::NetworkActorManager::Instance().BuildDrawCommands(static_cast(frames) * 0.033f, renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix()); if (until_game && rendered_game_frames == 20) { for (const auto& entry : ava2::NetworkActorManager::Instance().GetActorDict()) { diff --git a/src/eter_net/NetworkActorManager.cpp b/src/eter_net/NetworkActorManager.cpp index ad9e3858..47906fd4 100644 --- a/src/eter_net/NetworkActorManager.cpp +++ b/src/eter_net/NetworkActorManager.cpp @@ -893,6 +893,14 @@ std::shared_ptr NetworkActorManager::GetActorInstance(uint32_t vi return (it != m_actors.end()) ? it->second.m_pActorInstance : nullptr; } +void NetworkActorManager::UpdateLODLevel(float eyeX, float eyeY, float eyeZ, float targetX, float targetZ) { + for (auto& [vid, actorData] : m_actors) { + (void)vid; + if (actorData.m_pActorInstance) actorData.m_pActorInstance->UpdateLODLevel(eyeX, eyeY, eyeZ, targetX, targetZ); + if (actorData.m_pMountInstance) actorData.m_pMountInstance->UpdateLODLevel(eyeX, eyeY, eyeZ, targetX, targetZ); + } +} + std::vector NetworkActorManager::BuildDrawCommands(float animTime, const float* view, const float* proj) { (void)animTime; std::vector allDraws; diff --git a/src/eter_net/NetworkActorManager.h b/src/eter_net/NetworkActorManager.h index 4a7b6332..3ae2ad57 100644 --- a/src/eter_net/NetworkActorManager.h +++ b/src/eter_net/NetworkActorManager.h @@ -284,6 +284,8 @@ public: // Helpers to build 3D rendering draw calls for all visible network actors std::vector BuildDrawCommands(float animTime, const float* view = nullptr, const float* proj = nullptr); + // CArea/CInstanceBase -> CGraphicThingInstance::UpdateLODLevel for every actor and mount. + void UpdateLODLevel(float eyeX, float eyeY, float eyeZ, float targetX, float targetZ); private: uint32_t m_mainVID = 0; diff --git a/src/eter_render/ActorInstance.cpp b/src/eter_render/ActorInstance.cpp index da6522ac..929af39b 100644 --- a/src/eter_render/ActorInstance.cpp +++ b/src/eter_render/ActorInstance.cpp @@ -45,6 +45,7 @@ void ActorInstance::SetMainModel(std::shared_ptr model, const m_boundsModel = nullptr; m_model = std::move(model); m_modelPath = m_model ? path : std::string(); + ResetLodModel(); RebindParts(); // A new PART_MAIN instance restarts the current motion on it. if (m_model) { @@ -204,6 +205,24 @@ void ActorInstance::SetSashScale(float sx, float sy, float sz) { RebindParts(); } +void ActorInstance::ResetLodModel() { + // CActorInstance registers NoExtension(model) + "_lod_01.gr2" when it exists; only then does + // CGrannyLODController::UpdateLODLevel lower the animation FPS (m_que_pkModelInst.size() > 1). + m_hasLodModel = false; + m_lodAniFps = 120; + m_poseTimeValid = false; + if (!m_model) return; + const size_t dot = m_modelPath.find_last_of('.'); + const bool hasExtension = dot != std::string::npos && m_modelPath.find_first_of("/\\", dot) == std::string::npos; + std::error_code error; + m_hasLodModel = std::filesystem::is_regular_file(ResolvePath((hasExtension ? m_modelPath.substr(0, dot) : m_modelPath) + "_lod_01.gr2"), error); + // CGrannyLODController::AddModel: models of 500 units or more use the building LOD distance. + float loX, loY, loZ, hiX, hiY, hiZ; + m_lodDistance = 5000.0f; // LODDISTANCE_ACTOR + if (m_model->GetLocalBounds(loX, loY, loZ, hiX, hiY, hiZ) && std::max({hiX - loX, hiY - loY, hiZ - loZ}) >= 500.0f) + m_lodDistance = 25000.0f; // LODDISTANCE_BUILDING +} + bool ActorInstance::SetModel(const std::string& modelPath) { std::string resolved = ResolvePath(modelPath); if (!std::filesystem::exists(resolved)) { @@ -214,6 +233,7 @@ bool ActorInstance::SetModel(const std::string& modelPath) { m_model = Gr2ModelInstance::Load(resolved); if (!m_model) return false; m_modelPath = modelPath; + ResetLodModel(); return true; } @@ -457,6 +477,7 @@ void ActorInstance::Update(float dt, const TerrainHeightSource* terrain, const M // Advance animation time m_animTime += dt; + m_lodLocalTime += dt; // CGrannyModelInstance::UpdateLocalTime if (!m_motionQueue.empty() && m_animTime >= m_motionDuration) { // ReservingMotionProcess: the next pushed motion takes over when the current one ends. const auto next = m_motionQueue.front(); @@ -587,7 +608,7 @@ std::vector ActorInstance::BuildDrawCommands(const float* worldOve if (!m_model) return {}; float world[16]; MakeTransformMatrix(m_yaw, m_posX, m_posY + m_modelYOffset * m_scale, m_posZ, m_scale, world); - auto draws = m_model->BuildDrawCommands(m_animTime, worldOverride ? worldOverride : world); + auto draws = m_model->BuildDrawCommands(PoseTime(), worldOverride ? worldOverride : world); // Shining system (ENABLE_SHINING_SYSTEM): weapon aura / glow if (m_weaponShining > 0) { @@ -662,6 +683,30 @@ std::vector ActorInstance::BuildDrawCommands(const float* worldOve return draws; } +void ActorInstance::UpdateLODLevel(float eyeX, float eyeY, float eyeZ, float targetX, float targetZ) { + // CGrannyLODController::UpdateLODLevel; the distance from the camera target ignores height. + m_lodAniFps = 120; + if (m_hasLodModel) { + const float centerX = targetX - m_posX, centerZ = targetZ - m_posZ; + if (std::sqrt(centerX * centerX + centerZ * centerZ) > 500.0f) { // LOD_APPLY_MIN + const float dx = eyeX - m_posX, dy = eyeY - m_posY, dz = eyeZ - m_posZ; + const float factor = std::clamp(m_lodDistance - std::sqrt(dx * dx + dy * dy + dz * dz), 0.0f, m_lodDistance); + m_lodAniFps = static_cast((120 - 30) * factor / m_lodDistance + 30) / 10 * 10; // ANIFPS_MAX/MIN + } + } + // CGrannyModelInstance::Update: hold the pose until the next ANIFPS_STEP frame. A restarted or + // switched motion (time going back, new model) re-samples at once instead of mixing clips. + const uint32_t frame = static_cast(m_lodLocalTime * 120.0f); + const uint32_t step = 120 / m_lodAniFps; + if (m_poseTimeValid && m_poseModel == m_model.get() && m_animTime >= m_poseAnimTime + && frame > step && frame / step == m_oldUpdateFrame / step) + return; + m_oldUpdateFrame = frame; + m_poseAnimTime = m_animTime; + m_poseModel = m_model.get(); + m_poseTimeValid = step > 1; // full rate: always draw the current time +} + bool ActorInstance::IsVisible(const float* view, const float* proj) const { if (!view || !proj || !m_model) return true; if (m_boundsModel != m_model.get()) { @@ -705,7 +750,7 @@ bool ActorInstance::HitTestScreen(float clickX, float clickY, const Gr2SceneRend bool ActorInstance::GetBoneWorldTransform(const std::string& boneName, float* outWorld16) const { if (!m_model || !outWorld16) return false; float localBone[16]; - if (!m_model->GetBoneWorldTransform(boneName, localBone, m_animTime)) { + if (!m_model->GetBoneWorldTransform(boneName, localBone, PoseTime())) { return false; } // Bone poses are still Granny Z-up, just like the mesh vertices. Convert diff --git a/src/eter_render/ActorInstance.h b/src/eter_render/ActorInstance.h index 93838833..7209b8b5 100644 --- a/src/eter_render/ActorInstance.h +++ b/src/eter_render/ActorInstance.h @@ -210,6 +210,10 @@ public: // Generate 3D draw commands std::vector BuildDrawCommands(const float* worldOverride = nullptr); + // CGraphicThingInstance::UpdateLODLevel + CGrannyModelInstance::Update(dwAniFPS): actors with + // _lod_ models far from the camera sample their pose at a reduced animation FPS (Y-up world). + void UpdateLODLevel(float eyeX, float eyeY, float eyeZ, float targetX, float targetZ); + uint32_t GetLODAniFPS() const { return m_lodAniFps; } Gr2ModelInstance* GetModel() { return m_model.get(); } const std::string& GetModelPath() const { return m_modelPath; } @@ -218,6 +222,7 @@ private: std::string ResolvePath(const std::string& path) const; std::shared_ptr LoadPartModel(const std::string& path) const; void SetMainModel(std::shared_ptr model, const std::string& path); + void ResetLodModel(); void AttachWeaponPart(uint32_t part, const SPartItem& item); void RebindParts(); @@ -241,6 +246,16 @@ private: mutable const Gr2ModelInstance* m_boundsModel = nullptr; mutable float m_boundsRadius = 500.f; std::string m_modelPath; + // LOD animation rate (CGrannyLODController); the meshes of _lod_01.._03 are still not ported. + float PoseTime() const { return m_poseTimeValid ? m_poseAnimTime : m_animTime; } + bool m_hasLodModel = false; + float m_lodDistance = 5000.0f; // LODDISTANCE_ACTOR, LODDISTANCE_BUILDING for size >= 500 + float m_lodLocalTime = 0.0f; + uint32_t m_lodAniFps = 120; // CGrannyModelInstance::ANIFPS_MAX + uint32_t m_oldUpdateFrame = 0; + float m_poseAnimTime = 0.0f; + bool m_poseTimeValid = false; + const Gr2ModelInstance* m_poseModel = nullptr; std::unordered_map m_motionPaths; MotionType m_currentMotion = MotionType::Wait; diff --git a/tests/ava2_render_3d_test.cpp b/tests/ava2_render_3d_test.cpp index bbc031ac..c7c84366 100644 --- a/tests/ava2_render_3d_test.cpp +++ b/tests/ava2_render_3d_test.cpp @@ -828,6 +828,30 @@ int main(int argc, char** argv) { const auto poseBeforeCull = actor.GetModel()->GetImpl()->GetPoseEvaluationCount(); assert(!actor.IsVisible(cullCamera.GetViewMatrix(),cullCamera.GetProjMatrix())); assert(actor.GetModel()->GetImpl()->GetPoseEvaluationCount()==poseBeforeCull); + + // CGrannyLODController: warrior_assasin1 has _lod_ models, so far from the camera it animates at + // ANIFPS_MIN (pose re-sampled every 4/120 s) and near the camera target at full rate. + actor.SetPosition(0,0,0); + actor.UpdateLODLevel(0,200,-300,0,0); + assert(actor.GetLODAniFPS()==120); + actor.UpdateLODLevel(0,200,-4900,2000,0); + assert(actor.GetLODAniFPS()==30); + float lodBone[16]; + assert(actor.GetBoneWorldTransform("Bip01 Spine",lodBone)); + const auto poseBeforeLod=actor.GetModel()->GetImpl()->GetPoseEvaluationCount(); + for(int frame=0;frame<24;++frame){ + actor.Update(1.0f/60.0f,&terrain); + actor.UpdateLODLevel(0,200,-4900,2000,0); + assert(actor.GetBoneWorldTransform("Bip01 Spine",lodBone)); + } + const auto farEvaluations=actor.GetModel()->GetImpl()->GetPoseEvaluationCount()-poseBeforeLod; + assert(farEvaluations>=10 && farEvaluations<=14 && "30 FPS animation over 0.4 s"); + for(int frame=0;frame<24;++frame){ + actor.Update(1.0f/60.0f,&terrain); + actor.UpdateLODLevel(0,200,-300,0,0); + assert(actor.GetBoneWorldTransform("Bip01 Spine",lodBone)); + } + assert(actor.GetModel()->GetImpl()->GetPoseEvaluationCount()-poseBeforeLod-farEvaluations==24); assert(actor.IsVisible(nullptr,nullptr)); // Test movement driving