#Material
Name MetricCellsGrid
Base Std3D
Reference https://www.shadertoy.com/view/wdSXzm

#VertexInput
vec2 TexCoord

#MaterialInstance
Length 120
Stage  Fragment

Code
{
    vec4 x_color;
    vec4 y_color;
    vec4 x_axis_color;
    vec4 y_axis_color;
    vec4 center_color;

    vec2 lum;               //x=0.1 sub cell line,y=0.2 big cell line
    vec2 cell_step;
    vec2 big_cell_step;
    vec2 scale;

    float axis_line_width;
    float center_radius;
}

#Vertex
Output
{
    vec2 TexCoord
}

Code
{
    void main()
    {
        HandoverMI();

        Output.TexCoord=TexCoord;

        gl_Position=GetPosition3D();
    }
}

#Fragment
Output
{
    vec4 FragColor;
}

Code
{
    void main()
    {
        MaterialInstance mi=GetMI();

        float edge=viewport.inv_viewport_resolution.y;

        float x=(Input.TexCoord.x-0.5)*mi.scale.x;
        float y=(Input.TexCoord.y-0.5)*mi.scale.y;

        vec4 color=vec4(0,0,0,1);

        // ======= Lines + Bold lines
        color.xyz += step(1.0 - 1.0 / mi.cell_step.x,       fract(x / mi.cell_step.x    )) * mi.x_color.rgb * mi.lum.x;
        color.xyz += step(1.0 - 1.0 / mi.big_cell_step.x,   fract(x / mi.big_cell_step.x)) * mi.x_color.rgb * mi.lum.y;

        color.xyz += step(1.0 - 1.0 / mi.cell_step.y,       fract(y / mi.cell_step.y    )) * mi.y_color.rgb * mi.lum.x;
        color.xyz += step(1.0 - 1.0 / mi.big_cell_step.y,   fract(y / mi.big_cell_step.y)) * mi.y_color.rgb * mi.lum.y;

        // ======= AXES
        float xb = step(abs(x) - mi.axis_line_width, 0.0);
        float yb = step(abs(y) - mi.axis_line_width, 0.0);

        color.rgb = mix(color.rgb, mi.x_axis_color.rgb, (xb));
        color.rgb = mix(color.rgb, mi.y_axis_color.rgb, (yb));

        // ======= CENTER
        float cb = length(vec2(x,y))-mi.center_radius;
        color.rgb = mix(color.rgb, mi.center_color.rgb, cb>0.0?0.0:smoothstep(0,edge*mi.scale.y,abs(cb)));

        FragColor=color;
    }
}
