|
| 1 | +using UnityEngine; |
| 2 | +using UnityEngine.Rendering; |
| 3 | + |
| 4 | +namespace Zigurous.Graphics |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Utility class to generate procedural meshes at runtime. |
| 8 | + /// </summary> |
| 9 | + public static class MeshGenerator |
| 10 | + { |
| 11 | + public delegate Vector3 VertexGenerator(int x, int y, float u, float v); |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Creates a new procedural mesh of a grid of points. |
| 15 | + /// </summary> |
| 16 | + /// <param name="width">The width of the grid of points.</param> |
| 17 | + /// <param name="height">The height of the grid of points.</param> |
| 18 | + public static Mesh Create(int width, int height) |
| 19 | + { |
| 20 | + Mesh mesh = new Mesh(); |
| 21 | + |
| 22 | + if (width * height > 65535) { |
| 23 | + mesh.indexFormat = IndexFormat.UInt32; |
| 24 | + } |
| 25 | + |
| 26 | + mesh.vertices = CreateVertices(width, height, DefaultVertexGenerator); |
| 27 | + mesh.triangles = CreateTriangles(width, height); |
| 28 | + mesh.uv = CreateUVs(width, height); |
| 29 | + mesh.RecalculateNormals(); |
| 30 | + |
| 31 | + return mesh; |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Creates a new procedural mesh using a custom vertex function. |
| 36 | + /// </summary> |
| 37 | + /// <param name="width">The width of the grid of points.</param> |
| 38 | + /// <param name="height">The height of the grid of points.</param> |
| 39 | + /// <param name="vertexGenerator">A custom function to calculate the vertex for a given point.</param> |
| 40 | + public static Mesh Create(int width, int height, VertexGenerator vertexGenerator) |
| 41 | + { |
| 42 | + if (vertexGenerator == null) { |
| 43 | + vertexGenerator = DefaultVertexGenerator; |
| 44 | + } |
| 45 | + |
| 46 | + Mesh mesh = new Mesh(); |
| 47 | + |
| 48 | + if (width * height > 65535) { |
| 49 | + mesh.indexFormat = IndexFormat.UInt32; |
| 50 | + } |
| 51 | + |
| 52 | + mesh.vertices = CreateVertices(width, height, vertexGenerator); |
| 53 | + mesh.triangles = CreateTriangles(width, height); |
| 54 | + mesh.uv = CreateUVs(width, height); |
| 55 | + mesh.RecalculateNormals(); |
| 56 | + |
| 57 | + return mesh; |
| 58 | + } |
| 59 | + |
| 60 | + private static Vector3[] CreateVertices(int width, int height, VertexGenerator vertexGenerator) |
| 61 | + { |
| 62 | + Vector3[] verticies = new Vector3[(width + 1) * (height + 1)]; |
| 63 | + |
| 64 | + int index = 0; |
| 65 | + |
| 66 | + for (int y = 0; y <= height; y++) |
| 67 | + { |
| 68 | + for (int x = 0; x <= width; x++) |
| 69 | + { |
| 70 | + float u = x / (float)width; |
| 71 | + float v = y / (float)height; |
| 72 | + |
| 73 | + verticies[index] = vertexGenerator(x, y, u, v); |
| 74 | + index++; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return verticies; |
| 79 | + } |
| 80 | + |
| 81 | + private static int[] CreateTriangles(int width, int height) |
| 82 | + { |
| 83 | + int[] triangles = new int[width * height * 6]; |
| 84 | + |
| 85 | + int vert = 0; |
| 86 | + int tris = 0; |
| 87 | + |
| 88 | + for (int y = 0; y < height; y++) |
| 89 | + { |
| 90 | + for (int x = 0; x < width; x++) |
| 91 | + { |
| 92 | + triangles[tris + 0] = vert + 0; |
| 93 | + triangles[tris + 1] = vert + width + 1; |
| 94 | + triangles[tris + 2] = vert + 1; |
| 95 | + triangles[tris + 3] = vert + 1; |
| 96 | + triangles[tris + 4] = vert + width + 1; |
| 97 | + triangles[tris + 5] = vert + width + 2; |
| 98 | + |
| 99 | + vert++; |
| 100 | + tris += 6; |
| 101 | + } |
| 102 | + |
| 103 | + vert++; |
| 104 | + } |
| 105 | + |
| 106 | + return triangles; |
| 107 | + } |
| 108 | + |
| 109 | + private static Vector2[] CreateUVs(int width, int height) |
| 110 | + { |
| 111 | + Vector2[] uvs = new Vector2[(width + 1) * (height + 1)]; |
| 112 | + |
| 113 | + int index = 0; |
| 114 | + |
| 115 | + for (int y = 0; y <= height; y++) |
| 116 | + { |
| 117 | + for (int x = 0; x <= width; x++) |
| 118 | + { |
| 119 | + float u = x / (float)width; |
| 120 | + float v = y / (float)height; |
| 121 | + |
| 122 | + uvs[index] = new Vector2(u, v); |
| 123 | + index++; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return uvs; |
| 128 | + } |
| 129 | + |
| 130 | + private static Vector3 DefaultVertexGenerator(int x, int y, float u, float v) |
| 131 | + { |
| 132 | + return new Vector3(x, 0f, y); |
| 133 | + } |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments