我正在使用Perlin Noise and Shader Graph生成一个网格,以基于高度添加颜色。
我想要一个网格物体的纹理,以便将其用作小地图。
如何将mesh.colors
转换为Texture2D
变量?
先谢谢了。
答案 0 :(得分:0)
如果网格是具有textureWidth
* textureHeight
个顶点的平面网格,则可以执行以下操作:
public Mesh mesh;
public int textureWidth;
public int textureHeight;
public Texture2D GenerateTexture2D(){
Texture2D texture2D = new Texture2D(textureWidth, textureHeight);
for(int h=0; h<textureHeight; h++)
for(int w=0; w<textureWidth; w++)
texture2D.SetPixel(w, h, mesh.colors[h*textureWidth + w]);
return texture2D;
}
https://docs.unity3d.com/ScriptReference/Texture2D.SetPixel.html
或:(感谢@yes)
public Mesh mesh;
public int textureWidth;
public int textureHeight;
public Texture2D GenerateTexture2D(){
Texture2D texture2D = new Texture2D(textureWidth, textureHeight);
texture2D.SetPixels(mesh.colors);
return texture2D;
}
https://docs.unity3d.com/ScriptReference/Texture2D.SetPixels.html