Unity3D:如何将网格的颜色数组转换为texture2D

时间:2019-06-13 09:37:15

标签: unity3d textures mesh texture-mapping

我正在使用Perlin Noise and Shader Graph生成一个网格,以基于高度添加颜色。

我想要一个网格物体的纹理,以便将其用作小地图。

如何将mesh.colors转换为Texture2D变量?

先谢谢了。

1 个答案:

答案 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