A material is disappearing with a custom mesh

Here’s a simple script for mesh generation

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (MeshFilter))]
[RequireComponent (typeof (MeshRenderer))]

public class Terramorfing : MonoBehaviour {

    public int height, quad;
    public  float chunk_size;

    private MeshFilter mf;
    private Mesh mesh;
    private Vector3[] verticles, normals;
    private int[] triangles;
    private Vector2[] uv;

    /*  2----3
     *  |    |
     *  |    |
     *  0----1
     */

    void Start () 
    {
            mf = GetComponent<MeshFilter>();
            mesh = new Mesh();

    }
    

    void Update () 
    {
            if(Input.GetMouseButtonDown(0))
                    MeshGenerator();

    }

    void MeshGenerator()
    {
            verticles = new Vector3[quad * 4];
            normals = new Vector3[quad * 4];
            triangles = new int[quad * 6];
            uv = new Vector2[quad * 4];
            
            for(int i = 0, quad_index = 0, trianlges_index = 0; i < quad; i++, quad_index = i * 4, trianlges_index = i * 6)
            {
                    verticles[quad_index] = new Vector3(i * chunk_size, 0);
                    verticles[quad_index + 1] = new Vector3((i + 1) * chunk_size, 0);
                    verticles[quad_index + 2] = new Vector3((i + 1) * chunk_size, height); // 2 аргумент - высота 
                    verticles[quad_index + 3] = new Vector3(i * chunk_size, height); // 2 аргумент - высота 
                    
                    normals[quad_index] = Vector3.back;
                    normals[quad_index + 1] = Vector3.back;
                    normals[quad_index + 2] = Vector3.back;
                    normals[quad_index + 3] = Vector3.back;
                    
                    triangles[trianlges_index] = quad_index;
                    triangles[trianlges_index + 1] = quad_index + 2;
                    triangles[trianlges_index + 2] = quad_index + 1;
                    triangles[trianlges_index + 3] = quad_index;
                    triangles[trianlges_index + 4] = quad_index + 3;
                    triangles[trianlges_index + 5] = quad_index + 2;
                    
                    uv[quad_index] = new Vector2(0, 0);
                    uv[quad_index + 1] = new Vector2(1, 0);
                    uv[quad_index + 2] = new Vector2(1, 1);
                    uv[quad_index + 3] = new Vector2(0, 1);
                    
                    mesh.vertices = verticles;
                    mesh.normals = normals;
                    mesh.triangles = triangles;
                    mesh.uv = uv;
                    
                    mf.mesh = mesh;
            }
    }

}

This one works just fine

http://i080.radikal.ru/1412/71/88c4144f87c2.png

If I try to change the camera position, then in the editor or game window the material on my custom mesh just disappears

http://s020.radikal.ru/i704/1412/00/e1a0cf3b16d0.png

http://s010.radikal.ru/i313/1412/94/d57d34af29c4.png

Could someone please help me solve this issue?

Hi, first, there is something not optimal in your mesh creation loop: you assign mesh.vertices, triangles, normals etc… way too often: you need to do this only once, after/outside the for loop:

vertices = new...
for ( i ) {...}
mesh.vertices = vertices
mf.mesh = mesh

The disappearance of the mesh is caused by the bounding volume of the mesh that is not updated: To optimize rendering, Unity will not display a mesh that is outside the camera frustum. To know a mesh is inside or outside, it has to check if any triangle is inside the frustum. This would be a too expensive operation so to simplify, it computes a bounding box (once) that contains all the vertices of the mesh; then, it tests this bounding box against the camera frustum. If the box is inside, it draws the mesh.

You then need to update this box each time you modify vertices positions. You can use Unity - Scripting API: Mesh.RecalculateBounds to do so or compute it yourself and pass it to the mesh Unity - Scripting API: Mesh.bounds