Cull off-screen actor shadows; reserve per-frame draw lists

- Network actor ground shadows use the actor's own IsVisible frustum
  test (its bounds cover the shadow) instead of building 17-vertex
  meshes with terrain queries for every actor on the map.
- Shadow ring uses a precomputed sin/cos table and reserved streams.
- Frame and network draw vectors reserve last frame's peak count and
  append network draws in one insert.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-10-12 16:19:37 +09:00
co-authored by Claude Opus 5.5
parent 4662e06b35
commit c85f9d815d
2 changed files with 30 additions and 8 deletions
+27 -8
View File
@@ -78,7 +78,20 @@ ava2::Render3DDraw BuildGroundShadowDraw(float x, float y, float z, float radius
draw.lighting = false;
draw.fog = false;
const int SEGMENTS = 16;
constexpr int SEGMENTS = 16;
static const auto ring = [] {
std::array<std::array<float, 2>, SEGMENTS> r{};
for (int i = 0; i < SEGMENTS; ++i) {
float angle = (2.0f * 3.14159265358979323846f * static_cast<float>(i)) / static_cast<float>(SEGMENTS);
r[i] = {std::cos(angle), std::sin(angle)};
}
return r;
}();
draw.positions.reserve((SEGMENTS + 1) * 3);
draw.normals.reserve((SEGMENTS + 1) * 3);
draw.uv0.reserve((SEGMENTS + 1) * 2);
draw.diffuse.reserve(SEGMENTS + 1);
draw.indices.reserve(SEGMENTS * 3);
float center_y = terrain ? (terrain->GetHeightAtWorld(x, z) + 2.0f) : (y + 2.0f);
// Center vertex (index 0)
@@ -93,9 +106,9 @@ ava2::Render3DDraw BuildGroundShadowDraw(float x, float y, float z, float radius
draw.diffuse.push_back(0x90000000); // semi-transparent black center
for (int i = 0; i < SEGMENTS; ++i) {
float angle = (2.0f * 3.14159265358979323846f * static_cast<float>(i)) / static_cast<float>(SEGMENTS);
float px = x + std::cos(angle) * radius;
float pz = z + std::sin(angle) * radius;
const float c = ring[i][0], s = ring[i][1];
float px = x + c * radius;
float pz = z + s * radius;
float py = terrain ? (terrain->GetHeightAtWorld(px, pz) + 2.0f) : (y + 2.0f);
draw.positions.push_back(px);
@@ -104,8 +117,8 @@ ava2::Render3DDraw BuildGroundShadowDraw(float x, float y, float z, float radius
draw.normals.push_back(0.0f);
draw.normals.push_back(1.0f);
draw.normals.push_back(0.0f);
draw.uv0.push_back(0.5f + 0.5f * std::cos(angle));
draw.uv0.push_back(0.5f + 0.5f * std::sin(angle));
draw.uv0.push_back(0.5f + 0.5f * c);
draw.uv0.push_back(0.5f + 0.5f * s);
draw.diffuse.push_back(0x00000000); // fully transparent edge
}
@@ -1948,6 +1961,9 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
#else
std::vector<ava2::Render3DDraw> draws;
#endif
// Draw count is stable frame to frame; reserve last frame's peak up front.
static size_t draw_reserve = 0;
draws.reserve(draw_reserve);
const auto profileBuildStart = std::chrono::steady_clock::now();
if (last_phase == "game") {
// Update outdoor SkyBox, Clouds UV scrolling & dynamic environment
@@ -2079,7 +2095,9 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
}
}
for (const auto& [vid, actorData] : ava2::NetworkActorManager::Instance().GetActorDict()) {
if (actorData.m_pActorInstance && !actorData.m_isHidden) {
// Same frustum test the actor's own draws use; its bounds cover the shadow.
if (actorData.m_pActorInstance && !actorData.m_isHidden &&
actorData.m_pActorInstance->IsVisible(renderer3d->GetViewMatrix(), renderer3d->GetProjMatrix())) {
draws.push_back(BuildGroundShadowDraw(
actorData.m_pActorInstance->GetX(),
actorData.m_pActorInstance->GetY(),
@@ -2138,7 +2156,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
}
apply_char_lighting(net_draws);
for (auto& nd : net_draws) draws.push_back(std::move(nd));
draws.insert(draws.end(), std::make_move_iterator(net_draws.begin()), std::make_move_iterator(net_draws.end()));
// Add Particle effects, projectiles, and weapon ribbon trails
float camPos[3] = {eyeX, eyeY, eyeZ};
@@ -2164,6 +2182,7 @@ const auto profileActiveStart = std::chrono::steady_clock::now();
}
profileBuildMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now()-profileBuildStart).count();
draw_reserve = std::max(draw_reserve, draws.size());
if (!draws.empty()) {
const auto profileRasterStart = std::chrono::steady_clock::now();
uint32_t clear_col = (last_phase == "game") ? current_env.fog.GetColorUint() : 0x00000000;
+3
View File
@@ -896,6 +896,8 @@ std::shared_ptr<ActorInstance> NetworkActorManager::GetActorInstance(uint32_t vi
std::vector<Render3DDraw> NetworkActorManager::BuildDrawCommands(float animTime, const float* view, const float* proj) {
(void)animTime;
std::vector<Render3DDraw> allDraws;
static size_t reserveHint = 0; // stable frame to frame
allDraws.reserve(reserveHint);
for (auto& [vid, actorData] : m_actors) {
if (!actorData.m_pActorInstance || actorData.m_isHidden) continue;
if (!IsMainActorVID(vid) && !actorData.m_pActorInstance->IsVisible(view,proj)
@@ -920,6 +922,7 @@ std::vector<Render3DDraw> NetworkActorManager::BuildDrawCommands(float animTime,
allDraws.push_back(std::move(d));
}
}
reserveHint = std::max(reserveHint, allDraws.size());
return allDraws;
}