Vertex Extrusion
Jump to heading How does fast outline work?
Fast outline works by taking an object and rendering an extruded version of that object behind the original one.
They key of fast outline is: in what direction to we extrude each vertex?
Linework provides a few different options to control this direction.
Each option below is shown in an extreme way with an big outline width. For a lot of methods, bigger widths will result in an outline with gaps.
- Vertex Position (OS): Move each vertex along the vertex position in object space.
- Normalized Vertex Position (OS): Move each vertex along the normalized vertex position in object space.
- Normal Vector (OS): Move each vertex along the normal vector in object space.
- Vertex Color (OS): Move each vertex along the vertex color in object space.
- Normalized Vector (CS): Move each vertex along the normal vector in clip space.
- Normal Vector (SS): Move each vertex along the normal in screen space.
- Normal Vector (WS): Move each vertex along the normal in world space.
- Smoothed Normals: Move each vertex along the direction extracted from the UV8 channel in object space.
Jump to heading Smoothed normals
How does it work?
The last method listed above is Smoothed Normals. This method moves each vertex along a baked direction.
Imagine you have an object with sharp edges like a cube. If you were to extrude each vertex along the normal vector, you would get gaps at the edges since at the exact sharp transition, vertices would go in very different directions.
A solution for this is to smooth the normals. Because smoothing the normals directly would also possibly influence how the object is rendered (due to lighting calculations using the normal vector), we instead bake the normals into a separate channel. Linework uses the UV8 channel for this. By choosing the Smoothed Normals option, the vertex extrusion shader will use the baked, smoothed normals as the direction to extrude the vertices in.
How to enable smoothed normals?
To calculate the smoothed normals for an object and store them so that fast outline can use them, simply right-click on a mesh in your object folder, and enable Calculate Smoothed Normals toggle. To disable it, you can simply uncheck the toggle.
Using this option should give a smoother outline, even for objects with sharp edges.
Where are the smoothed normals stored?
Smoothed normals in Linework are implemented using an AssetPostprocessor. By right-clicking on a mesh and choosing to enable/disable Calculate Smoothed Normals, an asset label gets set/removed respectively for that mesh. An asset post processor then calculates the smoothed normals, if that label is present. The smoothed normals themselves, are stored in the UV8 channel of the mesh.
The relevant code is as follows.
mesh.SetUVs(7, SmoothNormalsBaker.ComputeSmoothedNormals(mesh));