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];


    // Update is called once per frame
    void Update()


    public void AddTriangles()
        for (int i = 0; i < triangles.Length; i++)
            newTriangles _= triangles*;*_


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.

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.