Skip to content

Instantly share code, notes, and snippets.

@bzgeb
Last active March 16, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bzgeb/484212fef4c04d273d9b848afc57e37a to your computer and use it in GitHub Desktop.
Save bzgeb/484212fef4c04d273d9b848afc57e37a to your computer and use it in GitHub Desktop.
Unity WorldSpace Grid Shader
Shader "MaskableGrid"
{
Properties
{
_Mask ("Mask", 2D) = "white" {}
_GridThickness ("Grid Thickness", Float) = 0.01
_GridSpacing ("Grid Spacing", Float) = 10.0
_GridColour ("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0)
_BaseColour ("Base Colour", Color) = (0.0, 0.0, 0.0, 0.0)
}
SubShader
{
Tags { "Queue" = "Transparent" }
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _Mask;
uniform float4 _Mask_ST;
uniform half _GridThickness;
uniform half _GridSpacing;
uniform half4 _GridColour;
uniform half4 _BaseColour;
struct vertexInput {
half4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};
struct vertexOutput {
half4 pos : SV_POSITION;
half2 texcoord : TEXCOORD0;
half4 worldPos : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = UnityObjectToClipPos(input.vertex);
output.texcoord = input.texcoord * _Mask_ST.xy + _Mask_ST.zw;
output.worldPos = mul(unity_ObjectToWorld, input.vertex);
return output;
}
half4 frag(vertexOutput input) : COLOR
{
half4 color = (0, 0, 0, tex2D(_Mask, input.texcoord).a);
half offset = _GridThickness / 2;
half2 offsetWorldPos = half2(input.worldPos.x + offset, input.worldPos.y + offset);
if (frac(offsetWorldPos.x / _GridSpacing) < _GridThickness || frac(offsetWorldPos.y / _GridSpacing) < _GridThickness)
{
color.rgb = _GridColour.rgb;
}
else
{
color.rgb = _BaseColour.rgb;
color.a = color.a * _BaseColour.a;
}
return color;
}
ENDCG
}
}
}
Shader "Grid_StencilMasked_Scrolling_LocalSpace"
{
Properties
{
_Mask ("Mask", 2D) = "white" {}
_GridThickness ("Grid Thickness", Float) = 0.01
_GridSpacing ("Grid Spacing", Float) = 10.0
_GridColour ("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0)
_BaseColour ("Base Colour", Color) = (0.0, 0.0, 0.0, 0.0)
_ScrollSpeedX ("Scroll Speed", Float) = 0
_ScrollSpeedY ("Scroll Speed", Float) = 0
}
SubShader
{
Tags { "Queue" = "Transparent" }
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Stencil { Ref 2 ReadMask 2 Comp Equal }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _Mask;
uniform half4 _Mask_ST;
uniform half _GridThickness;
uniform half _GridSpacing;
uniform half4 _GridColour;
uniform half4 _BaseColour;
uniform half _ScrollSpeedX;
uniform half _ScrollSpeedY;
struct vertexInput {
half4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};
struct vertexOutput {
half4 pos : SV_POSITION;
half2 texcoord : TEXCOORD0;
half4 localPos : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = UnityObjectToClipPos(input.vertex);
output.texcoord = input.texcoord * _Mask_ST.xy + _Mask_ST.zw;
output.localPos.xy = input.vertex.xz;
return output;
}
half4 frag(vertexOutput input) : COLOR
{
half4 color = (0, 0, 0, 1 - tex2D(_Mask, input.texcoord).a);
half offset = _GridThickness / 2;
half2 offsetLocalPos = half2(input.localPos.x + offset + _Time.y * _ScrollSpeedX, input.localPos.y + offset + _Time.y * _ScrollSpeedY);
if (frac(offsetLocalPos.x / _GridSpacing) < _GridThickness || frac(offsetLocalPos.y / _GridSpacing) < _GridThickness)
{
color.rgb = _GridColour.rgb;
}
else
{
color.rgb = _BaseColour.rgb;
color.a = color.a * _BaseColour.a;
}
return color;
}
ENDCG
}
}
}
Shader "GridUnmask"
{
Properties
{
}
SubShader
{
Tags { "Queue" = "Transparent-1" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Pass
{
Stencil { Ref 2 ReadMask 2 Comp Always Pass Replace}
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform half4 _Color;
uniform sampler2D _MainTex;
uniform float4 _MainTex_ST;
struct vertexInput
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
vertexOutput vert(vertexInput v)
{
vertexOutput o;
o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
half4 frag(vertexOutput i) : COLOR
{
return 0;
}
ENDCG
}
}
FallBack "Diffuse"
}
Shader "MaskableScrollingLocalSpaceGrid"
{
Properties
{
_Mask ("Mask", 2D) = "white" {}
_GridThickness ("Grid Thickness", Float) = 0.01
_GridSpacing ("Grid Spacing", Float) = 10.0
_GridColour ("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0)
_BaseColour ("Base Colour", Color) = (0.0, 0.0, 0.0, 0.0)
_ScrollSpeedX ("Scroll Speed", Float) = 0
_ScrollSpeedY ("Scroll Speed", Float) = 0
}
SubShader
{
Tags { "Queue" = "Transparent" }
Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _Mask;
uniform half4 _Mask_ST;
uniform half _GridThickness;
uniform half _GridSpacing;
uniform half4 _GridColour;
uniform half4 _BaseColour;
uniform half _ScrollSpeedX;
uniform half _ScrollSpeedY;
struct vertexInput {
half4 vertex : POSITION;
half2 texcoord : TEXCOORD0;
};
struct vertexOutput {
half4 pos : SV_POSITION;
half2 texcoord : TEXCOORD0;
half4 localPos : TEXCOORD1;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = UnityObjectToClipPos(input.vertex);
output.texcoord = input.texcoord * _Mask_ST.xy + _Mask_ST.zw;
output.localPos.xy = input.vertex.xz;
return output;
}
half4 frag(vertexOutput input) : COLOR
{
half4 color = (0, 0, 0, 1 - tex2D(_Mask, input.texcoord).a);
half offset = _GridThickness / 2;
half2 offsetLocalPos = half2(input.localPos.x + offset + _Time.y * _ScrollSpeedX, input.localPos.y + offset + _Time.y * _ScrollSpeedY);
if (frac(offsetLocalPos.x / _GridSpacing) < _GridThickness || frac(offsetLocalPos.y / _GridSpacing) < _GridThickness)
{
color.rgb = _GridColour.rgb;
}
else
{
color.rgb = _BaseColour.rgb;
color.a = color.a * _BaseColour.a;
}
return color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment