Mesh网格绘制地图

效果如下:

我们生成的这个地图是一定限制的,不是无限大,可以通过面板去调整凹凸部分的颜色以及大小。

代码如下:

using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class DItu : MonoBehaviour
{
    public Texture2D texture;
    public int wh = 128;
    public int wid;
    public int hig;
    public float p = 0.1f;
    public Color gao;
    public Color di;
    // Start is called before the first frame update
    void Start()
    {
        VertexHelper vh=new VertexHelper();
        texture = new Texture2D(wh, wh);
        for(int x = 0; x < wh; x++)
        {
            for(int y=0;y < wh; y++) {
                float noise = Mathf.PerlinNoise((x + wh * wid) * p,(y+wh*hig)*p);
                float z=noise*4;
                Color color=Color.Lerp(gao,di,noise);
                texture.SetPixel(x,y,color);
                float uvx = (float)x /(float) wh;
                float uvy = (float)y / (float)wh;
                vh.AddVert(new Vector3(x, z, y), color, new Vector2(uvx, uvy));
                if(x < wh-1&&y<wh-1) {
                    vh.AddTriangle(x * wh + y, x * wh + y + 1, (x + 1) * wh + y + 1);
                    vh.AddTriangle(x * wh + y, (x + 1) * wh + y + 1, (x + 1) * wh + y);
                }
            }
        }
        texture.Apply();
        Mesh mesh=new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshCollider>().sharedMesh = mesh;
        GetComponent<MeshRenderer>().material.mainTexture= texture;
    }


}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值