Baked set dressing into static meshes and tripled the prop density

Dressing bakes alive props into one DrawMesh per batch at init, rebuilt only on blasts. Adds 180 non-solid Decor props to Floor 01 (153 -> 333 total). Drops draw calls 95 -> 81 with no measurable GPU-time increase. Baked props give up paper sway and hard shadows; ramp is rebound after the batch so actors keep their shading.
This commit is contained in:
dandovlatov 2026-11-25 06:10:09 +04:00
parent aa1bafd9ab
commit ab4a16185d
7 changed files with 2740 additions and 10 deletions

File diff suppressed because it is too large Load diff

View file

@ -439,6 +439,13 @@ clipping < 0.5%, red-dominant 15–25%.
## Status log ## Status log
- 2026-11-25 (dressing density): destructible set dressing is now baked into two
static meshes at init (one `DrawMesh` per batch instead of one `DrawModelEx`
per prop per pass), rebuilt only when a blast destroys props. Added 180
non-solid `Decor` props to Floor 01 (153 → 333 total; 215 dressing), keeping
the plaza carpet cross and combat lanes clear. Bot captures show ~6x the
props with 95 → 81 draw calls and unchanged GPU time; baked props give up
paper sway and hard shadows. See `docs/VISUAL_DRESSING_INVESTIGATION.md`.
- 2026-11-24 (S18): built `phone_table.glb` (746 tris, red handset/dial/cord), - 2026-11-24 (S18): built `phone_table.glb` (746 tris, red handset/dial/cord),
`case_frame.glb` (176 tris, brass plinth/posts and yellow roof) and `case_frame.glb` (176 tris, brass plinth/posts and yellow roof) and
`case_glass.glb` (64 tris, four panes) in Blender via the reproducible `case_glass.glb` (64 tris, four panes) in Blender via the reproducible

View file

