1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
#include "../Graphics/CustomVertexLayout.h"
#include "Runtime/Utilities/StaticInitiator.h"
#include "../Math/Math.h"
#include "../Graphics/Color.h"
#include "UITextMesh.h"
#include "../Graphics/DefaultVertexLayout.h"
#include "Runtime/Debug/Log.h"
#include <vector>
#include <unordered_map>
#include "Runtime/Graphics/GfxDevice.h"
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_SizePerIndex;
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_IndicesPerText = 6;
s_SizePerIndex = VertexLayout::GetDefaultIndexSize();
s_SizePerText = sizeof(TextMeshVBOLayout) * 4;
});
// 一段文字里面的网格可能会来自不同的atlas,在生成UITextMesh时做好合批
// 需要支持
// * 大小
// * 锚点
// * 对齐方式
// * 换行
// * 颜色
UITextMesh::UITextMesh(const UnicodeString& str, Font* font,int pixelSize, ETextAnchor anchor, ETextAlignment alignment)
{
m_Font = font;
s_TextInfos.clear();
const Vector2 atlasSize = font->GetAtlasSize();
// 按照不同的atlas分类到s_TextInfos
float offset = 0;
for (int i = 0; i < str.length; ++i)
{
character::Codepoint c = str.str[i];
const Character* ch = font->GetCharacter(c, pixelSize);
if (ch == NULL)
continue;
unsigned int atlasIndex = ch->atlas;
if (atlasIndex != FONT_NOT_IN_ATLAS_PLACEHOLDER) //非空格
{
TextInfo info;
info.ch = ch;
info.offset = offset;
auto list = s_TextInfos.find(atlasIndex);
if (list == s_TextInfos.end())
s_TextInfos.insert(std::pair<unsigned int, vector<TextInfo>>(atlasIndex, vector<TextInfo>()));
vector<TextInfo>& v = s_TextInfos[atlasIndex];
v.push_back(info);
}
offset += ch->advance;
}
if (s_TextInfos.size() == 0)
return;
// 填充VBO和IBO
for (auto iter : s_TextInfos) {
unsigned int atlasIndex = iter.first; // atlas atlasIndex
vector<TextInfo>& texts = iter.second;
int textCount = texts.size();
VertexBuffer* vb = new VertexBuffer(textCount * s_SizePerText, textCount * s_IndicesPerText * s_SizePerIndex, VertexBuffer::VertexBufferType_Static);
void* pVB;
uint16* pIB;
vb->GetChunk(s_SizePerVertex, s_SizePerIndex, s_VertexPerText * textCount, s_IndicesPerText * textCount, EPrimitive::Primitive_Triangle, &pVB,(void**) &pIB);
TextMeshVBOLayout* dst = (TextMeshVBOLayout*)pVB;
for (int i = 0; i < textCount; ++i)
{
TextInfo& text = texts[i];
int vOff = i * s_VertexPerText;
// 左上角是原点
float pos[] = {
text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-left
text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y + text.ch->position.height, // bottom-right
text.offset + text.ch->bearing.x + text.ch->position.width, pixelSize - text.ch->bearing.y, // top-right
text.offset + text.ch->bearing.x, pixelSize - text.ch->bearing.y, // top-left
};
Vector4 uvQuad = Vector4(text.ch->position.x / atlasSize.x, text.ch->position.y / atlasSize.y, text.ch->position.width / atlasSize.x, text.ch->position.height / atlasSize.y);
float uv[] = {
uvQuad.x, uvQuad.y + uvQuad.w,
uvQuad.x + uvQuad.z, uvQuad.y + uvQuad.w,
uvQuad.x + uvQuad.z, uvQuad.y,
uvQuad.x, uvQuad.y,
};
for (int j = 0; j < s_VertexPerText; ++j)
{
dst[vOff + j].position.Set(pos[2 * j], pos[2 * j + 1]);
dst[vOff + j].uv.Set(uv[2 * j], uv[2 * j + 1]);
dst[vOff + j].color.Set(255 , 255, 255, 255);
}
int iOff = i * s_IndicesPerText;
int indices[] = {
0, 1, 3, // right-top
1, 2, 3, // left-bottom
};
for (int j = 0; j < s_IndicesPerText; ++j)
pIB[iOff + j] = vOff + indices[j];
}
vb->FlushChunk(s_VertexPerText * textCount, s_IndicesPerText * textCount);
m_VBOs.insert(std::pair<int, VertexBuffer*>(atlasIndex, vb));
}
WipeGLError();
}
void UITextMesh::Draw()
{
for (auto subText : m_VBOs)
{
int atlasIndex = subText.first; // atlasIndex of atlas
VertexBuffer* vbo = subText.second;
const GlyphAtals* atlas = m_Font->GetGlyphAtlas(atlasIndex);
if (atlas == NULL)
{
log_error("Render text failed, no glyph atlas.");
continue;
}
g_GfxDevice.SetUniformTexture("gamelab_main_tex", atlas->altas);
CheckGLError(
throw GLException(error);
);
vbo->Draw(s_TextMeshVBOLayout);
CheckGLError(
throw GLException(error);
);
g_GfxDevice.ResetUniformsState();
}
}
|