Real-Time Hatching in Unity
In this project I implement the real-time hatching rendering technique described in Emil Praun et al.'s paper “Real-Time Hatching” in the Unity game engine.
Hatching is a common artistic technique used to depict shading and textural effects by drawing closely spaced parallel lines. The denser and closer these lines are the darker an area would appear to be. In addition cross-hatching is commonly used with hatching to vary tone and texture of an image. Cross-hatching is the technique of overlapping hatch lines with a another set of lines in an orthogonal direction.
Emil Praun et al. describe a object-based NPR technique for creating hatching style images at interactive rates using tonal art maps and taking advantage of the GPU.
Tonal Art Maps
Tonal art maps (TAMs) can be described as a set of textures that depict different tonal or darkness values. Starting with the lightest tone and smallest mipmap level we continue to add strokes to the texture until the desired value is achieved at each tonal level.
Each Column of the tonal art map contains the texture from all lighter tones. The textures are toroidal allowing continuous smooth blending between tones and mipmap levels across triangle boundaries.
For this project, I built a TAM generation tool in Unity that allows users to specify a set of example stroke images to be used to create the hatching textures. The tool also allowed users to determine the stroke density and orientation per tonal level. This allows the user to have more control over the overall look of the hatching, rather than using the method described in Emil Praun et al.'s paper.
To create the illusion of smooth shading the tonal texture is applied by using a 6-way blend. First, calculate the light intensity at each vertex. Then, by taking the floor and ceiling tonal values of the three corners we can combine 6 tones into the final displayed texture.
This method takes advantage of modern GPUs since there is no data dependance between triangles and texture values can be interpolated. The TAMs are stored in the RGB channels of two textures, allowing 6 different tonal values plus pure black and white to be blended simultaneously across the mesh.
In Unity, mipmap levels had to be set one time manual during runtime and stored within the active textures. The blending coefficients are calculated in the vertex shader and then sent off to the fragment shader to handle the actual texture blending.
I chose to implement this technique in unity for many reasons. The primary motivation was the possibility of being able to use this technique in the production of a game in the future and/or to be shared with the Unity community. Unity also provided a familiar platform, visual editor, simple model importing, easy scene setup, simple mesh and texture manipulation, and quick access to GPU functionality and CG programming.
Much of this project could be improved upon. Here is a list of possible improvements for the future:
- Better TAM map generation to give a more natural, random hatching
- Better lighting model that includes specular reflections and multiple light sources
- Other TAMs such as stippling and fur
- Colored strokes
- Texturing of arbitrary geometry using the lapped textures method
Emil Praun, Hugues Hoppe, Matthew Webb, and Adam Finkelstein. 2001. Real-time hatching. InProceedings of the 28th annual conference on Computer graphics and interactive techniques(SIGGRAPH '01). ACM, New York, NY, USA, 581-. DOI=10.1145/383259.383328 http://doi.acm.org/10.1145/383259.383328
The link below shows a video of the technique in action.