Last active
September 16, 2022 03:22
-
-
Save arimger/bf52ca8040929eeaad878f45eb4a9559 to your computer and use it in GitHub Desktop.
Basic grid shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Grid" | |
{ | |
Properties | |
{ | |
[PerRendererData] | |
_MainTex("Albedo (RGB)", 2D) = "white" {} | |
_LineColor("Line Color", Color) = (1,1,1,1) | |
_CellColor("Cell Color", Color) = (0,0,0,0) | |
[IntRange][Space] | |
_GridSizeX("Grid Size X", Range(1, 10000)) = 10 | |
_GridSizeX("Grid Size Y", Range(1, 10000)) = 10 | |
_LineSize("Line Size", Range(0,1)) = 0.15 | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" } | |
LOD 100 | |
Pass | |
{ | |
Tags { "LightMode" = "UniversalForward" } | |
Blend SrcAlpha OneMinusSrcAlpha | |
ZWrite Off | |
HLSLPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
struct Attributes | |
{ | |
float4 positionOS : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct Varyings | |
{ | |
float4 positionCS : SV_POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
CBUFFER_START(UnityPerMaterial) | |
half4 _LineColor; | |
half4 _CellColor; | |
float _GridSizeX; | |
float _GridSizeY; | |
float _LineSize; | |
CBUFFER_END | |
float getGridRatio(float2 p, float thickness) | |
{ | |
float2 f = frac(p); | |
float g = min(min(f.x, 1.0 - f.x), min(f.y, 1.0 - f.y)) * 2.0 - thickness | |
, x = step(g, 0.0) | |
, w = fwidth(p.x) + fwidth(p.y) | |
, r = 2.0 * _ScreenParams.y | |
, c = r * abs(g) / (1. + 1. * r * w) | |
, s = sqrt(thickness) | |
, t = lerp(.5, s, min(w, 1)); | |
return lerp(t, x, clamp(c, 0, 1)); | |
} | |
Varyings vert(Attributes IN) | |
{ | |
Varyings OUT; | |
VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz); | |
OUT.positionCS = positionInputs.positionCS; | |
OUT.uv = IN.uv; | |
return OUT; | |
} | |
half4 frag(Varyings IN) : SV_Target | |
{ | |
float2 uv = IN.uv; | |
uv.x *= _GridSizeX; | |
uv.y *= _GridSizeY; | |
float gridRatio = getGridRatio(uv, _LineSize); | |
return lerp(_CellColor, _LineColor, gridRatio); | |
} | |
ENDHLSL | |
} | |
} | |
} |
Author
arimger
commented
Aug 18, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment