Skip to content
Snippets Groups Projects
Select Git revision
  • 059af3a0f3699f3a122f96616fb07ed6169b060e
  • main default protected
2 results

TextureGenerator.cs

Blame
  • user avatar
    Clément authored
    059af3a0
    History
    TextureGenerator.cs 975 B
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class TextureGenerator
    {
        public static Texture2D TextureFromColorMap(Color[] colorMap, int width, int height)
        {
            Texture2D texture = new Texture2D(width, height);
            texture.filterMode = FilterMode.Point;
            texture.wrapMode = TextureWrapMode.Clamp;
            texture.SetPixels(colorMap);
            texture.Apply();
            return texture;
        }
    
        public static Texture2D TextureFromHeightMap(float[,] heightMap)
        {
            int width = heightMap.GetLength(0);
            int height = heightMap.GetLength(1);
    
            Color[] colorMap = new Color[width * height];
    
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
                }
            }
    
            return TextureFromColorMap(colorMap, width, height);
        }
    }