using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateTerrain : MonoBehaviour
{
public TerrainManager mag;
public int wid;
public int hid;
public GameObject water;
public Texture2D texture;
void Start()
{
water = transform.Find("water").gameObject;
water.transform.localPosition = Vector3.up * mag.waterLine;
water.transform.localScale = new Vector3(mag.w / 10, 1, mag.h / 10);
transform.localPosition = new Vector3(wid * mag.w, 0, hid * mag.h);
texture = new Texture2D(mag.w, mag.h);
VertexHelper vh = new VertexHelper();
for (int x = 0; x < mag.w + 1; x++)
{
for (int z = 0; z < mag.h + 1; z++)
{
var noise = Mathf.PerlinNoise((wid * mag.w + x) * mag.p, (hid * mag.h + z) * mag.p);
var y = noise * mag.yMax;
var uvx = x / (float) (mag.w);
var uvy = z / (float) (mag.h);
vh.AddVert(new Vector3(x - mag.h / 2, y, z - mag.h / 2),
Color.white, new Vector2(uvx, uvy));
if (x != mag.w && z != mag.h)
{
vh.AddTriangle(x * (mag.h + 1) + z, x * (mag.h + 1) + (z + 1), (x + 1) * (mag.h + 1) + (z + 1));
vh.AddTriangle(x * (mag.h + 1) + z, (x + 1) * (mag.h + 1) + (z + 1), (x + 1) * (mag.h + 1) + z);
}
if (y < mag.waterLine)
{
texture.SetPixel(x, z, mag.shui);
}
else
{
texture.SetPixel(x, z, Color.Lerp(mag.tu, mag.cao, noise));
if (Random.Range(0, 100) == 99)
{
var tree = Instantiate(mag.trees[Random.Range(0, 4)], mag.tree);
tree.transform.localPosition =
new Vector3(x - mag.w / 2, y, z - mag.h / 2) + transform.position;
}
}
}
}
StaticBatchingUtility.Combine(mag.tree.gameObject);
texture.Apply();
Mesh mesh = new Mesh();
vh.FillMesh(mesh);
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<MeshCollider>().sharedMesh = mesh;
GameObject map = Instantiate(Resources.Load<GameObject>("map"), mag.mapbox);
map.GetComponent<RectTransform>().sizeDelta = new Vector2(mag.w * 2, mag.h * 2);
map.GetComponent<RectTransform>().anchoredPosition = new Vector2(wid * mag.w * 2, hid * mag.h * 2);
map.name = $"{wid}{hid}";
Material material = new Material(Shader.Find("UI/Default"));
material.mainTexture = texture;
map.GetComponent<Image>().material = material;
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainManager : MonoBehaviour
{
public static TerrainManager TM;
//地图数量
public int wNum = 4;
public int hNum = 4;
public int w = 100;
public int h = 100;
public float p = 0.02f; //起伏度
public float yMax = 10;
public Color tu = Color.green;
public Color cao = Color.yellow;
public Color shui = Color.blue;
public float waterLine = 4; //水位线
public Transform mapbox;
public GameObject[] trees;
public Transform tree;
private void Awake()
{
TM = this;
}
void Start()
{
for (int wid = 0; wid < wNum; wid++)
{
for (int hid = 0; hid < hNum; hid++)
{
GameObject terrain = Instantiate(Resources.Load<GameObject>("Terrain"), transform);
var temp = terrain.GetComponent<CreateTerrain>();
temp.mag = this;
temp.wid = wid;
temp.hid = hid;
}
}
}
public Vector3 GetPos(float x, float z)
{
float noise = Mathf.PerlinNoise((x + w / 2) * p, (z + h / 2) * p);
float y = noise * yMax;
return new Vector3(x, y, z);
}
}