TextureGenerator.cs 983 B

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. public static class TextureGenerator
  3. {
  4. public static Texture2D TextureFromColorMap(Color[] colorMap, int width, int height)
  5. {
  6. Texture2D texture = new Texture2D(width, height);
  7. texture.filterMode = FilterMode.Point;
  8. texture.wrapMode = TextureWrapMode.Clamp;
  9. texture.SetPixels(colorMap);
  10. texture.Apply();
  11. return texture;
  12. }
  13. public static Texture2D TextureFromHeightMap(float[,] heightMap)
  14. {
  15. int width = heightMap.GetLength(0);
  16. int height = heightMap.GetLength(1);
  17. Texture2D texture = new Texture2D(width, height);
  18. Color[] colorMap = new Color[width * height];
  19. for (int y = 0; y < height; y++)
  20. {
  21. for (int x = 0; x < width; x++)
  22. {
  23. colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
  24. }
  25. }
  26. return TextureFromColorMap(colorMap, width, height);
  27. }
  28. }