Skip to content

Instantly share code, notes, and snippets.

@jmcguirk
Created July 15, 2016 17:11
Show Gist options
  • Save jmcguirk/854cdf066e640e51bd99398d94cacbf4 to your computer and use it in GitHub Desktop.
Save jmcguirk/854cdf066e640e51bd99398d94cacbf4 to your computer and use it in GitHub Desktop.
Shader "GridShader" {
Properties {
_GridThickness ("Grid Thickness", Float) = 0.01
_GridSpacing ("Grid Spacing", Float) = 10.0
_GridColour ("Grid Colour", Color) = (1.0, 1.0, 1.0, 1.0)
_BaseColour ("Base Colour", Color) = (1.0, 0.0, 0.0, 0.0)
}
SubShader {
Tags { "Queue" = "Transparent" }
Pass {
ZWrite 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 = mul(UNITY_MATRIX_MVP, input.vertex);
// Calculate the world position coordinates to pass to the fragment shader
output.worldPos = mul(_Object2World, input.vertex);
return output;
}
// FRAGMENT SHADER
float4 frag(vertexOutput input) : COLOR {
float xfrac = frac(input.worldPos.x / _GridThickness) - 0.5;
float yfrac = frac(input.worldPos.z / _GridThickness) - 0.5;
float dist = xfrac * xfrac + yfrac * yfrac;
float strength = max(0.5 - dist, 0) * 2;
strength = 1;//pow(strength, 5);
bool xvalid = false;
bool yvalid = false;
if (input.worldPos.x > 0)
{
if (fmod(input.worldPos.x, _GridSpacing) < _GridThickness)
xvalid = true;
}
else
{
if (fmod(input.worldPos.x, _GridSpacing) < _GridThickness - _GridSpacing)
xvalid = true;
}
if (input.worldPos.z > 0)
{
if (fmod(input.worldPos.z, _GridSpacing) < _GridThickness)
yvalid = true;
}
else
{
if (fmod(input.worldPos.z, _GridSpacing) < _GridThickness - _GridSpacing)
yvalid = true;
}
if (xvalid && yvalid)
return _GridColour * strength + _BaseColour * (1.0 - strength);
else
return _BaseColour;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment