Skip to content

Commit 4bcb9c2

Browse files
committed
Simplified text drawing shader
1 parent 179bd46 commit 4bcb9c2

File tree

1 file changed

+4
-30
lines changed

1 file changed

+4
-30
lines changed

nimx/private/text_drawing.nim

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,15 @@ void main() {
2222
"""
2323
uniform sampler2D texUnit;
2424
uniform vec4 fillColor;
25-
uniform float preScale;
2625
2726
varying vec2 vTexCoord;
2827
2928
varying vec2 vPos;
3029
31-
float thresholdFunc(float glyphScale)
32-
{
33-
float base = 0.5;
34-
float baseDev = 0.065;
35-
float devScaleMin = 0.15;
36-
float devScaleMax = 0.3;
37-
return base - ((clamp(glyphScale, devScaleMin, devScaleMax) - devScaleMin) / (devScaleMax - devScaleMin) * -baseDev + baseDev);
38-
}
39-
40-
float spreadFunc(float glyphScale)
41-
{
42-
float range = 0.06;
43-
return range / glyphScale;
44-
}
45-
46-
void compose()
47-
{
48-
float scale = preScale / fwidth(vTexCoord.x);
49-
scale = abs(scale);
50-
float aBase = thresholdFunc(scale);
51-
float aRange = spreadFunc(scale);
52-
float aMin = max(0.0, aBase - aRange);
53-
float aMax = min(aBase + aRange, 1.0);
54-
30+
void compose() {
5531
float dist = texture2D(texUnit, vTexCoord).a;
56-
float alpha = smoothstep(aMin, aMax, dist);
32+
float d = fwidth(dist);
33+
float alpha = smoothstep(0.5 - d, 0.5 + d, dist);
5734
gl_FragColor = vec4(fillColor.rgb, alpha * fillColor.a);
5835
}
5936
""", false)
@@ -133,7 +110,6 @@ void main() {
133110
"""
134111
uniform sampler2D texUnit;
135112
uniform vec4 fillColor;
136-
uniform float preScale;
137113
138114
varying vec2 vTexCoord;
139115
@@ -156,6 +132,7 @@ float spreadFunc(float glyphScale)
156132
157133
void subpixelCompose()
158134
{
135+
float preScale = 1.0 / 320.0; // Magic constant...
159136
float scale = preScale / fwidth(vTexCoord.x); // 0.25 * 1.0 / (dFdx(vTexCoord.x) / (16.0 / 1280.0));
160137
scale = abs(scale);
161138
float aBase = thresholdFunc(scale);
@@ -243,8 +220,6 @@ proc drawText*(c: GraphicsContext, font: Font, pt: var Point, text: openarray[ch
243220
when defined(android) or defined(ios):
244221
subpixelDraw = false
245222

246-
let preScale = 1.0 / 320.0 # magic constant...
247-
248223
if subpixelDraw:
249224
if gl.getParami(gl.BLEND_SRC_ALPHA) != gl.SRC_ALPHA.GLint or gl.getParami(gl.BLEND_DST_ALPHA) != gl.ONE_MINUS_SRC_ALPHA.GLint:
250225
subpixelDraw = false
@@ -260,7 +235,6 @@ proc drawText*(c: GraphicsContext, font: Font, pt: var Point, text: openarray[ch
260235

261236
compositionDrawingDefinitions(cc, c, gl)
262237
setUniform("fillColor", c.fillColor)
263-
setUniform("preScale", preScale)
264238

265239
gl.uniformMatrix4fv(uniformLocation("uModelViewProjectionMatrix"), false, c.transform)
266240
setupPosteffectUniforms(cc)

0 commit comments

Comments
 (0)