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?