Unity 中的程序纹理?

Sri*_*ala 3 textures procedural-generation texture2d unity-game-engine

我主要是一个程序员,不太擅长绘图,所以我决定在运行时以程序方式制作纹理,我的意思是每次都生成一些新的和新鲜的东西,它应该看起来可信。如何获得从这个开始?在 Unity 中编程,如果这会有所帮助的话。

jga*_*ant 6

您可以通过执行以下操作在代码中创建纹理:

public Texture2D CreateTexture()
{
    int width = 100;
    int height = 100;

    texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
    texture.filterMode = FilterMode.Point;

    for (int i = 0; i < width; i++) {
        for (int j = 0; j < height; j++) {
            texture.SetPixel(j, Height-1-i, Color.red);
        }
    }
    texture.Apply();
    return texture;
}
Run Code Online (Sandbox Code Playgroud)

如果您想优化,您可能需要查看Texture2D.SetPixels(),因为Texture2D.SetPixel()慢得多。

对于程序纹理生成,这是一个非常广泛的主题,涉及各种技术。通常,您会使用某种相干噪声生成器来生成纹理,例如 Perlin 或 Simplex。

您可以在 google 上搜索“Texture Generation Noise”,并找到大量解释如何执行此操作的文章。

这个问题非常广泛,所以希望可以帮助您入门。