Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Forked from slipster216/Grid shader
Created September 2, 2024 13:19
Show Gist options
  • Select an option

  • Save unitycoder/cc16a45b59285fc205ca6c032bff9cd7 to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/cc16a45b59285fc205ca6c032bff9cd7 to your computer and use it in GitHub Desktop.

Revisions

  1. @slipster216 slipster216 revised this gist Jun 25, 2016. 1 changed file with 26 additions and 27 deletions.
    53 changes: 26 additions & 27 deletions Grid shader
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,10 @@ 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
    _GridSize("Grid Size", Float) = 10
    _Grid2Size("Grid 2 Size", Float) = 160
    _Grid3Size("Grid 3 Size", Float) = 320
    _Alpha ("Alpha", Range(0,1)) = 1
    }
    SubShader
    {
    @@ -15,8 +15,8 @@ hader "Unlit/GridOverlay"

    Pass
    {
    Blend SrcAlpha OneMinusSrcAlpha
    Offset -20, -20
    Blend SrcAlpha OneMinusSrcAlpha
    Offset -20, -20
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    @@ -25,30 +25,29 @@ hader "Unlit/GridOverlay"

    float _GridSize;
    float _Grid2Size;
    float _Grid3Size;
    float _Grid3Size;
    float _Alpha;
    sampler2D _Font;

    struct appdata
    {
    float4 vertex : POSITION;
    };
    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;
    }

    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)
  2. @slipster216 slipster216 created this gist Jun 25, 2016.
    81 changes: 81 additions & 0 deletions Grid shader
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,81 @@
    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;
    sampler2D _Font;

    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
    }
    }
    }