Hi everyone,
I’m creating a mesh for my terrain. But when unity render it, it seems the lowest triangles (in terms of altitude) are render in front of the other.
Do you have any idea where it’s coming from ?
void GenerateTerrain()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
ApplyTexture();
UpdateMesh();
}
void CreateShape()
{
vertices = new Vector3[(width + 1) * (height + 1)];
uvs = new Vector2[(width + 1) * (height + 1)];
for (int i = 0, z = 0; z <= height; z++)
{
for (int x = 0; x <= width; x++)
{
float y = Mathf.Round(Mathf.Max(0, (Mathf.PerlinNoise(x * 0.05f, z * 0.05f) + Mathf.PerlinNoise(10+ x * 0.1f, 10+ z * 0.1f)) - 0.2f )*8);
vertices[i] = new Vector3(x, y, z);
uvs[i] = new Vector2((float)x / width, (float)z / height);
i++;
}
}
triangles = new int[width * height * 6];
int vert = 0;
int tris = 0;
for (int z = 0; z < height; z++)
{
for (int x = 0; x < width; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + width + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + width + 1;
triangles[tris + 5] = vert + width + 2;
vert++;
tris += 6;
}
vert++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.uv = uvs;
mesh.RecalculateNormals();
Renderer renderer = GetComponent<Renderer>();
renderer.material = terrainMaterial;
//renderer.material.mainTexture = terrainMaterial;
}