/
text.vert
140 lines (110 loc) · 4.24 KB
/
text.vert
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
#version 450
#extension GL_ARB_separate_shader_objects : enable
#pragma import_defines (GPU_LAYOUT, CPU_LAYOUT, BILLBOARD)
#ifdef GPU_LAYOUT
// GPU layout provides computes vertex values based on layout computed in the vertex shader
#define GLYPH_DIMENSIONS 0
#define GLYPH_BEARINGS 1
#define GLYPH_UVRECT 2
// specialization constants
layout(constant_id = 0) const uint numTextIndices = 256;
layout(set = 0, binding = 1) uniform sampler2D glyphMetricsSampler;
layout(set = 1, binding = 0) uniform TextLayout {
vec4 position;
vec4 horizontal;
vec4 vertical;
vec4 color;
vec4 outlineColor;
float outlineWidth;
} textLayout;
layout(set = 1, binding = 1) uniform TextIndices {
uvec4 glyph_index[numTextIndices];
} text;
#else
// CPU layout provides all vertex data
layout(location = 1) in vec4 inColor;
layout(location = 2) in vec4 inOutlineColor;
layout(location = 3) in float inOutlineWidth;
layout(location = 4) in vec3 inTexCoord;
#ifdef BILLBOARD
layout(location = 5) in vec4 inCenterAndAutoScaleDistance;
#endif
#endif
layout(push_constant) uniform PushConstants {
mat4 projection;
mat4 modelview;
} pc;
layout(location = 0) in vec3 inPosition;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 outlineColor;
layout(location = 2) out float outlineWidth;
layout(location = 3) out vec2 fragTexCoord;
out gl_PerVertex {
vec4 gl_Position;
};
#ifdef BILLBOARD
mat4 computeBillboadMatrix(vec4 center_eye, float autoScaleDistance)
{
float distance = -center_eye.z;
float scale = (distance < autoScaleDistance) ? distance/autoScaleDistance : 1.0;
mat4 S = mat4(scale, 0.0, 0.0, 0.0,
0.0, scale, 0.0, 0.0,
0.0, 0.0, scale, 0.0,
0.0, 0.0, 0.0, 1.0);
mat4 T = mat4(1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
center_eye.x, center_eye.y, center_eye.z, 1.0);
return T*S;
}
#endif
void main() {
#ifdef GPU_LAYOUT
// compute the position of the glyph
float horiAdvance = 0.0;
float vertAdvance = 0.0;
for(uint i=0; i<gl_InstanceIndex; ++i)
{
uint glyph_index = text.glyph_index[i / 4][i % 4];
if (glyph_index==0)
{
// treat as a newlline
vertAdvance -= 1.0;
horiAdvance = 0.0;
}
else
{
horiAdvance += texture(glyphMetricsSampler, vec2(GLYPH_DIMENSIONS, glyph_index))[2];
}
}
// compute the position of vertex
uint glyph_index = text.glyph_index[gl_InstanceIndex / 4][gl_InstanceIndex % 4];
vec4 dimensions = texture(glyphMetricsSampler, vec2(GLYPH_DIMENSIONS, glyph_index));
vec4 bearings = texture(glyphMetricsSampler, vec2(GLYPH_BEARINGS, glyph_index));
vec4 uv_rec = texture(glyphMetricsSampler, vec2(GLYPH_UVRECT, glyph_index));
vec3 pos = textLayout.horizontal.xyz * (horiAdvance + textLayout.horizontal.w + bearings.x + inPosition.x * dimensions.x) +
textLayout.vertical.xyz * (vertAdvance + textLayout.vertical.w + bearings.y + (inPosition.y-1.0) * dimensions.y);
#ifdef BILLBOARD
gl_Position = (pc.projection * computeBillboadMatrix(pc.modelview * vec4(textLayout.position.xyz, 1.0), textLayout.position.w)) * vec4(pos, 1.0);
#else
gl_Position = (pc.projection * pc.modelview) * vec4(textLayout.position.xyz + pos, 1.0);
#endif
gl_Position.z += inPosition.z*0.0001;
fragColor = textLayout.color;
outlineColor = textLayout.outlineColor;
outlineWidth = textLayout.outlineWidth;
fragTexCoord = vec2(mix(uv_rec[0], uv_rec[2], inPosition.x), mix(uv_rec[1], uv_rec[3], inPosition.y));
#else
// CPU layout provides all vertex data
#ifdef BILLBOARD
gl_Position = (pc.projection * computeBillboadMatrix(pc.modelview * vec4(inCenterAndAutoScaleDistance.xyz, 1.0), inCenterAndAutoScaleDistance.w)) * vec4(inPosition, 1.0);
#else
gl_Position = (pc.projection * pc.modelview) * vec4(inPosition, 1.0);
#endif
gl_Position.z += inTexCoord.z*0.0001;
fragColor = inColor;
outlineColor = inOutlineColor;
outlineWidth = inOutlineWidth;
fragTexCoord = inTexCoord.xy;
#endif
}