TextureGenerator.cs 918 B

12345678910111213141516171819202122232425262728293031
  1. using UnityEngine;
  2. public static class TextureGenerator
  3. {
  4. public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height)
  5. {
  6. Texture2D texture = new(width, height);
  7. texture.filterMode = FilterMode.Point;
  8. texture.wrapMode = TextureWrapMode.Clamp;
  9. texture.SetPixels(colourMap);
  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. Color[] colourMap = new Color[width * height];
  18. for (int y = 0; y < height; y++)
  19. {
  20. for (int x = 0; x < width; x++)
  21. {
  22. colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
  23. }
  24. }
  25. return TextureFromColourMap(colourMap, width, height);
  26. }
  27. }