Unity3D: Как преобразовать цветовой массив сетки в texture2D - PullRequest
0 голосов
/ 13 июня 2019

Я создаю сетку, используя Perlin Noise и Shader Graph, чтобы добавить цвета в зависимости от высоты.

Я хочу иметь текстуру сетки, чтобы она использовалась в качестве мини-карты.

Как преобразовать mesh.colors в Texture2D переменную?

Заранее спасибо.

1 Ответ

0 голосов
/ 13 июня 2019

Если ваша сетка является плоской сеткой с 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

...