MeshRenderer MeshFilter
[RequireComponent(typeof(MeshCollider))]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class BoLinZaoSheng : MonoBehaviour
{
public Color startColor;
public Color endColor;
Texture2D texture;
VertexHelper vh;
int w, h;
void Start()
{
vh = new VertexHelper();
w = 200;
h = 200;
texture = new Texture2D(w, h);
for (int x = 0; x < h; x++)
{
for (int z = 0; z < h; z++)
{
float y = Mathf.PerlinNoise(x * 0.02f, z * 0.02f);
Color color = Color.Lerp(startColor, endColor, y);
texture.SetPixel(x, z, color);
vh.AddVert(new Vector3(x, y * 20, z), Color.red, Vector3.zero);
if (x < w - 1 && z < h - 1)
{
vh.AddTriangle(x * h + z, x * h + z + 1, (x + 1) * h + z + 1);
vh.AddTriangle(x * h + z, (x + 1) * h + z + 1, x * h + z + h);
}
}
}
texture.Apply();
vh.FillMesh(GetComponent<MeshFilter>().mesh);
GetComponent<MeshCollider>().sharedMesh = GetComponent<MeshFilter>().mesh;
Material mat = new Material(Shader.Find("Standard"));
mat.mainTexture = texture;
GetComponent<MeshRenderer>().material = mat;
}
}