31 Jul 2014
July 2014 - Typogenic
unitytextopensourcedigitalresearch

Well this one isn't a game at all. Not even a little. However, it's something that will likely make its way into many of my future projects, and hopefully into many other people's projects as well. This month I spent a chunk of my time adding some features to a free library designed to improve the 3D text rendering options for Unity developers.

This month's project was to contribute new features, features which I've needed in the past, to Thomas Hourdel's Typogenic library.

Doesn't Unity Do That?

Sort of. Last year, I worked on a project that integrated a Twine hypertext story with Unity visuals. This required extensive use of highly-legible dynamic text blocks which could style some text differently inline with other text, and detect clicks on a per-word level. Using Unity's standard features allowed me to render text, which after some trial and error was acceptably legible but not particularly pleasing. Each text element could only use a single material, so all text was rendered the same, and there was no simple method of detecting where the player clicked within the text.

My solution, for better or worse, was a system that broke a string apart into individual words, generating a separate text mesh and collider for each. That way each word could have a different appearance and could react to mouse clicks. This is not at all efficient, though I could get away with it since the game was otherwise fairly lightweight and simple, but the end result was clunky and fiddly and not a process I ever wanted to repeat.

I'd run into situations elsewhere that made me wish the Unity text mesh had a few more features and possibilities, and that project in particular left me wishing I had a better option available. Unfortunately, as a spare time hobby game dev, paying for libraries just to get better text rendering wasn't a very attractive option either.

Typogenic

Earlier this month, I spotted this reddit post about a new, free text rendering library for Unity3D, so I was hoping to find something that would solve at least some of the problems I had run into. When I tried it out, I was happy to see if worked so well at what it did, and the text looked great, but it still didn't solve some of the problems I'd had before. Since Typogenic is an open source library, I thought I'd dig into the code a bit and see what I could get working.

In the end, I identified three areas where I could expand Typogenic's features:

Shadows
Typogenic includes shaders which allow the text to be textured or flat/gradient color, and to be lit or unlit. I created copies of each that add a shadowcaster pass, which allows the text to cast the expected shadows (i.e. shadows which match the shape of the rendered glyphs).
Mixed Materials
Typogenic dynamically creates the text mesh at runtime, and an attached renderer component renders the text mesh with the chosen material. I modified the parser to accept simple numeric escape sequences in the displayed text string (\0, \1, \2, etc.) which switch the active material, and modified the mesh generation logic to break the mesh into submeshes and add each glyph to the appropriate submesh, so a renderer with multiple materials will result in the text being rendered with multiple materials.
Click Support
I added logic to resize any attached BoxCollider to match the bounds of the generated text mesh, and to record the position of each glyph. When a click event on an attached collider is received, the component can identify which glyph was clicked, and broadcasts a message indicating that a click event was received at a specific index of the displayed string in a particular text mesh object.

I cleaned up my testing project and put together this preview to demonstrate these features (click to embiggen):


I've spoken to Thomas about integrating these changes into the main Typogenic package, which as of this writing should be happening soon, and in the meantime I've made these changes available directly as a downloadable Unity package below.

The Verdict

Again, not a game, but I can definitely see all of this coming in handy in the future, especially if I decide to dabble in Twine/Unity integration again, and the rendering features of Typogenic itself will make it all much prettier than before. So this was definitely a good use of my limited game dev time this month, and hopefully will come in handy for others as well.