柏林噪音图

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Map : MonoBehaviour
{
public RectTransform playerimage;//小地图上的玩家
public Transform player;//玩家
public Image map;//地图
public Texture2D texture;
public Color tu = Color.yellow;
public Color cao = Color.green;
int w = 100;
int h = 100;
void Start()
{
texture = new Texture2D(w, h);
VertexHelper vh = new VertexHelper();
for (int x = 0; x < w; x++)
{
for (int z = 0; z < h; z++)
{
float y = Mathf.PerlinNoise(x * 0.1f, z * 0.1f);
float uvx = (float)x / (float)(w - 1);
float uvy = (float)z / (float)(h - 1);
Color color = Color.Lerp(tu, cao, y);
texture.SetPixel(x, z, color);
vh.AddVert(new Vector3(x-w/2, y * 10, z-h/2), Color.white, new Vector2(uvx, uvy));
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 + 1) * h + z);
}
}
}
texture.Apply();
Mesh mesh = new Mesh();
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshCollider>().sharedMesh = mesh;
Material material = new Material(Shader.Find("Standard"));
material.mainTexture = texture;
GetComponent<MeshRenderer>().material = material;
}
void Update()
{
float pivotX = (player.transform.position.x + w / 2) / (float)w;
float pivotY = (player.transform.position.z + h / 2) / (float)h;
map.rectTransform.pivot = new Vector2(pivotX, pivotY);
playerimage.eulerAngles = Vector3.back * player.eulerAngles.y;
if ((map.rectTransform.localScale + Vector3.one *Input.GetAxis("Mouse ScrollWheel")).x>=1)
{
map.rectTransform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");
}
}
}