TextureGenerator.cs 827 B

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