@ -2,14 +2,113 @@
#include "game/floor.hpp" #include "game/floor.hpp"
#include "render/models.hpp" #include "render/models.hpp"
#include "render/profiler.hpp"
#include "raymath.h"
#include <cmath> #include <cmath>
#include <cstring>
namespace { namespace {
constexpr float kCullRadiusSq = 52.0f * 52.0f; constexpr float kCullRadiusSq = 52.0f * 52.0f;
constexpr float kDeskTop = 0.790f; constexpr float kDeskTop = 0.790f;
constexpr float kTableTop = 0.828f; constexpr float kTableTop = 0.828f;
constexpr std::size_t kMaxBakedVerts = 65535;
struct BakeBatch {
std::vector<float> pos;
std::vector<float> nrm;
std::vector<float> uv;
std::vector<unsigned char> col;
std::vector<unsigned short> idx;
std::size_t vertex_count() const { return pos.size() / 3; }
void reset() {
pos.clear();
nrm.clear();
uv.clear();
col.clear();
idx.clear();
}
Mesh to_mesh() const {
Mesh mesh{};
const std::size_t verts = vertex_count();
if (verts == 0 || verts > kMaxBakedVerts) return mesh;
mesh.vertexCount = static_cast<int>(verts);
mesh.triangleCount = static_cast<int>(idx.size() / 3);
mesh.vertices = static_cast<float*>(MemAlloc(pos.size() * sizeof(float)));
std::memcpy(mesh.vertices, pos.data(), pos.size() * sizeof(float));
mesh.normals = static_cast<float*>(MemAlloc(nrm.size() * sizeof(float)));
std::memcpy(mesh.normals, nrm.data(), nrm.size() * sizeof(float));
mesh.texcoords = static_cast<float*>(MemAlloc(uv.size() * sizeof(float)));
std::memcpy(mesh.texcoords, uv.data(), uv.size() * sizeof(float));
mesh.colors = static_cast<unsigned char*>(MemAlloc(col.size()));
std::memcpy(mesh.colors, col.data(), col.size());
mesh.indices = static_cast<unsigned short*>(
MemAlloc(idx.size() * sizeof(unsigned short)));
std::memcpy(mesh.indices, idx.data(), idx.size() * sizeof(unsigned short));
UploadMesh(&mesh, false);
return mesh;
}
};
void bake_prop(BakeBatch& batch, Vec2 pos, float base_y, float yaw, float prop_scale,
const Mesh* src) {
if (src == nullptr || src->vertices == nullptr || src->vertexCount <= 0 ||
src->vertexCount > static_cast<int>(kMaxBakedVerts)) return;
if (batch.vertex_count() + static_cast<std::size_t>(src->vertexCount) >
kMaxBakedVerts) return;
const Matrix scale = MatrixScale(prop_scale, prop_scale, prop_scale);
const Matrix rot = MatrixRotate(Vector3{0.0f, 1.0f, 0.0f}, yaw * RAD2DEG);
const Matrix linear = MatrixMultiply(scale, rot);
const Matrix transform = MatrixMultiply(linear, MatrixTranslate(pos.x, base_y, pos.y));
const unsigned short base = static_cast<unsigned short>(batch.vertex_count());
for (int i = 0; i < src->vertexCount; ++i) {
const Vector3 v{src->vertices[i * 3], src->vertices[i * 3 + 1],
src->vertices[i * 3 + 2]};
const Vector3 w = Vector3Transform(v, transform);
batch.pos.push_back(w.x);
batch.pos.push_back(w.y);
batch.pos.push_back(w.z);
Vector3 n{0.0f, 1.0f, 0.0f};
if (src->normals != nullptr) {
n = Vector3Transform(
Vector3{src->normals[i * 3], src->normals[i * 3 + 1],
src->normals[i * 3 + 2]},
linear);
n = Vector3Normalize(n);
}
batch.nrm.push_back(n.x);
batch.nrm.push_back(n.y);
batch.nrm.push_back(n.z);
const Color c = src->colors != nullptr
? Color{src->colors[i * 4], src->colors[i * 4 + 1],
src->colors[i * 4 + 2], src->colors[i * 4 + 3]}
: WHITE;
batch.col.push_back(c.r);
batch.col.push_back(c.g);
batch.col.push_back(c.b);
batch.col.push_back(c.a);
batch.uv.push_back(src->texcoords != nullptr ? src->texcoords[i * 2] : 0.0f);
batch.uv.push_back(src->texcoords != nullptr ? src->texcoords[i * 2 + 1] : 0.0f);
}
const int count = src->indices != nullptr ? src->triangleCount * 3
: src->vertexCount;
for (int i = 0; i < count; ++i) {
const unsigned short index = src->indices != nullptr
? src->indices[i]
: static_cast<unsigned short>(i);
batch.idx.push_back(static_cast<unsigned short>(base + index));
}
}
} // namespace } // namespace
@ -24,6 +123,7 @@ void Dressing::init(const Floor* floor) {
props_.reserve(80); props_.reserve(80);
surfaces_.clear(); surfaces_.clear();
surfaces_.reserve(64); surfaces_.reserve(64);
baked_dirty_ = true;
if (floor == nullptr) return; if (floor == nullptr) return;
for (const level::Prop& p : floor->data().props) { for (const level::Prop& p : floor->data().props) {
@ -48,18 +148,92 @@ void Dressing::init(const Floor* floor) {
// Structural props have no GLB: their authored height is the // Structural props have no GLB: their authored height is the
// surface. Modelled props use the bank's visual height. // surface. Modelled props use the bank's visual height.
const float top = p.y + (has_model const float top = p.y + (has_model
? ModelBank::height(model) * p.scale ? ModelBank::height(model) * p.scale
: p.h + 0.05f); : p.h + 0.05f);
surfaces_.push_back({pos, top, prop_index}); surfaces_.push_back({pos, top, prop_index});
} }
} }
} }
void Dressing::reset() { void Dressing::clear_baked() {
for (Prop& p : props_) p.alive = true; for (Mesh& mesh : baked_meshes_) {
if (mesh.vertexCount > 0) UnloadMesh(mesh);
}
baked_meshes_.clear();
} }
void Dressing::draw(ModelBank& models, float time, Vec2 focus) const { void Dressing::rebuild_baked(ModelBank& models) {
baked_dirty_ = false;
clear_baked();
if (models.shader().id == 0 || models.ramp().id == 0) return;
if (!baked_material_ready_) {
baked_material_ = LoadMaterialDefault();
baked_material_.shader = models.shader();
baked_material_.maps[MATERIAL_MAP_EMISSION].texture = models.ramp();
baked_material_ready_ = true;
}
BakeBatch batch;
for (const Prop& p : props_) {
if (!p.alive) continue;
const Mesh* src = models.mesh(p.model);
if (src == nullptr) continue;
if (batch.vertex_count() + static_cast<std::size_t>(src->vertexCount) >
kMaxBakedVerts) {
Mesh mesh = batch.to_mesh();
if (mesh.vertexCount > 0) baked_meshes_.push_back(mesh);
batch.reset();
}
bake_prop(batch, p.pos, p.base_y, p.yaw, p.scale, src);
}
Mesh mesh = batch.to_mesh();
if (mesh.vertexCount > 0) baked_meshes_.push_back(mesh);
TraceLog(LOG_INFO, "DRESSING: baked %zu mesh(es) from %zu props",
baked_meshes_.size(), props_.size());
}
void Dressing::shutdown() {
clear_baked();
if (baked_material_ready_) {
MemFree(baked_material_.maps);
baked_material_ready_ = false;
}
}
void Dressing::reset() {
for (Prop& p : props_) p.alive = true;
baked_dirty_ = true;
}
void Dressing::draw(ModelBank& models, float time, Vec2 focus) {
if (baked_dirty_) rebuild_baked(models);
if (!baked_meshes_.empty()) {
if (models.depth_pass()) return;
const Shader shader = models.shader();
const auto set_float = [&](const char* name, float value) {
const int loc = GetShaderLocation(shader, name);
if (loc >= 0) SetShaderValue(shader, loc, &value, SHADER_UNIFORM_FLOAT);
};
set_float("uFlash", 0.0f);
set_float("uOutline", 0.0f);
set_float("uGlow", 0.0f);
set_float("uHiss", 0.0f);
set_float("uAlpha", 1.0f);
set_float("uRigScale", 0.0f);
set_float("uRigMode", 0.0f);
set_float("uWalk", 0.0f);
set_float("uStagger", 0.0f);
const float rig[4] = {0.0f, 0.0f, 0.0f, 0.0f};
const int rig_loc = GetShaderLocation(shader, "uRig");
if (rig_loc >= 0) SetShaderValue(shader, rig_loc, rig, SHADER_UNIFORM_VEC4);
for (const Mesh& mesh : baked_meshes_) {
PROF_DRAW_CALL();
DrawMesh(mesh, baked_material_, MatrixIdentity());
}
models.bind_ramp();
return;
}
for (const Prop& p : props_) { for (const Prop& p : props_) {
if (!p.alive) continue; if (!p.alive) continue;
const float dx = p.pos.x - focus.x; const float dx = p.pos.x - focus.x;
@ -74,7 +248,7 @@ void Dressing::draw(ModelBank& models, float time, Vec2 focus) const {
} }
int Dressing::destroy_in_radius(Vec2 center, float radius, Vector3* out, int Dressing::destroy_in_radius(Vec2 center, float radius, Vector3* out,
int capacity) { int capacity) {
const float reach = radius + 0.4f; const float reach = radius + 0.4f;
const float reach_sq = reach * reach; const float reach_sq = reach * reach;
int written = 0; int written = 0;
@ -84,6 +258,7 @@ int Dressing::destroy_in_radius(Vec2 center, float radius, Vector3* out,
const float dz = p.pos.y - center.y; const float dz = p.pos.y - center.y;
if (dx * dx + dz * dz > reach_sq) continue; if (dx * dx + dz * dz > reach_sq) continue;
p.alive = false; p.alive = false;
baked_dirty_ = true;
if (out != nullptr && written < capacity) { if (out != nullptr && written < capacity) {
out[written++] = {p.pos.x, out[written++] = {p.pos.x,
p.base_y + ModelBank::height(p.model) * p.scale * 0.5f, p.base_y + ModelBank::height(p.model) * p.scale * 0.5f,
@ -94,7 +269,7 @@ int Dressing::destroy_in_radius(Vec2 center, float radius, Vector3* out,
} }
int Dressing::surface_sources_in_radius(Vec2 center, float radius, int Dressing::surface_sources_in_radius(Vec2 center, float radius,
Vector3* out, int capacity) const { Vector3* out, int capacity) const {
const float reach_sq = radius * radius; const float reach_sq = radius * radius;
int written = 0; int written = 0;
for (const Surface& s : surfaces_) { for (const Surface& s : surfaces_) {

View file

@ -14,8 +14,9 @@ class Floor;
class Dressing { class Dressing {
public: public:
void init(const Floor* floor = nullptr); void init(const Floor* floor = nullptr);
void shutdown();
void reset(); void reset();
void draw(ModelBank& models, float time, Vec2 focus) const; void draw(ModelBank& models, float time, Vec2 focus);
// Marks props inside the blast alive=false and writes their world centres // Marks props inside the blast alive=false and writes their world centres
// into `out` so debris can originate from the papers that were destroyed. // into `out` so debris can originate from the papers that were destroyed.
@ -50,7 +51,13 @@ private:
void add(ActorModel model, float x, float y, float z, float yaw, void add(ActorModel model, float x, float y, float z, float yaw,
float scale = 1.0f); float scale = 1.0f);
void clear_baked();
void rebuild_baked(ModelBank& models);
std::vector<Prop> props_; std::vector<Prop> props_;
std::vector<Surface> surfaces_; std::vector<Surface> surfaces_;
std::vector<Mesh> baked_meshes_;
Material baked_material_{};
bool baked_material_ready_ = false;
bool baked_dirty_ = true;
}; };

View file

@ -197,12 +197,16 @@ void ModelBank::shutdown() {
ready_ = false; ready_ = false;
} }
void ModelBank::begin_frame(float time, float corruption, Vector3 camera_pos) { void ModelBank::bind_ramp() const {
if (!shader_ok_) return;
if (ramp_.id != 0) { if (ramp_.id != 0) {
rlActiveTextureSlot(MATERIAL_MAP_EMISSION); rlActiveTextureSlot(MATERIAL_MAP_EMISSION);
rlEnableTexture(ramp_.id); rlEnableTexture(ramp_.id);
} }
}
void ModelBank::begin_frame(float time, float corruption, Vector3 camera_pos) {
if (!shader_ok_) return;
bind_ramp();
SetShaderValue(shader_, loc_time_, &time, SHADER_UNIFORM_FLOAT); SetShaderValue(shader_, loc_time_, &time, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader_, loc_corruption_, &corruption, SHADER_UNIFORM_FLOAT); SetShaderValue(shader_, loc_corruption_, &corruption, SHADER_UNIFORM_FLOAT);
if (loc_camera_ >= 0) { if (loc_camera_ >= 0) {
@ -400,6 +404,14 @@ void ModelBank::update_animation(ActorModel id, const char* name, float dt,
} }
} }
const Mesh* ModelBank::mesh(ActorModel id) const {
const int index = static_cast<int>(id);
if (index < 0 || index >= static_cast<int>(ActorModel::Count)) return nullptr;
const Entry& e = entries_[index];
if (!e.loaded || e.model.meshCount == 0) return nullptr;
return &e.model.meshes[0];
}
float ModelBank::height(ActorModel id) { float ModelBank::height(ActorModel id) {
switch (id) { switch (id) {
case ActorModel::Guard: return 1.9f; case ActorModel::Guard: return 1.9f;

View file

@ -27,9 +27,16 @@ public:
void shutdown(); void shutdown();
bool ready() const { return ready_; } bool ready() const { return ready_; }
bool depth_pass() const { return depth_pass_; }
// Shared geometry / shading used by the baked static dressing mesh.
const Mesh* mesh(ActorModel id) const;
const Shader& shader() const { return shader_; }
Texture2D ramp() const { return ramp_; }
// Frame-wide shader uniforms (time / corruption / camera). // Frame-wide shader uniforms (time / corruption / camera).
void begin_frame(float time, float corruption, Vector3 camera_pos); void begin_frame(float time, float corruption, Vector3 camera_pos);
void bind_ramp() const;
void set_shadow(const ShadowMap& shadow, bool enabled); void set_shadow(const ShadowMap& shadow, bool enabled);
void begin_shadow_frame(float time); void begin_shadow_frame(float time);

View file

@ -564,6 +564,7 @@ void Presenter::reset() {
} }
void Presenter::shutdown() { void Presenter::shutdown() {
dressing_.shutdown();
debris_.shutdown(); debris_.shutdown();
concrete_debris_.shutdown(); concrete_debris_.shutdown();
debris_.clear(); debris_.clear();