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
+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