Skip to content

Commit

Permalink
improve font readability
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Jan 9, 2024
1 parent b437592 commit 94de064
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion data/cage/shader/engine/engine.assets
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

[]
scheme = pack
engine.pack
Expand All @@ -8,5 +7,6 @@ scheme = shader
billboard.glsl
blit.glsl
decal.glsl
font.glsl
skybox.glsl
standard.glsl
3 changes: 1 addition & 2 deletions data/cage/shader/engine/engine.pack
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

[]
blit.glsl
font.glsl
standard.glsl

48 changes: 48 additions & 0 deletions data/cage/shader/engine/font.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
$include ../shaderConventions.h

$define shader vertex

struct InstanceStruct
{
vec4 wrld;
vec4 text;
};
layout(std140, binding = 2) uniform Instances
{
InstanceStruct instances[512];
};

layout(location = 0) uniform mat4 uniMvp;

layout(location = CAGE_SHADER_ATTRIB_IN_POSITION) in vec3 inPosition;
layout(location = CAGE_SHADER_ATTRIB_IN_UV) in vec2 inUv;

out vec2 varUv;

void main()
{
InstanceStruct inst = instances[gl_InstanceID];
gl_Position = uniMvp * vec4(inPosition.xy * inst.wrld.zw + inst.wrld.xy, 0.0, 1.0);
varUv = (inPosition.xy * inst.text.zw + inst.text.xy);
}

$define shader fragment

layout(binding = 0) uniform sampler2D uniTexture;
layout(location = 4) uniform vec3 uniColor;

in vec2 varUv;
out vec4 outColor;

float median(vec3 v)
{
return max(min(v.r, v.g), min(max(v.r, v.g), v.b));
}

void main()
{
float sd = median(texture(uniTexture, varUv).rgb);
float edgeWidth = fwidth(sd);
float opacity = smoothstep(0.5 - edgeWidth, 0.5 + edgeWidth, sd);
outColor = vec4(uniColor, opacity);
}
10 changes: 4 additions & 6 deletions data/cage/shader/gui/font.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

$include ../shaderConventions.h

$define shader vertex
Expand Down Expand Up @@ -30,11 +29,10 @@ void main()
$define shader fragment

layout(binding = 0) uniform sampler2D uniTexture;

layout(location = 4) uniform vec3 uniColor;
layout(location = 15) uniform float uniScreenPxRange;

in vec2 varUv;

out vec4 outColor;

float median(vec3 v)
Expand All @@ -44,8 +42,8 @@ float median(vec3 v)

void main()
{
float dist = median(texture(uniTexture, varUv).rgb);
float edgeWidth = length(vec2(dFdx(dist), dFdy(dist))) * 0.70710678118654757;
float opacity = smoothstep(0.5 - edgeWidth, 0.5 + edgeWidth, dist);
float sd = median(texture(uniTexture, varUv).rgb);
float screenPxDistance = uniScreenPxRange * (sd - 0.5);
float opacity = clamp(screenPxDistance + 0.5, 0, 1);
outColor = vec4(uniColor, opacity);
}
2 changes: 1 addition & 1 deletion sources/asset-processor/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace
const Real tx = -l + .5 * (wi - wf);
const Real ty = -b + .5 * (hi - hf);
msdfgen::Bitmap<float, 3> msdf(wi, hi);
msdfgen::generateMSDF(msdf, shape, msdfgen::Projection(1.0, from(Vec2(tx, ty))), 2.5);
msdfgen::generateMSDF(msdf, shape, msdfgen::Projection(1.0, from(Vec2(tx, ty))), 6);

Holder<Image> png = newImage();
png->initialize(wi, hi, 3);
Expand Down
3 changes: 1 addition & 2 deletions sources/include/cage-engine/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace cage
{
class RenderQueue;
class Model;
class ShaderProgram;

struct CAGE_ENGINE_API FontFormat
{
Expand Down Expand Up @@ -45,7 +44,7 @@ namespace cage
Vec2 size(PointerRange<const uint32> glyphs, const FontFormat &format) const;
Vec2 size(PointerRange<const uint32> glyphs, const FontFormat &format, const Vec2 &mousePosition, uint32 &cursor) const;

void render(RenderQueue *queue, const Holder<Model> &model, const Holder<ShaderProgram> &shader, PointerRange<const uint32> glyphs, const FontFormat &format, uint32 cursor = m) const;
void render(RenderQueue *queue, const Holder<Model> &model, PointerRange<const uint32> glyphs, const FontFormat &format, uint32 cursor = m) const;
};

CAGE_ENGINE_API Holder<Font> newFont();
Expand Down
12 changes: 1 addition & 11 deletions sources/libengine/graphics/font.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
#include <algorithm>
#include <cstring>
#include <vector>

#include <cage-core/geometry.h>
#include <cage-core/pointerRangeHolder.h>
#include <cage-core/serialization.h>
#include <cage-core/utf.h>
#include <cage-engine/assetStructs.h>
#include <cage-engine/font.h>
#include <cage-engine/graphicsError.h>
#include <cage-engine/opengl.h>
#include <cage-engine/renderQueue.h>
#include <cage-engine/shaderConventions.h>
#include <cage-engine/texture.h>

namespace cage
Expand All @@ -38,7 +31,6 @@ namespace cage
struct ProcessData
{
Holder<Model> model;
Holder<ShaderProgram> shader;
RenderQueue *renderQueue = nullptr;
std::vector<Instance> instances;
PointerRange<const uint32> glyphs;
Expand Down Expand Up @@ -221,7 +213,6 @@ namespace cage
if (data.renderQueue)
{
data.renderQueue->bind(tex, 0);
data.renderQueue->bind(data.shader);
const uint32 s = numeric_cast<uint32>(data.instances.size());
const uint32 a = s / MaxCharacters;
const uint32 b = s - a * MaxCharacters;
Expand Down Expand Up @@ -402,14 +393,13 @@ namespace cage
return data.outSize;
}

void Font::render(RenderQueue *queue, const Holder<Model> &model, const Holder<ShaderProgram> &shader, PointerRange<const uint32> glyphs, const FontFormat &format, uint32 cursor) const
void Font::render(RenderQueue *queue, const Holder<Model> &model, PointerRange<const uint32> glyphs, const FontFormat &format, uint32 cursor) const
{
if (format.wrapWidth <= 0 || format.size <= 0)
return;
const FontImpl *impl = (const FontImpl *)this;
ProcessData data;
data.model = model.share();
data.shader = shader.share();
data.renderQueue = queue;
data.format = &format;
data.glyphs = glyphs;
Expand Down
4 changes: 2 additions & 2 deletions sources/libengine/graphics/renderPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ namespace cage
shaderVisualizeColor = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/color.glsl")));
shaderVisualizeDepth = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/depth.glsl")));
shaderVisualizeMonochromatic = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/visualize/monochromatic.glsl")));
shaderFont = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/gui/font.glsl")));
shaderFont = defaultProgram(assets->get<AssetSchemeIndexShaderProgram, MultiShaderProgram>(HashString("cage/shader/engine/font.glsl")));
CAGE_ASSERT(shaderBlit);
onDemand->process();

Expand Down Expand Up @@ -526,7 +526,7 @@ namespace cage
const Holder<RenderQueue> &renderQueue = data.renderQueue;
renderQueue->uniform(shaderFont, 0, data.viewProj * text.model);
renderQueue->uniform(shaderFont, 4, text.color);
text.font->render(+renderQueue, modelSquare, shaderFont, text.glyphs, text.format);
text.font->render(+renderQueue, modelSquare, text.glyphs, text.format);
}

template<RenderModeEnum RenderMode>
Expand Down
6 changes: 3 additions & 3 deletions sources/libengine/gui/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ namespace cage
if (!item->font)
return;
setClip(item->hierarchy);
data.transform = item->transform;
data.format = item->format;
data.font = item->font.share();
item->glyphs = std::move(item->glyphs);
data.glyphs = item->glyphs.share();
data.color = item->color;
if (disabled)
Expand All @@ -92,6 +90,7 @@ namespace cage
position *= item->hierarchy->impl->pointsScale;
data.transform = transpose(Mat4(2.0 / orr[0], 0, 0, 2.0 * position[0] / orr[0] - 1.0, 0, 2.0 / orr[1], 0, 1.0 - 2.0 * position[1] / orr[1], 0, 0, 1, 0, 0, 0, 0, 1));
data.format.wrapWidth = size[0] * item->hierarchy->impl->pointsScale;
data.screenPxRange = 6 * item->hierarchy->impl->pointsScale; // 6 is value taken from font asset processor
}

RenderableText::~RenderableText()
Expand All @@ -106,7 +105,8 @@ namespace cage
q->bind(shader);
q->uniform(shader, 0, data.transform);
q->uniform(shader, 4, data.color);
data.font->render(q, impl->graphicsData.fontModel, impl->graphicsData.fontShader, data.glyphs, data.format, data.cursor);
q->uniform(shader, 15, data.screenPxRange);
data.font->render(q, impl->graphicsData.fontModel, data.glyphs, data.format, data.cursor);
}

RenderableImage::RenderableImage(ImageItem *item, Vec2 position, Vec2 size, bool disabled) : RenderableBase(item->hierarchy->impl)
Expand Down
1 change: 1 addition & 0 deletions sources/libengine/gui/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ namespace cage
Holder<PointerRange<uint32>> glyphs;
Vec3 color = Vec3::Nan();
uint32 cursor = m;
Real screenPxRange;
};

struct TextItem : private Immovable, public CommonTextData
Expand Down

0 comments on commit 94de064

Please sign in to comment.