Last active
October 21, 2020 12:57
-
-
Save CynicatPro/1fe29411fae202eb0566c698303a372e to your computer and use it in GitHub Desktop.
A simple outline shader for unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Unlit/OutlineShader" { | |
Properties { | |
[HDR] _OutlineColor ("Outline Color", Color) = (1,1,1,1) | |
_OutlineRadius ("Outline Radius", Float) = 0.1 | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 100 | |
Pass { | |
Stencil { | |
Ref 2 | |
Comp always | |
Pass replace | |
} | |
ColorMask 0 | |
ZTest LEqual | |
ZWrite off | |
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; | |
}; | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
v2f vert (appdata v) { | |
v2f o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target { | |
return fixed4(1,1,1,1); | |
} | |
ENDCG | |
} | |
Pass { | |
Stencil { | |
Ref 2 | |
Comp notequal | |
Pass zero | |
ZFail zero | |
} | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
}; | |
struct v2f { | |
float4 vertex : SV_POSITION; | |
}; | |
half4 _OutlineColor; | |
half _OutlineRadius; | |
v2f vert (appdata v) { | |
v2f o; | |
float3 clipNormal = normalize(mul((float3x3) UNITY_MATRIX_VP, mul((float3x3) UNITY_MATRIX_M, v.normal))); | |
float4 clipPosition = UnityObjectToClipPos(v.vertex); | |
clipPosition.xyz += clipNormal * _OutlineRadius; | |
float2 offset = normalize(clipNormal.xy) * _OutlineRadius * clipPosition.w; | |
clipPosition.xy += offset; | |
o.vertex = clipPosition; | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target { | |
return _OutlineColor; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment