SDF font rendering

This commit is contained in:
Antoine Pilote
2021-06-03 22:08:38 -04:00
parent f01aeb4910
commit af70933eca
17 changed files with 522 additions and 20 deletions

View File

@@ -0,0 +1,52 @@
#shader vertex
#version 460 core
layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 UV;
uniform mat4 model;
uniform mat4 projection;
out vec2 a_UV;
void main()
{
a_UV = UV;
gl_Position = projection * model * vec4(Position, 1.0);
}
#shader fragment
#version 460 core
out vec4 FragColor;
in vec2 a_UV;
uniform vec2 texPos;
uniform vec2 texScale;
uniform sampler2D msdf;
uniform vec4 bgColor;
uniform vec4 fgColor;
uniform float pxRange; // set to distance field's pixel range
float screenPxRange(vec2 coord) {
vec2 unitRange = vec2(pxRange) / vec2(textureSize(msdf, 0));
vec2 screenTexSize = vec2(1.0) / fwidth(coord);
return max(0.5 * dot(unitRange, screenTexSize), 1.0);
}
float median(float r, float g, float b) {
return max(min(r, g), min(max(r, g), b));
}
void main() {
vec2 textSize = textureSize(msdf, 0);
vec2 uv = vec2(mix(texPos.x / textSize.x,texScale.x / textSize.x, a_UV.x),
mix(texScale.y / textSize.y, texPos.y / textSize.y, a_UV.y));
vec3 msd = texture(msdf, uv).rgb;
float sd = median(msd.r, msd.g, msd.b);
float screenPxDistance = screenPxRange(uv) * (sd - 0.5);
float alpha = smoothstep(0.5 - 1/16.0, 0.5 + 1 / 16.0, sd);
FragColor = mix(bgColor, fgColor, alpha);
}

View File

@@ -19,7 +19,7 @@
#include "src/Resource/Project.h"
#include <src/Scene/Entities/Components/LuaScriptComponent.h>
#include <src/Core/Logger.h>
Ref<UI::UserInterface> userInterface;
ImFont* normalFont;
ImFont* EditorInterface::bigIconFont;
void EditorInterface::Init()
@@ -56,6 +56,19 @@ void EditorInterface::DrawViewport()
}
}
ImGui::End();
if (ImGui::Begin("SDF FONT"))
{
ImVec2 regionAvail = ImGui::GetContentRegionAvail();
if (userInterface)
{
auto id = userInterface->font->FontAtlas->GetID();
ImGui::Image((void*)id, regionAvail, ImVec2(0, 1), ImVec2(1, 0));
}
}
ImGui::End();
if(ImGui::Begin("Viewport"))
{
@@ -222,7 +235,6 @@ void EditorInterface::DrawSceneTree()
// Buttons to add and remove entity.
ImGui::BeginChild("Buttons", ImVec2(300, 20), false);
{
// Add entity.
if (ImGui::Button("Add"))
Engine::GetCurrentScene()->CreateEntity("Entity");
@@ -895,7 +907,7 @@ void OpenProject()
Engine::LoadProject(project);
// Create new interface named test.
Ref<UI::UserInterface> userInterface = UI::UserInterface::New("test");
userInterface = UI::UserInterface::New("test");
// Set current interface running.