I’ve search the forums and in here also but nothing has help me so far.
I’m drawing a simple grid with GL on the Editor SceneView with a size of 100x100 and it’s really laggy I’m guessing there’s something wrong with my code but I can’t find what, I would appreciate any help.
Here’s the code
Material lineMaterial;
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);
}
}
protected void DrawGrid()
{
// Tile size
float size = positionSnap;
// Grid Size
int gridSizeX = 100;
int gridSizeZ = 100;
// Half Size
float halfSizeX = (gridSizeX * 0.5f) * size;
float halfSizeZ = (gridSizeZ * 0.5f) * size;
float offset = positionSnap * 0.5f;
halfSizeX -= offset;
halfSizeZ -= offset;
GL.PushMatrix();
lineMaterial.SetPass(0);
GL.Begin(GL.LINES);
GL.Color(Color.grey);
for(int x = 0; x < gridSizeX + 1; x++)
{
for(int z = 0; z < gridSizeZ + 1; z++)
{
// X
Vector3 start = new Vector3(x * size, height, z * size);
Vector3 end = new Vector3(gridSizeX, height, z * size);
start += Vector3.left * halfSizeX;
end += Vector3.left * halfSizeX;
start += Vector3.back * halfSizeZ;
end += Vector3.back * halfSizeZ;
GL.Vertex3(start.x, start.y, start.z);
GL.Vertex3(end.x, end.y, end.z);
// Z
start = new Vector3(x * size, height, z * size);
end = new Vector3(x * size, height, gridSizeZ);
start += Vector3.left * halfSizeX;
end += Vector3.left * halfSizeX;
start += Vector3.back * halfSizeZ;
end += Vector3.back * halfSizeZ;
GL.Vertex3(start.x, start.y, start.z);
GL.Vertex3(end.x, end.y, end.z);
}
}
GL.End();
GL.PopMatrix();
}