Skip to content

Instantly share code, notes, and snippets.

@phosphoer
Created October 20, 2020 22:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phosphoer/822912180db10e6ad21e16c51a924648 to your computer and use it in GitHub Desktop.
Save phosphoer/822912180db10e6ad21e16c51a924648 to your computer and use it in GitHub Desktop.
Outline Shader Example
Shader "Custom/CellShadedOutline"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_OutlineColor ("Outline Color", Color) = (1,1,1,1)
_OutlineThickness ("Outline Thickness", float) = 0.1
_MainTex ("Texture", 2D) = "white" {}
_LightRamp ("Light Ramp", 2D) = "white" {}
_VertexColorWeight ("Vertex Color Weight", float) = 1
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
#include "CellShading.cginc"
struct appdata
{
float4 vertex : POSITION;
fixed4 normal : NORMAL;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
fixed4 color : COLOR;
float2 uv : TEXCOORD0;
SHADOW_COORDS(1)
fixed3 worldNormal : TEXCOORD2;
UNITY_FOG_COORDS(3)
};
sampler2D _MainTex;
float4 _Color;
float4 _OutlineColor;
float _OutlineThickness;
float _VertexColorWeight;
v2f vert (appdata v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = mul(unity_ObjectToWorld, fixed4(v.normal.xyz, 0));
o.color = v.color;
TRANSFER_SHADOW(o)
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
v2f vertOutline (appdata v)
{
float3 worldNormal = mul(unity_ObjectToWorld, fixed4(v.normal.xyz, 0));
float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
worldPos += normalize(worldNormal) * _OutlineThickness;
v2f o;
v.vertex = mul(unity_WorldToObject, float4(worldPos, 1));
o.worldNormal = worldNormal;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.color = v.color;
TRANSFER_SHADOW(o)
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// Get base diffuse color
fixed3 diffuse = _Color.rgb * tex2D(_MainTex, i.uv).rgb * lerp(fixed3(1, 1, 1), i.color.rgb, _VertexColorWeight);
diffuse *= CalculateLighting(normalize(i.worldNormal), SHADOW_ATTENUATION(i)).rgb;
UNITY_APPLY_FOG(i.fogCoord, diffuse);
return fixed4(diffuse, 1);
}
fixed4 fragOutline (v2f i) : SV_Target
{
// Get base diffuse color
fixed3 diffuse = _OutlineColor.rgb;
UNITY_APPLY_FOG(i.fogCoord, diffuse);
return fixed4(diffuse, 1);
}
ENDCG
Pass
{
Tags { "RenderType"="Opaque" }
Stencil
{
Ref 1
Comp always
Pass replace
}
ZWrite On
Cull Front
CGPROGRAM
#pragma vertex vertOutline
#pragma fragment fragOutline
#pragma multi_compile_fog
ENDCG
}
Pass
{
Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" }
Stencil
{
Ref 1
Comp always
Pass replace
}
ZWrite On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
ENDCG
}
}
Fallback "Diffuse"
}
@thecrazy
Copy link

thecrazy commented Nov 1, 2020

This is interesting can I use it in my game? If yes were can I find the file CellShading.cginc?

@phosphoer
Copy link
Author

This is interesting can I use it in my game? If yes were can I find the file CellShading.cginc?

Hey thecrazy, sorry I didn't see your comment till now. I just uploaded this as an example to someone on how to implement an outline effect, so other irrelevant parts are left out. Feel free though to use anything you can!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment