Skip to content

Instantly share code, notes, and snippets.

@Andrioden
Last active August 1, 2016 06:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Andrioden/1e41e3fefc20270d413cec2818c02c11 to your computer and use it in GitHub Desktop.
Save Andrioden/1e41e3fefc20270d413cec2818c02c11 to your computer and use it in GitHub Desktop.
// Source: http://answers.unity3d.com/questions/482128/draw-grid-lines-in-game-view.html
using UnityEngine;
using System.Collections;
public class GridOverlay : MonoBehaviour
{
public bool show = true;
public int gridSizeX;
public int gridSizeY;
public int gridSizeZ;
public float step;
public float startX;
public float startY;
public float startZ;
private Material lineMaterial;
public Color mainColor = new Color(0f, 1f, 0f, 1f);
void CreateLineMaterial()
{
if (!lineMaterial)
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
var shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", 0);
}
}
void OnPostRender()
{
CreateLineMaterial();
// set the current material
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
if (step == 0)
Debug.Log("Step is 0, wont draw Grid. Step it to higher than 0.");
if (show)
{
GL.Color(mainColor);
//Layers
for (float j = 0; j <= gridSizeY; j += step)
{
//X axis lines
for (float i = 0; i <= gridSizeZ; i += step)
{
GL.Vertex3(startX, startY + j, startZ + i);
GL.Vertex3(startX + gridSizeX, startY + j, startZ + i);
}
//Z axis lines
for (float i = 0; i <= gridSizeX; i += step)
{
GL.Vertex3(startX + i, startY + j, startZ);
GL.Vertex3(startX + i, startY + j, startZ + gridSizeZ);
}
}
//Y axis lines
for (float i = 0; i <= gridSizeZ; i += step)
{
for (float k = 0; k <= gridSizeX; k += step)
{
GL.Vertex3(startX + k, startY, startZ + i);
GL.Vertex3(startX + k, startY + gridSizeY, startZ + i);
}
}
}
GL.End();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment