Skip to content

Instantly share code, notes, and snippets.

@belzecue
Forked from slipster216/Grid shader
Last active December 16, 2021 22:03
Show Gist options
  • Save belzecue/2205ed10d0dc2dd2c2d85b62aa67873f to your computer and use it in GitHub Desktop.
Save belzecue/2205ed10d0dc2dd2c2d85b62aa67873f to your computer and use it in GitHub Desktop.
// Originally written by Jason Booth https://gist.github.com/slipster216/3eec15c240066277e69e065a90e1f9fe
// Updated by Andrew Ferguson, April 2019.
// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/GridShader" {
Properties
{
_GridColor("Grid Color", Color) = (1,1,1,1)
_MeshColor("Mesh Color", Color) = (0,0,0,0)
_GridSize("Grid Size", Float) = 1
_LineThickness("Line Thickness", Float) = 0.03
[IntRange] _Xonly("Show X only", Range(0, 1)) = 0
[IntRange] _Yonly("Show Y only", Range(0, 1)) = 0
_OffsetX("X Offset", Range(0, 1)) = 0
_OffsetY("Y Offset", Range(0, 1)) = 0
}
SubShader
{
Tags { "Queue"="Transparent"}
LOD 100
//ZWrite Off
Pass
{
//Blend SrcAlpha OneMinusSrcAlpha
Blend One OneMinusSrcAlpha
Offset -20, -20
CGPROGRAM
#define IF(a, b, c) lerp(b, c, step((fixed) (a), 0));
#pragma vertex vert
#pragma fragment frag
# include "UnityCG.cginc"
fixed4 _GridColor, _MeshColor;
float _GridSize, _LineThickness, _Xonly, _Yonly, _OffsetX, _OffsetY;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = mul(unity_ObjectToWorld, v.vertex).xz;
return o;
}
float DrawGrid(float2 uv, float sz, float aa)
{
float aaThresh = aa;
float aaMin = aa * 0.1;
float2 gUV = float2(uv.x + _OffsetX, uv.y + _OffsetY) / sz + aaThresh;
// Check if want X only.
gUV = IF(
_Xonly > 0
, uv.x + _OffsetX / sz + aaThresh
, gUV
);
// Check if want Y only.
gUV = IF(
_Yonly > 0
, uv.y + _OffsetY / sz + aaThresh
, gUV
);
float2 fl = floor(gUV);
gUV = frac(gUV);
gUV -= aaThresh;
gUV = smoothstep(aaThresh, aaMin, abs(gUV));
float d = max(gUV.x, gUV.y);
return d;
}
fixed4 frag(v2f i) : SV_Target
{
fixed r = DrawGrid(i.uv, _GridSize, _LineThickness);
return IF(
r > 0
, _GridColor
, _MeshColor
);
}
ENDCG
}
}
}
@kpurens
Copy link

kpurens commented Dec 16, 2021

Super nice shader, thanks!

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