Я создаю сетку, используя Perlin Noise и Shader Graph, чтобы добавить цвета в зависимости от высоты.
Я хочу иметь текстуру сетки, чтобы она использовалась в качестве мини-карты.
Как преобразовать mesh.colors в Texture2D переменную?
mesh.colors
Texture2D
Заранее спасибо.
Если ваша сетка является плоской сеткой с textureWidth*textureHeight вершинами, вы можете сделать:
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