Files
DarkflameServer/dUgcServer/UgcBricks.h
Aaron Kimbrell 7c5d69c1ed feat(ugc): make models like LU Toolbox does, within CPU and memory budgets
Parity with LU Toolbox's Process Model, Bake Lighting and icon renderer, with
its defaults as the settings' defaults:

- Colors from its LU palette (UgcPalette: LU colors, LDD colors mapped to the
  nearest LU one, unknown ones black), transparent bricks at 58.82% opacity, a
  brick transparent only when all of its materials are.
- Color variation: each brick's material gets its HSV value shifted in a 2.224
  gamma by up to 5% (times the color's own amount), from a random number of
  the model, brick and material, so reprocessing gives the same colors in
  every LOD. Icons get none, and the icon renderer's color corrections.
- LODs 0 and 2 with its distance logic, written as NiLODNode/NiRangeLODData
  like its exports and the game's own brick models, shapes divided at 65535
  vertices along the longest side like divide_mesh.
- Ambient occlusion like its AO-only bake: 64 rays per vertex, distance 5,
  after hidden surface removal, transparent bricks neither baked nor
  occluding, glow colors added.
- Icons from its icon scene: 50 mm lens at 53.4/19.5 degrees, sun of 2.5 at
  21/50.3 degrees with soft shadows, grey world light with occlusion; LOD 0's
  hidden surface removal and occlusion are reused for them.
- Optional ground plane for hidden surface removal; stats.json per model and
  the previous version's previews kept for comparing.

Budgets, applied live on config reload: max_cpu_percent (workers account
their thread CPU time and sleep to stay under it, long renders included),
worker_nice, max_memory_mb (jobs are estimated from their brick count and
wait until they fit), max_model_bricks and pause_hours. /status and the
traffic report show CPU, memory and throttling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 22:31:06 -05:00

84 lines
3.0 KiB
C++

#pragma once
#include <array>
#include <cstdint>
#include <filesystem>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
/**
* The client's brick data the UGC server builds models from: LDD geometry (res/brickprimitives/lod<n>/<design>.g, .g1,
* ...) and the material colors (Materials.xml in res/brickdb.zip). The parsers are pure; BrickLibrary loads and caches
* files and may be used from any thread.
*/
namespace UgcBricks {
// One LDD .g file: a triangle mesh with a normal per vertex
struct Geometry {
std::vector<float> positions; // x, y, z per vertex
std::vector<float> normals; // x, y, z per vertex
std::vector<uint32_t> indices;
};
// An LDD material: sRGB color and opacity, 0-255
struct Material {
uint8_t r{ 160 };
uint8_t g{ 160 };
uint8_t b{ 160 };
uint8_t a{ 255 };
bool Transparent() const { return a < 255; }
};
// A .g file ("10GB" magic, counts, positions, normals, texture coordinates for decorated parts, indices)
std::optional<Geometry> ParseGeometry(std::string_view data);
// Materials.xml: MatID -> color
std::map<uint32_t, Material> ParseMaterials(std::string_view xml);
// A file from a zip archive (stored or deflated), matched without regard to case; nullopt when it isn't there
std::optional<std::string> ReadZipEntry(std::string_view zip, std::string_view name);
// `relative` (either slash, any case) under `root`, matching each part without regard to case as the client's
// files are named on Windows; nullopt when there is no such file
std::optional<std::filesystem::path> ResolvePath(const std::filesystem::path& root, std::string_view relative);
// Reads a whole file; nullopt when it can't
std::optional<std::string> ReadFile(const std::filesystem::path& path);
class BrickLibrary {
public:
// `res` is the client's res folder; `lod` the brickprimitives level used (0 is the most detailed)
BrickLibrary(std::filesystem::path res, uint32_t lod);
// Loads the material colors; false when brickdb.zip or its Materials.xml can't be read
bool LoadMaterials();
// For tests and callers that have the colors already
void SetMaterials(std::map<uint32_t, Material> materials);
// A material's color (a grey when the id is unknown)
Material GetMaterial(uint32_t id) const;
// Every geometry file of a design, in order (.g, .g1, ...); empty when the design has none. Loaded once.
// `lod`: the brickprimitives level, the library's own when omitted; a design without that level uses the
// next more detailed one.
std::shared_ptr<const std::vector<Geometry>> GetDesign(uint32_t design, std::optional<uint32_t> lod = std::nullopt);
// Designs loaded (for the memory report)
size_t CachedDesigns() const;
const std::filesystem::path& GetResPath() const { return m_Res; }
private:
std::filesystem::path m_Res;
uint32_t m_Lod;
std::map<uint32_t, Material> m_Materials;
mutable std::mutex m_Mutex;
std::map<uint64_t, std::shared_ptr<const std::vector<Geometry>>> m_Designs; // lod << 32 | design
};
}