Making a silhouette outline shader

I’m trying to make an outline shader that only shows outline from the model’s silhouette, to be used as indication that the character has been selected.

the current problem with this one is the outline doesn’t show in the feet, because of the offset, it gets hidden by the ground.

I want the outline to be above the terrain, but below the character.

alt text

Here is the code:

Shader "Outlined/Diffuse" {
	Properties {
		_Color ("Main Color", Color) = (.5,.5,.5,1)
		_OutlineColor ("Outline Color", Color) = (0,0,0,1)
		_Outline ("Outline width", Range (.002, 0.03)) = .005
		_MainTex ("Base (RGB)", 2D) = "white" { }
	}
	
CGINCLUDE
#include "UnityCG.cginc"

struct appdata {
	float4 vertex : POSITION;
	float3 normal : NORMAL;
};

struct v2f {
	float4 pos : POSITION;
	float4 color : COLOR;
};

uniform float _Outline;
uniform float4 _OutlineColor;

v2f vert(appdata v) {
	// just make a copy of incoming vertex data but scaled according to normal direction
	v2f o;
	o.pos = mul(UNITY_MATRIX_MVP, v.vertex);

	float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
	float2 offset = TransformViewToProjection(norm.xy);

	o.pos.xy += offset * o.pos.z * _Outline;
	o.color = _OutlineColor;
	return o;
}
ENDCG

	SubShader {
		//Tags {"Queue" = "Overlay" }
CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
}
ENDCG

		// note that a vertex shader is specified here but its using the one above
		Pass {
			Name "OUTLINE"
			Tags { "LightMode" = "Always" "Queue" = "Overlay" }
			Cull Front
			ZWrite On
			ZTest LEqual
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha
			Offset 15,15

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			half4 frag(v2f i) :COLOR { return i.color; }
			ENDCG
		}
	}
	
	SubShader {
		Tags {"Queue" = "Overlay" }
CGPROGRAM
#pragma surface surf Lambert

sampler2D _MainTex;
fixed4 _Color;

struct Input {
	float2 uv_MainTex;
};

void surf (Input IN, inout SurfaceOutput o) {
	fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
	o.Albedo = c.rgb;
	o.Alpha = c.a;
}
ENDCG

		Pass {
			Name "OUTLINE"
			Tags { "LightMode" = "Always" }
			Cull Front
			ZWrite On
			ColorMask RGB
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma exclude_renderers gles xbox360 ps3
			ENDCG
			SetTexture [_MainTex] { combine primary }
		}
	}
	
	Fallback "Diffuse"
}

this is not exactly what asked for, but methinks this is a good alternative to do outline with low CPU/GPU load. real outline will take much more improvements to achieve.

    Shader "Unity Answers/Diffuse Rim"
    {
    	Properties
    	{
    		_MainTex		("Base (RGB)",		2D)						= "white" { }
    		_Color			("Main Color",		Color)					= (.5,.5,.5,1)
    		_OutlineColor	("Outline Color",	Color)					= (0,0,0,1)
    		_OutlineOffset	("Outline offset",	Range (0, 1))			= 0.1
    		_OutlineSize	("Outline size",	Range (0, 1))			= 0.1
    	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }

		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		fixed4 _Color;

		struct Input
		{
			float2 uv_MainTex;
		};

		void surf (Input IN, inout SurfaceOutput o)
		{
			fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG

		//ouline pass
		Pass
		{
			Blend SrcAlpha OneMinusSrcAlpha

			Cull Back

			Offset -1, -1

			Lighting Off
			
			ZWrite Off
		
			CGPROGRAM

			#pragma vertex vert
			#pragma fragment frag

			float _OutlineOffset;
			float _OutlineSize;
			fixed4 _OutlineColor;

			uniform float4x4 _Object2World, _World2Object;
			uniform float3 _WorldSpaceCameraPos;
			uniform float4 _Time;

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float4 pos : SV_POSITION; // project view position
				float4 wPos : TEXCOORD0; // world-based position
				float4 wNor : TEXCOORD1; // world-based normal
			};

			v2f vert(appdata v)
			{
				v2f o;

				v.vertex.xyz += _OutlineOffset * v.normal;
				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
				o.wPos = mul(_Object2World, v.vertex);
				o.wNor = mul(_Object2World, float4(v.normal.xyz, 0));

				return o;
			}

			half4 frag(v2f i) : COLOR
			{
				float3 worldNormal = normalize(i.wNor.xyz);
				float3 worldToCameraDirection = normalize(_WorldSpaceCameraPos - i.wPos.xyz);
				half4 answer = _OutlineColor;
				answer.a *= saturate((_OutlineSize - dot(worldNormal, worldToCameraDirection)) * 100f);
				return answer;
			}

			ENDCG
		}
	}
}

I’ll post this as an answer because it’s another possibility, not sure if it’s costly when scaled.

It’s simple but may prove to be unpractical to manage at some point(in case you modify the camera a lot).

Using the first shader posted, you duplicate your camera and create one new layer, let’s call it ‘Outlined’.

You select your object that has the mesh with that shader, and change the layer to ‘Outlined’.

On one camera, you only change the culling mask value, removing the ‘Outlined’ layer from it. On the other, you have to change three values:

  • On “Clear Flags” change to “Depth only”;
  • On “Culling mask”, select Nothing, then select only the ‘Outlined’ layer;
  • Lastly, set the “Depth” value to 0(while the other camera will be set to -1);

This works properly and may be a valid option.

Won’t disabling the depth test with
ZTest Always
for the outline-mesh do the trick?
Also, this will cause the outline to appear “through” other objects - but since you are doing this to indicate selection, this is probably acceptable.

Old question, but anyway it seems important enough to share this answer: I found a really good source here although I modified the final result (very slightly). It´s not perfect, but if your camera is not too close to the object and the outline is thin enough, it works well. As it´s a long shader I included it as a unity package with shader, material and a test scene.

[20408-outline.unitypackage.zip|20408]