The following code won’t draw my material:
public class GridGraphics
{
private static readonly Material gridMaterial = new(Shader.Find("Cubusky/Grid"));
public static void DrawGrid(Matrix4x4 matrix)
{
GL.PushMatrix();
gridMaterial.SetPass(0);
GL.Begin(GL.QUADS);
GL.Vertex(matrix.MultiplyPoint3x4(new Vector3(-5f, 0f, -5f)));
GL.Vertex(matrix.MultiplyPoint3x4(new Vector3(-5f, 0f, 5f)));
GL.Vertex(matrix.MultiplyPoint3x4(new Vector3(5f, 0f, 5f)));
GL.Vertex(matrix.MultiplyPoint3x4(new Vector3(5f, 0f, -5f)));
GL.End();
GL.PopMatrix();
}
}
If I change the grid material to use new(Shader.Find("Unlit/Color")); instead, it will draw a white quad as expected. What am I missing? I’m assuming that one of my shader settings or -functions is the culprit, in need of some data the GL is not yet providing? Shader below.
Here is the material working on a Mesh Renderer as expected btw. Don’t worry I’m not stupid ![]()
Shader:
Shader "Cubusky/Grid"
{
Properties
{
_Color("Main Color", Color) = (1, 1, 1, 1)
_Div("Divisions", Float) = 10
_LineWidth("Line Width", Range(0, 1)) = 0.1
_Edge("Edge", Range(0, 1)) = 0.9
}
SubShader
{
Lighting Off
Cull Off
ZWrite Off
ZTest always
Fog { Mode Off }
LOD 100
Tags {"Queue"="Transparent+1" }
Pass
{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
int _Div;
v2f vert (appdata input)
{
v2f OUT;
OUT.pos = UnityObjectToClipPos(input.vertex);
OUT.uv = input.uv;
return OUT;
}
half _LineWidth;
// Draw the grid lines
half grid(float2 uv)
{
const half EPSILON = 2.4414e-4;
half result;
half lineWidth = 1.0 / _Div * _LineWidth * 0.5;
for (half i = 0.0; i <= 1 + EPSILON; i += 1.0 / _Div)
{
result += 1.0 - smoothstep(0.0, lineWidth, abs(uv.x - i));
result += 1.0 - smoothstep(0.0, lineWidth, abs(uv.y - i));
}
return result;
}
half _Edge;
// Fade the grid edges
half edge(float2 uv)
{
// Early out to avoid division by zero.
if (_Edge == 1.0)
{
return 1.0;
}
half2 edges = saturate(smoothstep(1.0, _Edge, abs(uv * 2.0 - 1.0)));
return min(edges.x, edges.y);
}
half4 _Color;
// Color 'n stuff
fixed4 frag (v2f input) : SV_Target
{
half4 col = _Color;
col.a *= grid(input.uv) * edge(input.uv);
#if defined(UNITY_COLORSPACE_GAMMA)
col = half4(LinearToGammaSpace(col.rgb), col.a);
#endif
return col;
}
ENDCG
}
}
}
