Skip to content

Instantly share code, notes, and snippets.

@frarees
Created January 4, 2014 17:53
Show Gist options
  • Save frarees/8258128 to your computer and use it in GitHub Desktop.
Save frarees/8258128 to your computer and use it in GitHub Desktop.
Unity3D compatible CG shader to use with an orthographic projector to create a square grid.
Shader "Projector/Grid" {
Properties {
_Color ("Color", Color) = (1,1,1,0)
_ShadowTex ("Cookie", 2D) = "black"
_Size ("Grid Size", Float) = 1
}
Subshader {
Tags { "RenderType"="Transparent" "Queue"="Transparent+100" }
Pass {
ZWrite Off
Offset -1, -1
Fog { Mode Off }
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_fog_exp2
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
uniform sampler2D _ShadowTex;
float4 _ShadowTex_ST;
float4 _Color;
float4x4 _Projector;
fixed _Size;
v2f vert (appdata_tan v) {
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX (mul (_Projector, v.vertex).xy, _ShadowTex);
return o;
}
fixed4 frag (v2f i) : COLOR {
return tex2D (_ShadowTex, fmod (float2(saturate(i.uv.x),saturate(i.uv.y)), 1 / _Size) * _Size) * _Color;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment