A downloadable tool

[Requires Odin Inspector to function]

An outline shader with script that allows easy outlines on any non-skinned object for URP.

Main features: 

  • Adds outline to any non skinned object with customizable color and thickness
  • Normal prediction for outlining meshes with hard normals
  • Automatically detects meshes to outline

This outline is used in the VR Gunslinger project.


Source Code Explanation

Shader

The shader that is used to draw the outline has been made with shader graph and works in the following way.

The shader draws only the vertices of the mesh that are facing inwards, this way the outline doesn't cover the front parts of the object.


The shader also scales the mesh slightly with the Thickness paramater so that the outline does show on the edges of the mesh. The mesh can be scaled in two ways. One way uses the normal vector of the mesh to scale the mesh, this is suitable for meshes with smooth normals.

The other way the shader can scale the mesh is by using the normalized position in local space of the vertices to estimate it's normal. This method is useful for meshes with hard normals where using the vertex normal creates gaps in the outline.

The shader uses one of these methods depending on the value of the "Hard Normals" boolean keyword.


Next the shader multiplies the normal vector with the thickness value and adds this to the position of the vertex to scale the mesh.


Script

The script included in this project is made to streamline the process of adding outlines to objects. The script uses the Odin Inspector plugin to organize the inspector window. 

Here you can see the script as it is attached to the gun object:



To draw the outline of the mesh at the right spot I created a class that stores all the necessary data.


Next in the Late Update loop the script draws the mesh on the transform's position using the Graphics.DrawMesh() method.


Besides drawing a mesh with the outline material the script also allows the user to change the properties of the outline in the inspector. To implement this I used a Material Property Block that gets updated whenever a value is changed in the inspector.


Finally to streamline the setup of the outline I added a method that finds all meshes in the GameObject's children and adds them to the OutlineData array. This method is triggered by the "Get Meshes" button and is called in the Reset() method so that is automatically finds meshes when you add the script to a GameObject.


Known issues:

  • No support for skinned mesh renderers or any rigged meshes

Download

Download
Toon Outline.unitypackage 8 kB