Skip to content

Instantly share code, notes, and snippets.

@slipster216
Last active September 2, 2024 13:19
Show Gist options
  • Save slipster216/3eec15c240066277e69e065a90e1f9fe to your computer and use it in GitHub Desktop.
Save slipster216/3eec15c240066277e69e065a90e1f9fe to your computer and use it in GitHub Desktop.
hader "Unlit/GridOverlay"
{
Properties
{
_GridSize("Grid Size", Float) = 10
_Grid2Size("Grid 2 Size", Float) = 160
_Grid3Size("Grid 3 Size", Float) = 320
_Alpha ("Alpha", Range(0,1)) = 1
}
SubShader
{
Tags { "RenderType"="Overlay" }
LOD 100
ZTest Always
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
Offset -20, -20
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _GridSize;
float _Grid2Size;
float _Grid3Size;
float _Alpha;
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = mul(_Object2World, v.vertex).xz;
return o;
}
float DrawGrid(float2 uv, float sz, float aa)
{
float aaThresh = aa;
float aaMin = aa*0.1;
float2 gUV = uv / sz + aaThresh;
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, 0.03);
fixed b = DrawGrid(i.uv, _Grid2Size, 0.005);
fixed g = DrawGrid(i.uv, _Grid3Size, 0.002);
return float4(0.8*r*_Alpha,0.8*g*_Alpha,0.8*b*_Alpha,(r+b+g)*_Alpha);
}
ENDCG
}
}
}
@cihadturhan
Copy link

Thanks!
I modified it a bit to draw vertical lines on 2D Mode (XY Plane) and added color

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Unlit/ColumnOverlay"
{
    Properties
    {
        _ColumnSize("Column Size", Float) = 3
        _GridColor("Grid Color", Color) = (1,0,0.4,1)
        _Alpha ("Alpha", Range(0,1)) = 1
    }
    SubShader
    {
        Tags
        {
            "RenderType"="Overlay"
        }
        LOD 100
        ZTest Always

        Pass
        {
            Blend SrcAlpha OneMinusSrcAlpha
            Offset -10, -10
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            float _ColumnSize;
            float4 _GridColor;
            float _Alpha;

            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).xy;
                return o;
            }


            float DrawColumn(float2 uv, float sz, float aa)
            {
                float aaThresh = aa;
                float aaMin = aa * 0.001;

                float2 gUV = uv / sz + aaThresh;

                float2 fl = floor(gUV);
                gUV = frac(gUV);
                gUV -= aaThresh;
                gUV = smoothstep(aaThresh, aaMin, abs(gUV));
                float d = gUV.x;

                return d;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                fixed r = DrawColumn(i.uv, _ColumnSize, 0.03);
                return float4(_GridColor.xyz * r, r * _Alpha);
            }
            ENDCG
        }
    }
}

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