improve atlas texture size handling
This commit is contained in:
@@ -70,8 +70,8 @@ namespace OpenVulkano
|
|||||||
|
|
||||||
#ifdef MSDFGEN_AVAILABLE
|
#ifdef MSDFGEN_AVAILABLE
|
||||||
msdf_atlas::Charset charset = MsdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
|
msdf_atlas::Charset charset = MsdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
|
||||||
//Charset charset = Charset::ASCII;
|
//msdf_atlas::Charset charset = msdf_atlas::Charset::ASCII;
|
||||||
//for (unicode_t c = 0x0410; c <= 0x041F; c++)
|
//for (msdf_atlas::unicode_t c = 0x0410; c <= 0x041F; c++)
|
||||||
//{
|
//{
|
||||||
// // some unicode values
|
// // some unicode values
|
||||||
// charset.add(c);
|
// charset.add(c);
|
||||||
|
|||||||
@@ -175,15 +175,17 @@ namespace OpenVulkano::Scene
|
|||||||
|
|
||||||
TightAtlasPacker packer;
|
TightAtlasPacker packer;
|
||||||
packer.setDimensionsConstraint(DimensionsConstraint::SQUARE);
|
packer.setDimensionsConstraint(DimensionsConstraint::SQUARE);
|
||||||
int width = 1024, height = 1024;
|
int width, height;
|
||||||
packer.setDimensions(width, height);
|
const int glyphsPerRow = std::sqrt(glyphsGeometry.size());
|
||||||
|
const int glyphSize = m_config.glyphSize;
|
||||||
|
const int rowWidth = glyphSize * glyphsPerRow;
|
||||||
|
packer.setDimensions(rowWidth, rowWidth);
|
||||||
|
// something to play with. should not be too high.
|
||||||
// more value - more sdf impact
|
// more value - more sdf impact
|
||||||
// this setup is tricky. with low value and large amount of characters visible artifacts (extra lines) may appear.
|
packer.setPixelRange(m_config.pixelRange);
|
||||||
// with high value and large amount of characters sdf deals huge impact and characters are not readable anymore.
|
packer.setMiterLimit(m_config.miterLimit);
|
||||||
const double pixelRange = std::min((width / (double)chset.size()) * 3, 26.0);
|
|
||||||
packer.setPixelRange(pixelRange);
|
|
||||||
packer.setMiterLimit(1.0);
|
|
||||||
packer.pack(glyphsGeometry.data(), glyphsGeometry.size());
|
packer.pack(glyphsGeometry.data(), glyphsGeometry.size());
|
||||||
|
packer.getDimensions(width, height);
|
||||||
|
|
||||||
m_generator.resize(width, height);
|
m_generator.resize(width, height);
|
||||||
GeneratorAttributes attributes;
|
GeneratorAttributes attributes;
|
||||||
@@ -250,10 +252,7 @@ namespace OpenVulkano::Scene
|
|||||||
info.advance = glyphBox.advance;
|
info.advance = glyphBox.advance;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pngOutput && !pngOutput->empty())
|
if (pngOutput && !pngOutput->empty()) { SavePng(storage, pngOutput.value(), 1); }
|
||||||
{
|
|
||||||
SavePng(storage, pngOutput.value(), 1);
|
|
||||||
}
|
|
||||||
destroyFont(font);
|
destroyFont(font);
|
||||||
deinitializeFreetype(ft);
|
deinitializeFreetype(ft);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,11 +21,20 @@
|
|||||||
|
|
||||||
namespace OpenVulkano::Scene
|
namespace OpenVulkano::Scene
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct MsdfFontAtlasGeneratorConfig
|
||||||
|
{
|
||||||
|
int glyphSize = 42;
|
||||||
|
double miterLimit = 1.0;
|
||||||
|
msdfgen::Range pixelRange = 5;
|
||||||
|
};
|
||||||
|
|
||||||
class MsdfFontAtlasGenerator : public FontAtlasGenerator
|
class MsdfFontAtlasGenerator : public FontAtlasGenerator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using SdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 1, msdf_atlas::sdfGenerator,
|
using SdfGenerator = msdf_atlas::ImmediateAtlasGenerator<float, 1, msdf_atlas::sdfGenerator,
|
||||||
msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 1>>;
|
msdf_atlas::BitmapAtlasStorage<msdfgen::byte, 1>>;
|
||||||
|
using Config = MsdfFontAtlasGeneratorConfig;
|
||||||
static msdf_atlas::Charset LoadAllGlyphs(const std::variant<std::string, Array<char>>& data);
|
static msdf_atlas::Charset LoadAllGlyphs(const std::variant<std::string, Array<char>>& data);
|
||||||
void GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset,
|
void GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset,
|
||||||
const std::optional<std::string>& pngOutput = std::nullopt) override;
|
const std::optional<std::string>& pngOutput = std::nullopt) override;
|
||||||
@@ -37,11 +46,12 @@ namespace OpenVulkano::Scene
|
|||||||
const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII,
|
const msdf_atlas::Charset& charset = msdf_atlas::Charset::ASCII,
|
||||||
const std::optional<std::string>& pngOutput = std::nullopt);
|
const std::optional<std::string>& pngOutput = std::nullopt);
|
||||||
void SaveAtlasMetadataInfo(const std::string& outputFile, bool packIntoSingleFile = true) const override;
|
void SaveAtlasMetadataInfo(const std::string& outputFile, bool packIntoSingleFile = true) const override;
|
||||||
|
void SetGeneratorConfig(const Config& config) { m_config = config; }
|
||||||
const Texture& GetAtlas() const override { return m_atlasTex; }
|
const Texture& GetAtlas() const override { return m_atlasTex; }
|
||||||
std::map<uint32_t, GlyphInfo>& GetGlyphsInfo() override { return m_symbols; }
|
std::map<uint32_t, GlyphInfo>& GetGlyphsInfo() override { return m_symbols; }
|
||||||
AtlasMetadata& GetAtlasMetadata() override { return m_meta; }
|
AtlasMetadata& GetAtlasMetadata() override { return m_meta; }
|
||||||
SdfGenerator& GetFontAtlasGenerator() { return m_generator; }
|
SdfGenerator& GetFontAtlasGenerator() { return m_generator; }
|
||||||
|
Config& GetGeneratorConfig() { return m_config; }
|
||||||
private:
|
private:
|
||||||
void InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font, const std::string& file);
|
void InitFreetypeFromFile(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font, const std::string& file);
|
||||||
void InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font,
|
void InitFreetypeFromBuffer(msdfgen::FreetypeHandle*& ft, msdfgen::FontHandle*& font,
|
||||||
@@ -55,6 +65,7 @@ namespace OpenVulkano::Scene
|
|||||||
SdfGenerator m_generator;
|
SdfGenerator m_generator;
|
||||||
Texture m_atlasTex;
|
Texture m_atlasTex;
|
||||||
AtlasMetadata m_meta;
|
AtlasMetadata m_meta;
|
||||||
|
Config m_config;
|
||||||
std::map<uint32_t, GlyphInfo> m_symbols;
|
std::map<uint32_t, GlyphInfo> m_symbols;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user