Bootstrap

Unity生成柏林噪声地图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CreatMap : MonoBehaviour
{
    public int x;
    public int y;
    void Start()
    {
        Texture2D texture = new Texture2D(x, y);
        VertexHelper vh = new VertexHelper();
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                float h = Mathf.PerlinNoise(i * 0.05f, j * 0.05f);
                Color color = Color.Lerp(Color.blue, Color.green, h);
                float uvx = (float)j / (x - 1);
                float uvy = (float)j / (y - 1);
                texture.SetPixel(i, j, color);
                vh.AddVert(new Vector3(i, h * 15, j), color, new Vector2(uvx, uvy));
                if (i != x - 1 && j != y - 1)
                {
                    vh.AddTriangle(i * y + j, i * y + j + 1, (i + 1) * y + j + 1);
                    vh.AddTriangle(i * y + j, (i + 1) * y + j + 1, (i + 1) * y + j);
                }
            }
        }
        texture.Apply();
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.mainTexture = texture;
    }
}

;