Skip to content

Instantly share code, notes, and snippets.

@bgolus
Last active October 25, 2023 01:44
Show Gist options
  • Save bgolus/c67c29c218ec1768a770ca6966fa0765 to your computer and use it in GitHub Desktop.
Save bgolus/c67c29c218ec1768a770ca6966fa0765 to your computer and use it in GitHub Desktop.
A mobile VR and MSAA friendly single object highlight shader using the stencil buffer
Shader "Mobile VR Highlight" {
Properties {
_ColorOutline ("Outline", Color) = (1,1,1,1)
_ColorInterior ("Interior", Color) = (0.25,0.25,0.25,0.25)
_ColorInteriorFaded ("Interior Faded", Color) = (0.1,0.1,0.1,0.1)
_ColorInteriorOcc ("Interior Occluded", Color) = (0.15,0.15,0.15,0.15)
_ColorInteriorOccFaded ("Interior Occluded Faded", Color) = (0.05,0.05,0.05,0.05)
_PulseRateMod ("Pulse Rate Modifier", Float) = 4.0
_OutlneWidth ("Outline Pixel Width", Float) = 1.0
}
Subshader {
Tags{ "Queue" = "Overlay" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
ZWrite off
ZTest Always
Blend SrcAlpha OneMinusSrcAlpha
Offset -0.5, 0
Stencil {
ReadMask 2
WriteMask 2
Ref 2
Comp NotEqual
Pass Replace
}
CGINCLUDE
#include "UnityCG.cginc"
struct v2f
{
float4 pos : SV_POSITION;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vertDefault (appdata_base v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
float _OutlneWidth;
v2f vertOutline (appdata_base v, float2 offsetDir)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = UnityObjectToClipPos(v.vertex);
o.pos.xy += (offsetDir * 2.0) / _ScreenParams.xy * o.pos.w * _OutlneWidth;
return o;
}
float _PulseRateMod;
fixed4 pulse(fixed4 a, fixed4 b) { return lerp(a, b, saturate(sin(_Time.y * _PulseRateMod) * 0.5 + 0.5)); }
fixed4 _ColorOutline;
fixed4 fragOutline (v2f i) : SV_Target
{
return _ColorOutline;
}
ENDCG
Pass {
Name "Unoccluded Fill"
ZTest LEqual
CGPROGRAM
#pragma vertex vertDefault
#pragma fragment frag
#pragma multi_compile_instancing
fixed4 _ColorInterior, _ColorInteriorFaded;
fixed4 frag (v2f i) : SV_Target
{
return pulse(_ColorInterior, _ColorInteriorFaded);
}
ENDCG
}
Pass {
Name "Occluded Fill"
CGPROGRAM
#pragma vertex vertDefault
#pragma fragment frag
#pragma multi_compile_instancing
fixed4 _ColorInteriorOcc, _ColorInteriorOccFaded;
fixed4 frag (v2f i) : SV_Target
{
return pulse(_ColorInteriorOcc, _ColorInteriorOccFaded);
}
ENDCG
}
Pass {
Name "Outline UR"
CGPROGRAM
#pragma vertex vert
#pragma fragment fragOutline
#pragma multi_compile_instancing
v2f vert(appdata_base v)
{
return vertOutline(v, float2( 1, 1));
}
ENDCG
}
Pass {
Name "Outline UL"
CGPROGRAM
#pragma vertex vert
#pragma fragment fragOutline
#pragma multi_compile_instancing
v2f vert(appdata_base v)
{
return vertOutline(v, float2(-1, 1));
}
ENDCG
}
Pass {
Name "Outline DR"
CGPROGRAM
#pragma vertex vert
#pragma fragment fragOutline
#pragma multi_compile_instancing
v2f vert(appdata_base v)
{
return vertOutline(v, float2( 1,-1));
}
ENDCG
}
Pass {
Name "Outline DL"
CGPROGRAM
#pragma vertex vert
#pragma fragment fragOutline
#pragma multi_compile_instancing
v2f vert(appdata_base v)
{
return vertOutline(v, float2(-1,-1));
}
ENDCG
}
}
}
@thnewlands
Copy link

Hi there! I'd love to use this for a project. Do you have a particular license for it?

@bgolus
Copy link
Author

bgolus commented Feb 22, 2023

I generally go with Unlicense, or WTF License, or MIT if you need something more formal.

@thnewlands
Copy link

Thank you!

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