Unity中用Mesh生成灰度地图

using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
public class ImageMap : MonoBehaviour
{
    [SerializeField]
    private Texture2D texture;
    private int width, height;
    private void Start()
    {
        Mesh mesh = new Mesh();
        width = texture.width;
        height = texture.height;

        VertexHelper vh = new VertexHelper();
        for (int i = 0; i < texture.width; i++)
        {
            for (int j = 0; j < texture.height; j++)
            {
                float y = texture.GetPixel(i, j).grayscale;
                vh.AddVert(new Vector3(i, y * 10, j), Color.white, new Vector2((float)i / width, (float)j / height));

                //列 * 总高度 + 本列索引 就是开始位置
                int startIndex = i * height + j;

                if (i < width - 1 && j < height - 1)
                {
                    vh.AddTriangle(startIndex, startIndex + 1, startIndex + height + 1);
                    vh.AddTriangle(startIndex, startIndex + height + 1, startIndex + height);
                }
            }
        }
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshRenderer>().material = new Material(Shader.Find("Standard"));
        //GetComponent<MeshRenderer>().material.mainTexture = texture;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值