So, I’m pretty new to creating my own meshes in Unity, and I learned the basics from a Brakceys tutorial. However, I started to notice that, after I use Perlin Noise to change certain vertices’ height, strange faces keep appearing, most of which seem to be connected to the first vertice. Does it have to do with the way Unity’s meshes handle vertices? I know I am under the limit, which is, I believe, 65536 (256 * 256). Any help would be appreciated greatly. Here’s the code I used to generate the mesh:
private class Island
{
private int sizeInVerts;
public int SizeInVerts
{
get { return sizeInVerts; }
}
private Vector3 position;
public Vector3 Position
{
get { return position; }
}
private Mesh mesh;
public Mesh IslandMesh
{
get { return mesh; }
}
private GameObject meshObject;
public GameObject MeshObject
{
get { return meshObject; }
}
public Island (int _sizeInVerts, Vector3 _position)
{
sizeInVerts = _sizeInVerts;
position = _position;
mesh = new Mesh();
meshObject = new GameObject();
meshObject.name = position.ToString();
meshObject.transform.position = position;
meshObject.transform.parent = GameObject.Find("Islands").transform;
meshObject.AddComponent<MeshCollider>().sharedMesh = mesh;
}
private Vector3[] MeshVerts;
private int[] MeshTris;
private void UpdateMesh()
{
MeshFilter meshFilter = meshObject.AddComponent<MeshFilter>();
MeshRenderer meshRenderer = meshObject.AddComponent<MeshRenderer>();
var GrassMat = Resources.Load("Materials/Grass_Mat") as Material;
meshRenderer.materials = new Material[] {GrassMat};
mesh.Clear();
mesh.vertices = MeshVerts;
mesh.triangles = MeshTris;
meshFilter.mesh = mesh;
mesh.RecalculateNormals();
meshObject.transform.position = position;
}
public void Generate()
{
MeshVerts = new Vector3[(SizeInVerts + 1) * (SizeInVerts + 1)];
MeshTris = new int[SizeInVerts * SizeInVerts * 6];
int Vert = 0;
int Tri = 0;
// Create Vertices
for (int x = 0; x < SizeInVerts; x++)
{
for (int z = 0; z < SizeInVerts; z++)
{
// 0.025 / 2
float YNoise = Mathf.PerlinNoise(x * 0.025F / 2, z * 0.025F / 2);
float YValue = 0;
if (YNoise >= 0 & YNoise <= 0.40F)
{
YValue = 0;
}
else if (YNoise > 0.40F & YNoise <= 0.50F)
{
YValue = 3;
}
else if (YNoise > 0.50F)
{
YValue = 5;
}
// Create new Vertice using x and z
Vector3 NewVert = new Vector3(x, YValue, z);
MeshVerts[Vert] = NewVert;
Vert++;
}
Vert++;
}
print("Vert: " + Vert);
Vert = 0;
for (int x = 0; x < SizeInVerts; x++)
{
for (int z = 0; z < SizeInVerts; z++)
{
// Create new Trianges
MeshTris[Tri + 0] = Vert + 0;
MeshTris[Tri + 1] = Vert + 1;
MeshTris[Tri + 2] = Vert + SizeInVerts + 1;
MeshTris[Tri + 3] = Vert + 1;
MeshTris[Tri + 4] = Vert + SizeInVerts + 2;
MeshTris[Tri + 5] = Vert + SizeInVerts + 1;
Vert++;
Tri += 6;
}
Vert++;
}
UpdateMesh();
}