/// <summary>Renders the grid at runtime</summary> /// <param name="from">Lower limit</param> /// <param name="to">Upper limit</param> /// <param name="colors">Colors for rendering</param> /// <param name="width">Width of the line</param> /// <param name="cam">Camera for rendering</param> /// <param name="camTransform">Transform of the camera</param> /// /// Renders the grid with lower and upper limit, a given line width and individual colours for the three axes. /// If the lines have line width 1 they will be exactly one pixel wide, and if they have a larger with they will be rendered as billboards (always facing the camera). /// If there is no camera and camera Transform passed this won't be possible and the lines will default back to one pixel width. /// /// It is not necessary to call this method manually, rather you should just set the @c #renderGrid flag to @c true and let Grid Framework take care of it. /// However, if you want total control use this method, usually from within an /// <c><a href="http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnPostRender.html">OnPostRender</a></c> method. public void RenderGrid(Vector3 from, Vector3 to, ColorVector3 colors, int width = 0, Camera cam = null, Transform camTransform = null) { if (!renderGrid) { return; } if (!renderMaterial) { renderMaterial = defaultRenderMaterial; } drawPointsUpdate(from, to); RenderGridLines(colors, width, cam, camTransform); }
protected void RenderGridLines(ColorVector3 colors, int width = 0, Camera cam = null, Transform camTransform = null) { renderMaterial.SetPass(0); if (width <= 1 || !cam || !camTransform) // use simple lines for width 1 or if no camera was passed { GL.Begin(GL.LINES); for (int i = 0; i < 3; i++) { if (hideAxis[i]) { continue; } GL.Color(colors[i]); foreach (Vector3[] line in _drawPoints[i]) { if (line == null) { continue; } GL.Vertex(line[0]); GL.Vertex(line[1]); } } GL.End(); } else // quads for "lines" with width { GL.Begin(GL.QUADS); float mult = Mathf.Max(0, 0.5f * width); //the multiplier, half the desired width for (int i = 0; i < 3; i++) { GL.Color(colors[i]); if (hideAxis[i]) { continue; } //sample a direction vector, one per direction is enough (using the first line of each line set (<- !!! ONLY TRUE FOR RECT GRIDS !!!) Vector3 dir = new Vector3(); if (_drawPoints[i].Length > 0) //can't get a line if the set is empty { dir = Vector3.Cross(_drawPoints[i][0][0] - _drawPoints[i][0][1], camTransform.forward).normalized; } //multiply dir with the world length of one pixel in distance #if UNITY_2_6 || UNITY_2_6_1 if (cam.isOrthoGraphic) { #else if (cam.orthographic) { #endif dir *= (cam.orthographicSize * 2) / cam.pixelHeight; } else // (the 50 below is just there to smooth things out) { dir *= (cam.ScreenToWorldPoint(new Vector3(0, 0, 50)) - cam.ScreenToWorldPoint(new Vector3(20, 0, 50))).magnitude / 20; } foreach (Vector3[] line in _drawPoints[i]) { if (line == null) { continue; } // if the grid is not rectangular we need to change dir every time if (GetType() != typeof(GFRectGrid)) { dir = Vector3.Cross(line[0] - line[1], camTransform.forward).normalized; if (cam.isOrthoGraphic) { dir *= (cam.orthographicSize * 2) / cam.pixelHeight; } else // (the 50 below is just there to smooth things out) { dir *= (cam.ScreenToWorldPoint(new Vector3(0, 0, 50)) - cam.ScreenToWorldPoint(new Vector3(20, 0, 50))).magnitude / 20; } } GL.Vertex(line[0] - mult * dir); GL.Vertex(line[0] + mult * dir); GL.Vertex(line[1] + mult * dir); GL.Vertex(line[1] - mult * dir); } } GL.End(); } }