Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Unlit/Grid"
- {
- Properties{
- _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
- Cull Off
- Blend SrcAlpha OneMinusSrcAlpha
- CGPROGRAM
- // Define the vertex and fragment shader functions
- #pragma vertex vert
- #pragma fragment frag
- // Access Shaderlab properties
- uniform float _GridThickness;
- uniform float _GridSpacing;
- uniform float4 _GridColour;
- uniform float4 _BaseColour;
- // Input into the vertex shader
- struct vertexInput {
- float4 vertex : POSITION;
- };
- // Output from vertex shader into fragment shader
- struct vertexOutput {
- float4 pos : SV_POSITION;
- float4 worldPos : TEXCOORD0;
- };
- // VERTEX SHADER
- vertexOutput vert(vertexInput input) {
- vertexOutput output;
- output.pos = UnityObjectToClipPos(input.vertex);
- // Calculate the world position coordinates to pass to the fragment shader
- output.worldPos = mul(unity_ObjectToWorld, input.vertex);
- return output;
- }
- float calcDist(float pos, float spacing)
- {
- float left = 1.0 - frac(pos / spacing);
- float right = frac(pos / spacing);
- return right < left ? right : - left;
- }
- // FRAGMENT SHADER
- float4 frag(vertexOutput input) : COLOR{
- float distX = calcDist(input.worldPos.x, _GridSpacing);
- float distZ = calcDist(input.worldPos.z, _GridSpacing);
- float distX5 = calcDist(input.worldPos.x, _GridSpacing * 5.0);
- float distZ5 = calcDist(input.worldPos.z, _GridSpacing * 5.0);
- float dist = abs(distX) < abs(distZ) ? distX : distZ;
- float dist5 = abs(distX5) < abs(distZ5) ? distX5 : distZ5;
- //float delta = fwidth(abs(dist));//replace for non-sighed version
- float delta = fwidth(dist);
- float delta5 = fwidth(dist5);
- float alpha = smoothstep(_GridThickness - delta, _GridThickness, abs(dist));
- float alpha5 = smoothstep(_GridThickness * 0.5 - delta5, _GridThickness * 0.5, abs(dist5));
- return lerp(_GridColour, _BaseColour, min(alpha, alpha5));
- }
- ENDCG
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement