public Texture2D texture;//图片(设为可读取)
    int w;//图片的宽
    int h;//图片的高

    void Start()
    {
        w = texture.width;//w*h不能超过65000
        h = texture.height;
        VertexHelper vh = new VertexHelper();
        vh.Clear();
        for (int x = 0; x < w; x++)
        {
            for (int z = 0; z < h; z++)
            {
                Color color = texture.GetPixel(x, z);//获取图片的颜色
                float y = color.grayscale * 10;
                vh.AddVert(new Vector3(x, y, z), Color.white, new Vector2((float)x / w, (float)z / h));
            }
        }
        for (int i = 0; i < w-1; i++)
        {
            for (int j = 0; j < h-1; j++)
            {
                vh.AddTriangle(i * h + j, i * h + j + 1, (i + 1) * h + j + 1);
                vh.AddTriangle(i * h + j, (i + 1) * h + j + 1, (i + 1) * h + j);
            }
        }
        Mesh mesh = new Mesh();
        vh.FillMesh(mesh);
        GetComponent<MeshFilter>().mesh = mesh;
        GetComponent<MeshRenderer>().material.mainTexture = texture;
        GetComponent<MeshCollider>().sharedMesh = mesh;
    }

 

posted on 2023-03-21 09:08  a帝  阅读(69)  评论(0编辑  收藏  举报