diff options
author | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2021-11-02 09:30:25 +0800 |
commit | b7511abf1a1f302b5c7ebf50faaf65b62d7bb6ed (patch) | |
tree | 7265398e248b55ede4b3550ec47660be334bf1ff /Runtime/GUI | |
parent | a11097e51fcaef4488218411f2d7b3ee34550000 (diff) |
* TextMesh
Diffstat (limited to 'Runtime/GUI')
-rw-r--r-- | Runtime/GUI/Font.h | 12 | ||||
-rw-r--r-- | Runtime/GUI/TextMesh.cpp | 90 | ||||
-rw-r--r-- | Runtime/GUI/TextMesh.h | 23 | ||||
-rw-r--r-- | Runtime/GUI/TextMeshGenerator.h | 14 |
4 files changed, 135 insertions, 4 deletions
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 <vector> +#include <unordered_map> + +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<unsigned int, vector<TextInfo>> 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<unsigned int, vector<TextInfo>>(index, vector<TextInfo>())); + list = s_TextInfos.find(index); + } + vector<TextInfo>& 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<TextInfo>& 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 <unordered_map>
+
+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<int/*IndexOfAtlas*/, VertexBuffer*> 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<TextMeshGenerator>
+{
+public:
+ TextMesh* GetTextMesh(const UnicodeString& str);
+
+private:
+
+
+};
+
+#define g_TextMeshGenerator (*TextMeshGenerator::Instance())
\ No newline at end of file |