How to split vertices?

Hi,

I’m experimenting with procedurally generated grids, and I’m trying to find a way in which I can split the vertices, that way no vertices are shared between two triangles. This should be able to give me ‘sharp’ edges in my mesh.

The problem is, I can’t seem to figure out how to do that. I’m pretty new to mesh generation, and if anyone can point me to the right direction that’ll be great! :slight_smile:

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class TGen : MonoBehaviour {
	
	private Mesh mesh;
	
	private void OnEnable () {
		if (mesh == null) {
			mesh = new Mesh();
			mesh.name = "Surface Mesh";
			GetComponent<MeshFilter>().mesh = mesh;
		}
		Refresh();
	}
	
	public void Refresh () {
		CreateGrid();
	}

	private void CreateGrid () {
		Vector3[] vertices = new Vector3[(33 + 1) * (33 + 1)];
		Vector2[] uv = new Vector2[vertices.Length];
		float stepSize = 1f / 33;
		for (int v = 0, y = 0; y <= 33; y++) {
			for (int x = 0; x <= 33; x++, v++) {
				vertices[v] = new Vector3(x * stepSize - 0.5f, y * stepSize - 0.5f);
				uv[v] = new Vector2(x * stepSize, y * stepSize);

			}
		}
		mesh.vertices = vertices;
		mesh.triangles = new int[] {
			0, 2, 1,
			1, 2, 3
		};

		int[] triangles = new int[33 * 33 * 6];
		for (int t = 0, v = 0, y = 0; y < 33; y++, v++) {
			for (int x = 0; x < 33; x++, v++, t += 6) {
				triangles[t] = v;
				triangles[t + 1] = v + 33 + 1;
				triangles[t + 2] = v + 1;
				triangles[t + 3] = v + 1;
				triangles[t + 4] = v + 33 + 1;
				triangles[t + 5] = v + 33 + 2;
			}
		}
		mesh.triangles = triangles;
	}
}

Take a look at one of the numerous Minecraft terrain scripts for Unity. The idea is that you don’t create all the vertices and UVs at once; Instead, you declare an array that will hold all tile/cube positions; 2-dimensional for 2d-terrain, 3-dimensional array for 3d terrain (as in MC). You then iterate through each position in this array and at each position, you create only the required vertices for that position.

I found this tool, the Oasis Mesh Editor, that can Split Vertices and Merge Vertices back together, Oasis Mesh Editor | Modeling | Unity Asset Store

The Merge Verts tool also has a Max Tolerance setting so that only verts that are closer than this distance will be merged.

Hope this helps anyone looking =)