地图及地形

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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值