- Home /

# Adding triangles to a mesh

I've got a flatshaded mesh (no shared vertices). At runtime i flatten the triangles by moving the triangles vertices Y- value to all be the same. However when i do so the neighboring triangles will be disconnected from the flattened triangles. One way to fix this is by moving the neighboring overlapping vertices to the same Y-value around it. But this distorts already flattened triangles if the new flattened area is at a different Y-value beside the previously flattened area. So i would like to connect the overlapping vertices with triangles but im not sure how to do so? the mesh-script is created from Sebastian Lague's Procedural terrain generation tutorial. The red lines is where i want new triangle faces

The 2 triangles in the square are flattened out

```
using System.Collections;
using UnityEngine;
public class AlterMesh : MonoBehaviour
{
public MeshFilter meshFilter;
public MeshCollider meshCollider;
public Mesh mesh;
Vector3[] vertices;
int[] triangles;
int[] newTriangles;
// Use this for initialization
void Start()
{
meshCollider = this.GetComponent<MeshCollider>();
meshFilter = this.GetComponent<MeshFilter>();
mesh = meshFilter.mesh;
vertices = mesh.vertices;
triangles = mesh.triangles;
newTriangles = new int[triangles.Length*2];
AddTriangles();
}
// Update is called once per frame
void Update()
{
}
public void AddTriangles()
{
for (int i = 0; i < triangles.Length; i++)
{
newTriangles[i] = triangles[i];
}
for (int i = 0; i < triangles.Length / 3 - 1; i++)
{
newTriangles[triangles.Length - 1 + (i * 3) + 0] = triangles[];
newTriangles[triangles.Length - 1 + (i * 3) + 1] = triangles[];
newTriangles[triangles.Length - 1 + (i * 3) + 2] = triangles[];
}
mesh.triangles = newTriangles;
meshFilter.mesh = mesh;
meshCollider.sharedMesh = mesh;
}
}
```

This code is about as far as i've gotten. I can't seem to figure out a way to add these triangles correctly.

I can help you verbally. since you know the indices of the vertices of the triangles you flatten, you basically know everything necessary. save the vertice vectors before flattening and after. this should give you 8 vertices. if you know how the triangle indices are arranged, you should know what the order is when viewed from the top. bow, in counter- or clockwise order, generate a mesh strip that fills the side walls being the gap. either add the 4 new vertices to the end of the array, or insert them where the originals reside. then add the strip logic for the triangle indices also somewhere into the triangle array.

**Answer** by Bunny83
·
Sep 28, 2018 at 11:45 AM

What you want to do here doesn't make too much sense. You really should do the generation of your mesh inside the "GenerateTerrainMesh" or the "MeshGenerator" class. Keep in mind that those extra triangles also need **their own vertices**. You can not reuse the vertices from the existing triangles / quads since your extra triangles need different normals.

As far as i understood you want each quad of your original terrain to be completely flat (parallel to the x-z- plane). Now you move a single quad up or down and you want to connect the edges of the quad with the 4 neighboring quads with vertical faces (quads). Unfortunately the way the mesh generator works at the moment it first creates shared vertices and later the FlatShading method actually splits the vertices per triangle index. Additionally due to the LOD mechanic build into the generation you will have a hard time to actually work out which triangles are where in the triangles or the vertices arrays. It generally seems to be the wrong approach for what you want to achieve. It seems you are more after a minecraft-like look of the terrain with actual 90° edges but not block based but with arbitrary heights for each "grid cell". If that's what you actually want you want to include those connecting quads right away when you create the grid. Something like that:

Where the green quads are the actual top surfaces that make up your grid and each grid quad is connected to the neighbor quad with an additional quad that would be vertically. So the first image just shows how the quads are connected while the second is the actual position of the vertices in the X-Z- plane.

Though we can't write the whole mesh generator you may need for you. Especially since it's not really clear what the exact result should look like. The mesh generator that "Sebastian Lague" made is based on perlin noise.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.