- Home /
Remove vertices and triangles that are at a cirtain y value
Currently I'm generating a mesh at runtime based off of a heightmap. I have the UV unwraping and heightmap generation all working, but now I have an issue where I want there to be no mesh where there is black on the heightmap.
It's working OK so far, but there are some spots where the triangles come down to the 0/no data area, and I'm not sure how to fix it.
Here is my heightmap: (There are some "no data" values on the heightmap, it's square but the no data values show as transparent on here. In Unity they are converted to black)
When I generate my mesh, it comes out looking like this:
I don't want anything coming down to the "no data" area, but you can see there is stuff coming down. I believe this has to do with me generating all the vertices, and then if the area has data, adding the triangles, but I'm not sure how to fix it.
Here is my relevant function:
void CreateMesh (Texture2D texture, int Startx, int Starty, int Width, int Height)
{
if (Width > 250 && Height > 250)
return;
List<Vector3> verts = new List<Vector3> ();
List<int> tris = new List<int> ();
List<Vector2> uvs = new List<Vector2> ();
if (Startx + Width > texture.width)
Width = texture.width - Startx;
if (Starty + Height > texture.height)
Height = texture.height - Starty;
//uses 0x0 as the zero "no data" value
float zero = texture.GetPixel (0, 0).grayscale * 100;
//Bottom left section of the map, other sections are similar
for (int i = 0; i < Width; i++) {
for (int j = 0; j < Height; j++) {
float y = texture.GetPixel (Startx + i, Starty + j).grayscale * 100;
//Add each new vertex in the plane
verts.Add (new Vector3 (i, y, j));
uvs.Add (new Vector2 ((Startx + i) / ((float)texture.width), (Starty + j) / ((float)texture.height)));
//Skip if a new square on the plane hasn't been formed
if (i == 0 || j == 0)
continue;
if (texture.GetPixel(Startx+i,Starty+j) == Color.black || y < zero + ErrorMargin) continue;
//Adds the index of the three vertices in order to make up each of the two tris
tris.Add (Width * i + j); //Top right
tris.Add (Width * i + j - 1); //Bottom right
tris.Add (Width * (i - 1) + j - 1); //Bottom left - First triangle
tris.Add (Width * (i - 1) + j - 1); //Bottom left
tris.Add (Width * (i - 1) + j); //Top left
tris.Add (Width * i + j); //Top right - Second triangle
}
}
GameObject plane = new GameObject ("ProcPlane"); //Create GO and add necessary components
plane.AddComponent<MeshFilter> ();
MeshRenderer renderer = plane.AddComponent<MeshRenderer> () as MeshRenderer;
renderer.material = mat;
Mesh procMesh = new Mesh ();
procMesh.vertices = verts.ToArray (); //Assign verts, uvs, and tris to the mesh
procMesh.uv = uvs.ToArray ();
procMesh.triangles = tris.ToArray ();
procMesh.RecalculateNormals (); //Determines which way the triangles are facing
plane.GetComponent<MeshFilter> ().mesh = procMesh; //Assign Mesh object to MeshFilter
plane.transform.position = new Vector3 (Startx, 0, Starty);
MeshCollider meshC = plane.AddComponent<MeshCollider> () as MeshCollider;
meshC.sharedMesh = procMesh;
}
When you add triangles you're going back and connecting vertices from the current vertex to those on the bottom, bottom left, and left of the vertex. You're getting those artifacts because the current vertex has a valid y-value but the vertices below it do not. You'll have to put in a check to see if those are also valid in order to get rid of the artifacting.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
RunTime hole in mesh not working 0 Answers
Spawned Object Gradient Mesh Colors? 2 Answers