diff --git a/src/app/main.cpp b/src/app/main.cpp index ab0edb52..30020d34 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -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, SEGMENTS> r{}; + for (int i = 0; i < SEGMENTS; ++i) { + float angle = (2.0f * 3.14159265358979323846f * static_cast(i)) / static_cast(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(i)) / static_cast(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 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(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; diff --git a/src/eter_net/NetworkActorManager.cpp b/src/eter_net/NetworkActorManager.cpp index 84cdaeaa..ad9e3858 100644 --- a/src/eter_net/NetworkActorManager.cpp +++ b/src/eter_net/NetworkActorManager.cpp @@ -896,6 +896,8 @@ std::shared_ptr NetworkActorManager::GetActorInstance(uint32_t vi std::vector NetworkActorManager::BuildDrawCommands(float animTime, const float* view, const float* proj) { (void)animTime; std::vector 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 NetworkActorManager::BuildDrawCommands(float animTime, allDraws.push_back(std::move(d)); } } + reserveHint = std::max(reserveHint, allDraws.size()); return allDraws; }