A game about forced loneliness, made by TACStudios
1using UnityEngine; 2using System.Collections.Generic; 3using System.Reflection; 4using System; 5 6namespace UnityEngine.Rendering 7{ 8 /// <summary> 9 /// Helper to build and render a mesh for Gizmos, it is a lot more faster than drawing a ton of gizmos separately 10 /// </summary> 11 class MeshGizmo : IDisposable 12 { 13 public static readonly int vertexCountPerCube = 24; 14 15 public Mesh mesh; 16 17 List<Vector3> vertices; 18 List<int> indices; 19 List<Color> colors; 20 21 Material wireMaterial; 22 Material dottedWireMaterial; 23 Material solidMaterial; 24 25 public MeshGizmo(int capacity = 0) 26 { 27 vertices = new List<Vector3>(capacity); 28 indices = new List<int>(capacity); 29 colors = new List<Color>(capacity); 30 mesh = new Mesh { indexFormat = IndexFormat.UInt32, hideFlags = HideFlags.HideAndDontSave }; 31#if UNITY_EDITOR 32 wireMaterial = (Material)UnityEditor.EditorGUIUtility.LoadRequired("SceneView/HandleLines.mat"); 33 dottedWireMaterial = (Material)UnityEditor.EditorGUIUtility.LoadRequired("SceneView/HandleDottedLines.mat"); 34 solidMaterial = UnityEditor.HandleUtility.handleMaterial; 35#endif 36 } 37 38 public void Clear() 39 { 40 vertices.Clear(); 41 indices.Clear(); 42 colors.Clear(); 43 } 44 45 public void AddWireCube(Vector3 center, Vector3 size, Color color) 46 { 47 var halfSize = size / 2.0f; 48 Vector3 p0 = new Vector3(halfSize.x, halfSize.y, halfSize.z); 49 Vector3 p1 = new Vector3(-halfSize.x, halfSize.y, halfSize.z); 50 Vector3 p2 = new Vector3(-halfSize.x, -halfSize.y, halfSize.z); 51 Vector3 p3 = new Vector3(halfSize.x, -halfSize.y, halfSize.z); 52 Vector3 p4 = new Vector3(halfSize.x, halfSize.y, -halfSize.z); 53 Vector3 p5 = new Vector3(-halfSize.x, halfSize.y, -halfSize.z); 54 Vector3 p6 = new Vector3(-halfSize.x, -halfSize.y, -halfSize.z); 55 Vector3 p7 = new Vector3(halfSize.x, -halfSize.y, -halfSize.z); 56 57 AddEdge(center + p0, center + p1); 58 AddEdge(center + p1, center + p2); 59 AddEdge(center + p2, center + p3); 60 AddEdge(center + p3, center + p0); 61 62 AddEdge(center + p4, center + p5); 63 AddEdge(center + p5, center + p6); 64 AddEdge(center + p6, center + p7); 65 AddEdge(center + p7, center + p4); 66 67 AddEdge(center + p0, center + p4); 68 AddEdge(center + p1, center + p5); 69 AddEdge(center + p2, center + p6); 70 AddEdge(center + p3, center + p7); 71 72 void AddEdge(Vector3 p1, Vector3 p2) 73 { 74 vertices.Add(p1); 75 vertices.Add(p2); 76 indices.Add(indices.Count); 77 indices.Add(indices.Count); 78 colors.Add(color); 79 colors.Add(color); 80 } 81 } 82 83 void DrawMesh(Matrix4x4 trs, Material mat, MeshTopology topology, CompareFunction depthTest, string gizmoName) 84 { 85 mesh.Clear(); 86 mesh.SetVertices(vertices); 87 mesh.SetColors(colors); 88 mesh.SetIndices(indices, topology, 0); 89 90 mat.SetFloat("_HandleZTest", (int)depthTest); 91 92 var cmd = CommandBufferPool.Get(gizmoName ?? "Mesh Gizmo Rendering"); 93 cmd.DrawMesh(mesh, trs, mat, 0, 0); 94 Graphics.ExecuteCommandBuffer(cmd); 95 } 96 97 public void RenderWireframe(Matrix4x4 trs, CompareFunction depthTest = CompareFunction.LessEqual, string gizmoName = null) 98 => DrawMesh(trs, wireMaterial, MeshTopology.Lines, depthTest, gizmoName); 99 100 public void Dispose() 101 { 102 CoreUtils.Destroy(mesh); 103 } 104 } 105}