Skip to content

Commit

Permalink
fix: SDF shaders
Browse files Browse the repository at this point in the history
  • Loading branch information
hoffstadt committed May 20, 2024
1 parent cdba5fe commit da67709
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
42 changes: 42 additions & 0 deletions examples/example_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,41 @@ pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)

// setup draw
gptDraw->initialize(&ptAppData->tGraphics);

// builtin default font (proggy @ 13)
gptDraw->add_default_font(&ptAppData->tFontAtlas);

// typical font range (you can also add individual characters)
const plFontRange tRange = {
.iFirstCodePoint = 0x0020,
.uCharCount = 0x00FF - 0x0020
};

// adding another font
plFontConfig tFontConfig0 = {
.bSdf = false,
.fFontSize = 18.0f,
.uHOverSampling = 1,
.uVOverSampling = 1,
.ucOnEdgeValue = 255,
.iSdfPadding = 1
};
pl_sb_push(tFontConfig0.sbtRanges, tRange);
gptDraw->add_font_from_file_ttf(&ptAppData->tFontAtlas, tFontConfig0, "../data/pilotlight-assets-master/fonts/Cousine-Regular.ttf");

// adding previous font but as a signed distance field
plFontConfig tFontConfig1 = {
.bSdf = true,
.fFontSize = 18.0f,
.uHOverSampling = 1,
.uVOverSampling = 1,
.ucOnEdgeValue = 180,
.iSdfPadding = 1
};
pl_sb_push(tFontConfig1.sbtRanges, tRange);
gptDraw->add_font_from_file_ttf(&ptAppData->tFontAtlas, tFontConfig1, "../data/pilotlight-assets-master/fonts/Cousine-Regular.ttf");

// build font atlass
gptDraw->build_font_atlas(&ptAppData->tFontAtlas);

// register our app drawlist
Expand Down Expand Up @@ -230,6 +264,14 @@ pl_app_update(plAppData* ptAppData)
// drawing API usage
gptDraw->add_circle(ptAppData->ptFGLayer, (plVec2){120.0f, 120.0f}, 50.0f, (plVec4){1.0f, 1.0f, 0.0f, 1.0f}, 0, 1.0f);
gptDraw->add_circle_filled(ptAppData->ptBGLayer, (plVec2){100.0f, 100.0f}, 25.0f, (plVec4){1.0f, 0.0f, 1.0f, 1.0f}, 24);
gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[0], 13.0f, (plVec2){200.0f, 100.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Proggy @ 13 (loaded at 13)", 0.0f);
gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[0], 45.0f, (plVec2){200.0f, 115.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Proggy @ 45 (loaded at 13)", 0.0f);

gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[1], 18.0f, (plVec2){25.0f, 200.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Cousine @ 18, bitmap (loaded at 18)", 0.0f);
gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[1], 100.0f, (plVec2){25.0f, 220.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Cousine @ 100, bitmap (loaded at 18)", 0.0f);

gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[2], 18.0f, (plVec2){25.0f, 320.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Cousine @ 18, sdf (loaded at 18)", 0.0f);
gptDraw->add_text(ptAppData->ptFGLayer, &ptAppData->tFontAtlas.sbtFonts[2], 100.0f, (plVec2){25.0f, 340.0f}, (plVec4){1.0f, 1.0f, 1.0f, 1.0f}, "Cousine @ 100, sdf (loaded at 18)", 0.0f);

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~drawing prep~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
8 changes: 4 additions & 4 deletions extensions/pl_draw_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,11 @@ pl_initialize(plGraphics* ptGraphics)
// 2d
const plSamplerDesc tSamplerDesc = {
.tFilter = PL_FILTER_LINEAR,
.fMinMip = 0.0f,
.fMaxMip = 64.0f,
.fMinMip = -1000.0f,
.fMaxMip = 1000.0f,
.fMaxAnisotropy = 1.0f,
.tVerticalWrap = PL_WRAP_MODE_CLAMP,
.tHorizontalWrap = PL_WRAP_MODE_CLAMP,
.tVerticalWrap = PL_WRAP_MODE_WRAP,
.tHorizontalWrap = PL_WRAP_MODE_WRAP,
.tMipmapMode = PL_MIPMAP_MODE_LINEAR
};
gptCtx->tFontSampler = gptDevice->create_sampler(&ptGraphics->tDevice, &tSamplerDesc, "font sampler");
Expand Down
6 changes: 3 additions & 3 deletions shaders/glsl/draw_2d_sdf.frag
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ layout(location = 0) out vec4 fColor;

void main()
{
float fDistance = texture(sampler2D(tFontAtlas, tFontSampler), In.UV.st).a;
float fDistance = texture(sampler2D(tFontAtlas, tFontSampler), In.UV).a;
float fSmoothWidth = fwidth(fDistance);
float fAlpha = smoothstep(0.5 - fSmoothWidth, 0.5 + fSmoothWidth, fDistance);
vec3 fRgbVec = In.Color.rgb * texture(sampler2D(tFontAtlas, tFontSampler), In.UV.st).rgb;
fColor = vec4(fRgbVec, fAlpha);
vec3 fRgbVec = In.Color.rgb * texture(sampler2D(tFontAtlas, tFontSampler), In.UV).rgb;
fColor = vec4(fRgbVec, In.Color.a * fAlpha);
}
2 changes: 1 addition & 1 deletion shaders/metal/draw_2d_sdf.metal
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ fragment float4 fragment_main(
float smoothWidth = fwidth(distance);
float alpha = smoothstep(0.5 - smoothWidth, 0.5 + smoothWidth, distance);
float3 texColor = bg1.tFontAtlas.sample(bg0.tDefaultSampler, in.UV).rgb * float3(in.Color.rgb);
return float4(texColor, alpha);
return float4(texColor, in.Color.a * alpha);
}

0 comments on commit da67709

Please sign in to comment.