Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created January 8, 2015 03:28
Show Gist options
  • Save mattatz/44a8ebdc557896f0a5dc to your computer and use it in GitHub Desktop.
Save mattatz/44a8ebdc557896f0a5dc to your computer and use it in GitHub Desktop.
Example of stencil shader to draw a simple outline for Unity.
Shader "Mattatz/StencilSample" {
Properties {
_Outline ("Outline Length", Range(0.0, 1.0)) = 0.2
_Color ("Color", Color) = (0.8, 0.8, 0.8, 1.0)
_OutlineColor ("Outline Color", Color) = (0.2, 0.2, 0.2, 1.0)
}
SubShader {
Tags {
"RenderType"="Opaque"
"Queue"="Transparent"
}
LOD 200
// render model
Pass {
Stencil {
Ref 128
Comp always
Pass replace
}
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float4 _Color;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
float4 vert = v.vertex;
o.pos = mul(UNITY_MATRIX_MVP, vert);
return o;
}
half4 frag(v2f i) : COLOR {
return _Color;
}
ENDCG
}
// render outline
Pass {
Stencil {
Ref 128
Comp NotEqual
}
Cull Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
float _Outline;
float4 _OutlineColor;
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f {
float4 pos : SV_POSITION;
};
v2f vert(appdata v) {
v2f o;
float4 vert = v.vertex;
vert.xyz += v.normal * _Outline;
o.pos = mul(UNITY_MATRIX_MVP, vert);
return o;
}
half4 frag(v2f i) : COLOR {
return _OutlineColor;
}
ENDCG
}
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment