Hello Guest

Author Topic: Sharp Fonts  (Read 5337 times)

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Sharp Fonts
« on: November 23, 2013, 09:22:14 am »
Hi,

in general I am a bit obsessed with sharp fonts (see my efforts on Cocos2D/iOS…  8)), and I am not too happy how far I can get with Unity & 2d Toolkit right now. I am just trying to apply the same fixing as described in my before-mentioned blog, but I just found also this blog on improving font scaling by applying trilinear filtering to the font material and using a shader with a mipbias of -0.65. This is pretended to be done on Unity with a modified Toolkit 2D shader, but beside a mentioning of how to achieve this, no more details are given. I tried to modify the BlendVertexColor shader like this:
Code: [Select]
fixed4 frag_mult(v2f_vct i) : COLOR
{
float4 coord;
coord.xy = i.texcoord;
coord.w = -0.65f;
fixed4 col = tex2Dbias(_MainTex, coord) * i.color;
return col;
}

but this doesn't work. Well, this was like heart sugery with a bread knife, since I have no clue about shaders at all and just wanted to give it a bold try, but to me that sounds like something useful as an additional shader for 2D toolkit, or? And you guys probably have no problem in creating such a working shader in a second  ;)

Regards, habitoti
« Last Edit: November 23, 2013, 10:34:46 am by habitoti »
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sharp Fonts
« Reply #1 on: November 23, 2013, 07:29:32 pm »
You can try assigning the mipmapBias on the texture. Eg. attach a behaviour on the text mesh, and on start, set renderer.sharedMaterial.mainTexture.mipMapBias = ...;
You can obviously also set it to trilinear there. Does that work for you?

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Re: Sharp Fonts
« Reply #2 on: November 24, 2013, 12:26:21 pm »
Yes, I can set it like this. But I don't see any difference, I have to admit…

Is there any best practice for scaling fonts? Is there a rule of thumb or something that you should not scale further down than, say,  50%, or only in so-and-so steps etc.? Can I add other base sizes to the same font texture?

Maybe a great feature would be a text management in tk2d that would allow for setting actual fonts sizes, and some magic behind the scenes would take the closest available bitmap size and scale it down down to come up with the requested font size. Worst case you start with just one large bitmap size, but -- like for the different resolutions -- I could simply add other sizes and Toolkit 2D text management would take care of choosing the best. Given that fonts is such a pain with Unity, that would be a great additional value proposition!

I volunteer for testing ;-)

Regards, habitoti
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sharp Fonts
« Reply #3 on: November 24, 2013, 05:01:49 pm »
Well, scaling fonts is no different to scaling anything else. If you have mipmaps, you should always scale by multiples of 2  to avoid interpolation. Bilinear interpolation will make things blurry.

Automatically picking the correct bitmaps is a pretty good idea. You can obviously do this by hand, by merging fonts from multiple sizes into a sprite collections, and switching fonts appropriately. The tricky thing is that you'll likely see a jump when downscaling. Since all offsets in bmfont happen to be ints, a 5 spacing between glyphs at 32 pix, will round down to 2 spacing or up to 3 at 16pix. This will mean you will see the characters jump slightly when switching to a lower font size... it can of course be made to work in specific cases when you have control over the fonts and data, its pretty tricky to make work with generic data transparently.

If you really want super sharp fonts that can scale, I suggest looking into signed distance field fonts. Its not without its limits, but it can work quite well in a controlled environment.

habitoti

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
    • Rombos Homepage
Re: Sharp Fonts
« Reply #4 on: November 24, 2013, 07:43:13 pm »
I am actually not looking for changing size at runtime, but a simple textcomponent where I just choose the font size at design time (as you would for a vector font), and tk2d would pick every other setting behind the scenes (which best bitmap data plus required scaling from there). If I am not happy how this turns out on a device later on, I simply add another rendered bitmap size, and tk2d would instantly adapt all these text components, taking the new available native size into account.
Checkout our homepage or visit the Rombos blog.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sharp Fonts
« Reply #5 on: November 24, 2013, 10:29:12 pm »
I'll put something on the roadmap trello. I can't promise a timescale or indeed if it will ever get implemented.