【问题标题】:Unity3D: How to convert a mesh's color's array to a texture2DUnity3D:如何将网格的颜色数组转换为纹理 2D
【发布时间】:2019-10-27 21:01:36
【问题描述】:

我正在使用 Perlin Noise 和 Shader Graph 生成网格,以根据高度添加颜色。

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

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

提前致谢。

【问题讨论】:

标签: unity3d textures mesh texture-mapping


【解决方案1】:

如果您的网格是具有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

【讨论】:

  • 既然他想使用顶点颜色 (mesh.colors) 并且它已经是一维颜色数组,为什么不直接使用SetPixels(Color[])
  • @yes 谢谢,不知道。
猜你喜欢
  • 2018-05-21
  • 2018-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 1970-01-01
相关资源
最近更新 更多