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 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 17:36:41 +09:00
co-authored by Claude Opus 5.5
parent 696f4f62e6
commit b2c7c6ce7d
6 changed files with 97 additions and 2 deletions
+1
View File
@@ -2112,6 +2112,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
auto char_draws = preview_data ? player_actor.BuildDrawCommands() : std::vector<ava2::Render3DDraw>{};
auto dog_draws = preview_data ? mob_dog.BuildDrawCommands() : std::vector<ava2::Render3DDraw>{};
auto metin_draws = preview_data ? mob_metin.BuildDrawCommands() : std::vector<ava2::Render3DDraw>{};
ava2::NetworkActorManager::Instance().UpdateLODLevel(eyeX, eyeY, eyeZ, player_world_x, player_world_z);
auto net_draws = ava2::NetworkActorManager::Instance().BuildDrawCommands(static_cast<float>(frames) * 0.033f, renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix());
if (until_game && rendered_game_frames == 20) {
for (const auto& entry : ava2::NetworkActorManager::Instance().GetActorDict()) {
+8
View File
@@ -893,6 +893,14 @@ std::shared_ptr<ActorInstance> 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<Render3DDraw> NetworkActorManager::BuildDrawCommands(float animTime, const float* view, const float* proj) {
(void)animTime;
std::vector<Render3DDraw> allDraws;
+2
View File
@@ -284,6 +284,8 @@ public:
// Helpers to build 3D rendering draw calls for all visible network actors
std::vector<Render3DDraw> 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;
+47 -2
View File
@@ -45,6 +45,7 @@ void ActorInstance::SetMainModel(std::shared_ptr<Gr2ModelInstance> 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<Render3DDraw> 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<Render3DDraw> 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<uint32_t>((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<uint32_t>(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
+15
View File
@@ -210,6 +210,10 @@ public:
// Generate 3D draw commands
std::vector<Render3DDraw> 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<Gr2ModelInstance> LoadPartModel(const std::string& path) const;
void SetMainModel(std::shared_ptr<Gr2ModelInstance> 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<MotionType, std::string> m_motionPaths;
MotionType m_currentMotion = MotionType::Wait;