How font cache works

I understand that Defold streams in glyphs into memory. Can more about this be explained?

cache_width and cache_height can define the generated font image, which is where the individual glyphs are streamed from. So it doesn’t really matter the size of this and can be as large as is needed because it’s never loaded into memory itself?

How are glyphs streamed in handled? Composed into paged texture atlases in memory?

Also what’s shadow_x and shadow_y for? Offset of shadow? Negative values work? Doesn’t work in preview atm? Shadow alpha greater than 0 seems to do something, but only when outline_alpha is greater than 0. At least looking preview only… preview just buggy?

shadow_blur has a max value of 4?

I can answer the shadow-questions, the streaming can probably be more accurately explained by @sven.

This is a case where in my opinion we have transgressed into the domain of content and convenience too much. We thought it would be great to have a blurred shadow for fonts because it just produces great visual quality. The technique of doing them for bitmaps is simply to use the blue channel in the texture to describe a blurred version of the glyphs. This means you can offset this texture when rendering to produce an underlying soft shadow, which must produce it even when there is none. This turned out to be too expensive for the lowspec cases. So in the end we had to replace the default font shader with something cheaper. We also had to leave the shadow parameters in because otherwise we would break the content (and it’s also nice to have it for people who know what to do with it). But it certainly creates a confusion in the tool.

A better model is to do something like how we deal with sprites and shader constants. They are exposed dynamically and depend on the materials.

1 Like

It looks like Version 1.2.22 is when you introduced the new shader which removed shadows for fonts. I’m assuming I could grab an earlier version and use that version’s shader to enable shadows for fonts? It’s on my big todo list to test…

Unfortunately I don’t think versions that old are still available. However, I think @Andreas_Jirenius tried to resurrect it at one point?

AUG 12, 2013 is the date for the 1.2.22 release, so maybe if there is still an old repository with the old development on it could look there?

You can try these!

Vertex program:

uniform mediump mat4 view_proj;
uniform mediump mat4 world;

varying mediump vec2 var_texcoord0;
varying lowp vec4 var_face_color;
varying lowp vec4 var_outline_color;
varying lowp vec4 var_shadow_color;

attribute mediump vec4 position;
attribute mediump vec2 texcoord0;
attribute lowp vec4 face_color;
attribute lowp vec4 outline_color;
attribute lowp vec4 shadow_color;

void main()
{
    var_texcoord0 = texcoord0;
    var_face_color = face_color;
    var_outline_color = outline_color;
    var_shadow_color = shadow_color;
    gl_Position = view_proj * vec4(position.x, position.y, position.z, 1.0);
}

Fragment program:

varying mediump vec4 position;
varying mediump vec2 var_texcoord0;
varying lowp vec4 var_face_color;
varying lowp vec4 var_outline_color;
varying lowp vec4 var_shadow_color;

uniform lowp vec4 texture_size_recip;
uniform lowp sampler2D texture;

void main()
{
    vec4 t = texture2D(texture, var_texcoord0.xy);

    vec4 fc = var_face_color;
    vec4 oc = var_outline_color;
    vec4 sc = var_shadow_color;

    fc.w *= t.x;
    oc.w *= t.y;
    sc.w *= t.z;

    gl_FragColor = fc + oc + sc;
}
2 Likes

Just changing the programs text shows as this. I’m going to mess with the fragment program now…

Alright, sorry, it was a stab in the dark. The shadow data should come with the vertices anyway, you could start by doing gl_FragColor = var_shadow_color to make sure.

I had set the shadow color to this color so it looks like something is working.

What it looks like with standard shader.

By the way, it appears that turning anti-aliasing off disables some features that shouldn’t be disabled.

Try to multiply it with t.z which describes the shadow from the texture:
gl_FragColor = var_shadow_color * t.z;

This is fun, like remote coding! :slight_smile:

1 Like

Changed the fp to this

varying mediump vec4 position;
varying mediump vec2 var_texcoord0;
varying lowp vec4 var_face_color;
varying lowp vec4 var_outline_color;
varying lowp vec4 var_shadow_color;

uniform lowp vec4 texture_size_recip;
uniform lowp sampler2D texture;

void main()
{
    vec4 t = texture2D(texture, var_texcoord0.xy);

    vec4 fc = var_face_color;
    vec4 oc = var_outline_color;
    vec4 sc = var_shadow_color;

    fc.w *= t.x;
    oc.w *= t.y;
    sc.w *= t.z;

    gl_FragColor = fc * t.x + oc * t.y + sc * t.z;
}

:grin:

shadow_x and shadow_y appear to be ignored. What I would like is to have solid pixel (for my pixel font) shadows which are offset.

Here’s another look I’m trying to achieve in another project (image is what it looks like in the PSD):

Also again antialias being off seems to disable shadow/outline.


2 Likes