1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| using UnityEngine;
public static class DrawGL { public enum DrawMode : int { Line = GL.LINES, LineStrip = GL.LINE_STRIP, Triangle = GL.TRIANGLES, TriangleStrip = GL.TRIANGLE_STRIP, Quad = GL.QUADS }
static Material drawMaterial;
static void InitMaterial() { if (!drawMaterial) { Shader shader = Shader.Find("Hidden/Internal-Colored"); drawMaterial = new Material(shader); drawMaterial.hideFlags = HideFlags.HideAndDontSave; } }
public static void WireframeMode(float zBias = 0, bool bAlpha = false, bool bZTest = true, int cullMode = 0) { InitMaterial(); if (bAlpha) { drawMaterial.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.SrcAlpha); drawMaterial.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); } else { drawMaterial.SetInt("_SrcBlend", (int) UnityEngine.Rendering.BlendMode.One); drawMaterial.SetInt("_DstBlend", (int) UnityEngine.Rendering.BlendMode.Zero); }
drawMaterial.SetInt("_Cull", cullMode); drawMaterial.SetInt("_ZWrite", 0);
var zTest = bZTest ? UnityEngine.Rendering.CompareFunction.LessEqual : UnityEngine.Rendering.CompareFunction.Always; drawMaterial.SetInt("_ZTest", (int) zTest); drawMaterial.SetFloat("_ZBias", zBias);
drawMaterial.SetPass(0); }
public static void DrawMesh(Mesh mesh, Matrix4x4 matrix, Color color, float zBias = 0) { Vector3[] vertices = mesh.vertices; int[] triangles = mesh.triangles;
WireframeMode(zBias); GL.PushMatrix(); GL.MultMatrix(matrix);
GL.Begin(GL.LINES); GL.Color(color); for (int cnt = 0; cnt < triangles.Length; cnt += 3) { GL.Vertex(vertices[triangles[cnt]]); GL.Vertex(vertices[triangles[cnt + 1]]); GL.Vertex(vertices[triangles[cnt + 1]]); GL.Vertex(vertices[triangles[cnt + 2]]); GL.Vertex(vertices[triangles[cnt + 2]]); GL.Vertex(vertices[triangles[cnt]]); }
GL.End(); GL.PopMatrix(); } }
|