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;
}
}
Unity中用Mesh生成灰度地图
最新推荐文章于 2025-05-20 18:29:08 发布