#pragma once #include #include #include #include #include #include namespace boundard { // 渲染组件:描述 Actor 的几何形状、颜色和可选 PNG 纹理。 class Mesh { public: struct Vertex { std::array position; std::array color; std::array uv; }; std::vector vertices; std::vector indices; std::string texture; // PNG 纹理路径;为空时使用白色纹理。 std::array color() const { std::lock_guard lock(*color_mutex_); return color_; } void set_color(std::array color) { std::lock_guard lock(*color_mutex_); color_ = color; } static Mesh quad() { Mesh mesh; mesh.vertices = { {{-1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {0.0F, 0.0F}}, {{1.0F, -1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {1.0F, 0.0F}}, {{1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {1.0F, 1.0F}}, {{-1.0F, 1.0F}, {1.0F, 1.0F, 1.0F, 1.0F}, {0.0F, 1.0F}}, }; mesh.indices = {0, 1, 2, 2, 3, 0}; return mesh; } static Mesh grid(int half_cells = 12) { Mesh mesh; const float world_to_local = 1.0F / 0.35F; const float half_extent = static_cast(half_cells) * world_to_local; const float line_half_width = 0.012F * world_to_local; const std::array line_color{1.0F, 1.0F, 1.0F, 1.0F}; auto add_rect = [&](float left, float bottom, float right, float top) { const uint32_t base = static_cast(mesh.vertices.size()); mesh.vertices.insert(mesh.vertices.end(), { {{left, bottom}, line_color, {0.0F, 0.0F}}, {{right, bottom}, line_color, {1.0F, 0.0F}}, {{right, top}, line_color, {1.0F, 1.0F}}, {{left, top}, line_color, {0.0F, 1.0F}}, }); mesh.indices.insert(mesh.indices.end(), {base, base + 1, base + 2, base + 2, base + 3, base}); }; for (int cell = -half_cells; cell <= half_cells; ++cell) { const float coordinate = static_cast(cell) * world_to_local; add_rect(coordinate - line_half_width, -half_extent, coordinate + line_half_width, half_extent); add_rect(-half_extent, coordinate - line_half_width, half_extent, coordinate + line_half_width); } return mesh; } private: std::unique_ptr color_mutex_ = std::make_unique(); std::array color_{1.0F, 1.0F, 1.0F, 1.0F}; }; } // namespace boundard