From b7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed Mon Sep 17 00:00:00 2001 From: chai Date: Tue, 2 Nov 2021 09:30:25 +0800 Subject: * TextMesh --- Runtime/GUI/Font.h | 12 ++++-- Runtime/GUI/TextMesh.cpp | 90 +++++++++++++++++++++++++++++++++++++++++ Runtime/GUI/TextMesh.h | 23 ++++++++++- Runtime/GUI/TextMeshGenerator.h | 14 +++++++ 4 files changed, 135 insertions(+), 4 deletions(-) (limited to 'Runtime/GUI') diff --git a/Runtime/GUI/Font.h b/Runtime/GUI/Font.h index 344cc00..9fea14e 100644 --- a/Runtime/GUI/Font.h +++ b/Runtime/GUI/Font.h @@ -15,10 +15,10 @@ //https://github.com/kaienfr/Font struct Character { - unsigned int atlas; // atlas索引 - Rect position; // 在altas里的位置 + unsigned int atlas; // atlas索引 + Rect position; // 在altas里的位置 Vector2 bearing; // 左上角相对于原点的偏移 - unsigned int advance; // 总宽,算上了间隔 + unsigned int advance; // 总宽,算上了间隔 }; namespace character @@ -42,6 +42,12 @@ namespace character }; } +struct UnicodeString +{ + character::Codepoint* str; + int length; +}; + namespace std { template <> diff --git a/Runtime/GUI/TextMesh.cpp b/Runtime/GUI/TextMesh.cpp index e69de29..6f3b3d6 100644 --- a/Runtime/GUI/TextMesh.cpp +++ b/Runtime/GUI/TextMesh.cpp @@ -0,0 +1,90 @@ +#include "../Graphics/CustomVertexLayout.h" +#include "Runtime/Utilities/StaticInitiator.h" +#include "../Math/Math.h" +#include "../Graphics/Color.h" +#include "TextMesh.h" +#include +#include + +using namespace std; + +struct TextMeshVBOLayout +{ + Vector2 position; + Vector2 uv; + Color32 color; +}; + +static CustomVertexLayout s_TextMeshVBOLayout; + +static unsigned int s_VertexPerText; +static unsigned int s_SizePerVertex; +static unsigned int s_SizePerText; +static unsigned int s_IndicesPerText; + +struct TextInfo { + const Character* ch; + float offset; +}; + +static unordered_map> s_TextInfos; + +InitializeStaticVariables([]() { + VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(TextMeshVBOLayout)); + VertexAttributeDescriptor COLOR = VertexAttributeDescriptor(sizeof(Vector2)*2, 4, VertexAttrFormat_Unsigned_Byte, sizeof(TextMeshVBOLayout), true); + s_TextMeshVBOLayout.attributes.push_back(POSITION); + s_TextMeshVBOLayout.attributes.push_back(UV); + s_TextMeshVBOLayout.attributes.push_back(COLOR); + + s_VertexPerText = 4; + s_SizePerVertex = sizeof(TextMeshVBOLayout); + s_SizePerText = sizeof(TextMeshVBOLayout) * 4; + s_IndicesPerText = 6; +}); + +TextMesh::TextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment) +{ + m_Font = font; + s_TextInfos.clear(); + + float offset = 0; + for (int i = 0; i < str.length; ++i) + { + character::Codepoint c = str.str[i]; + const Character* ch = font->GetCharacter(c, pixelSize); + unsigned int index = ch->atlas; + + TextInfo info; + info.ch = ch; + info.offset = offset; + + auto list = s_TextInfos.find(index); + if (list == s_TextInfos.end()) + { + s_TextInfos.insert(std::pair>(index, vector())); + list = s_TextInfos.find(index); + } + vector& v = list->second; + v.push_back(info); + + offset += ch->advance; + } + + if (s_TextInfos.size() == 0) + return; + + for (auto iter : s_TextInfos) { + unsigned int index = iter.first; + vector& texts = iter.second; + int count = texts.size(); + + VertexBuffer* vb = new VertexBuffer(count * s_SizePerText, count * s_IndicesPerText, VertexBuffer::VertexBufferType_Static); + + } +} + +void TextMesh::Draw() +{ + +} diff --git a/Runtime/GUI/TextMesh.h b/Runtime/GUI/TextMesh.h index 2ca4618..8a29c92 100644 --- a/Runtime/GUI/TextMesh.h +++ b/Runtime/GUI/TextMesh.h @@ -1,5 +1,10 @@ #pragma once #include "../Graphics/VertexBuffer.h" +#include "Font.h" +#include "Runtime/Utilities/Exception.h" +#include + +CustomException(TextMeshException); enum ETextAnchor { @@ -22,11 +27,27 @@ enum ETextAlignment { TextAlignment_Auto, }; +typedef unsigned long long TextMeshHash; + +namespace TextHelper +{ + TextMeshHash GetTextMeshHash(); +} + +// 一段文字里面的网格可能会来自不同的atlas,在生成TextMesh时做好合批 + class TextMesh { public: + TextMesh(const UnicodeString& str, Font* font, int pixelSize, ETextAnchor anchor, ETextAlignment alignment)/*throw TextMeshException*/; + + ~TextMesh(); + + void Draw(); private: - VertexBuffer* m_VBO; + Font* m_Font; + std::unordered_map m_VBOs; + }; diff --git a/Runtime/GUI/TextMeshGenerator.h b/Runtime/GUI/TextMeshGenerator.h index c5992bd..baeaf16 100644 --- a/Runtime/GUI/TextMeshGenerator.h +++ b/Runtime/GUI/TextMeshGenerator.h @@ -1,6 +1,8 @@ #pragma once +#include "TextMesh.h" #include "Runtime/Utilities/IIncrementalTask.h" +#include "Font.h" // 回收长期没有被使用的text mesh class GraduallyReleaseTextMesh : public IIncrementalTask @@ -18,3 +20,15 @@ public: }; + +class TextMeshGenerator : public Singleton +{ +public: + TextMesh* GetTextMesh(const UnicodeString& str); + +private: + + +}; + +#define g_TextMeshGenerator (*TextMeshGenerator::Instance()) \ No newline at end of file -- cgit v1.1-26-g67d0