Skip to content

Instantly share code, notes, and snippets.

@mrjoelkemp
Last active August 18, 2023 04:24
Show Gist options
  • Save mrjoelkemp/a8714aed3381a288770ce9978a3b8871 to your computer and use it in GitHub Desktop.
Save mrjoelkemp/a8714aed3381a288770ce9978a3b8871 to your computer and use it in GitHub Desktop.
Unity 3D Outline Shader - adds a configurable outline around the associated object
// Graciously adapted from https://www.youtube.com/watch?v=SlTkBe4YNbo&lc=UgwdZi_heg9VslLssQp4AaABAg
Shader "Unlit/Outline"
{
Properties{
_Color("Main Color", Color) = (0,0,0,1)
_OutlineColor("Outline color", color) = (0,0,0,1)
_OutlineWidth("Outline width", Range(1.0,5.0)) = 1.01
}
SubShader{
Tags {
"Queue" = "Transparent"
"RenderType" = "Transparent"
"DisableBatching" = "True"
}
// Render the outline by expanding the vertices
// and filling the object with the outline color
Pass {
ZWrite off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
struct appdata{
float4 vertex : POSITION;
};
struct v2f{
float4 pos : POSITION;
};
float _OutlineWidth;
float4 _OutlineColor;
v2f vert(appdata v) {
v.vertex.xyz *= _OutlineWidth;
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : COLOR
{
return _OutlineColor;
}
ENDCG
}
// Render the normal vertices with the main color
// over the outline width fill pass.
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
float4 _Color;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
fixed4 frag (v2f i) : COLOR
{
return _Color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment