Zewen Senpai
Zewen Senpai

Reputation: 1

Unity 2D Colormap based on Noisemap for terrain generation. Noisemap displaying just fine, but when toggle to Colormap nothing shows

I am trying to display a Unity 2D Colormap based on Noisemap for terrain generation. Noisemap displaying just fine, but when toggle to Colormap nothing shows. By nothing shows I mean the display is just the background and there are no entity in place of the noisemap, but when I switch back to the noisemap it still display perfectly fine. I don't really understand why or how.

The code is also based off Sebastian Lague's guide on Procedural Landmass Generation but I tried to write it based on the logic but not on the exact code and for 2D.

using UnityEngine;

public class MapDisplay : MonoBehaviour
{
    public enum DrawMode { NoiseMap, ColorMap }
    public DrawMode drawMode;

    [Header("Map Settings")]
    public int mapLength = 256;
    public int mapWidth = 256;
    public float noiseScale = 20f;
    public Vector2 offset;
    public bool autoUpdate;

    [Header("Noise Settings")]
    public int octaves = 4;
    [Range(0, 1)]
    public float persistence = 0.5f;
    public float lacunarity = 2f;
    public int seed;

    [Header("Terrain Settings")]
    public TerrainType[] regions;

    private MapCreator mapCreator;
    private SpriteRenderer spriteRenderer;

    void Start()
    {
        mapCreator = gameObject.AddComponent<MapCreator>();
        spriteRenderer = gameObject.AddComponent<SpriteRenderer>();
        GenerateAndDisplayMap();
    }

    public void GenerateAndDisplayMap()
    {
        mapLength = Mathf.Max(1, mapLength);
        mapWidth = Mathf.Max(1, mapWidth);
        octaves = Mathf.Max(0, octaves);
        lacunarity = Mathf.Max(1f, lacunarity);
        persistence = Mathf.Clamp(persistence, 0f, 1f);

        float[,] noiseMap = mapCreator.GenerateMap(mapLength, mapWidth, noiseScale, offset, octaves, lacunarity, persistence, seed);
        DisplayMap(noiseMap);
    }

    public void DisplayMap(float[,] noiseMap)
    {
        Texture2D texture;

        if (drawMode == DrawMode.NoiseMap)
        {
            texture = TextureGenerator.TextureFromHeightMap(noiseMap);
        }
        else // ColorMap
        {
            texture = GenerateTextureFromColourMap(noiseMap);
        }

        spriteRenderer.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
        spriteRenderer.transform.localScale = new Vector3(mapWidth, mapLength, 1);
    }

    private Texture2D GenerateTextureFromColourMap(float[,] noiseMap)
    {
        int width = noiseMap.GetLength(0);
        int height = noiseMap.GetLength(1);
        Color[] colourMap = new Color[width * height];

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                float currentHeight = noiseMap[x, y];
                for (int i = 0; i < regions.Length; i++)
                {
                    if (currentHeight <= regions[i].height)
                    {
                        colourMap[y * width + x] = regions[i].color;
                        break;
                    }
                }
            }
        }

        return TextureGenerator.TextureFromColourMap(colourMap, width, height);
    }
}

This is TextureFromColourMap method

    using UnityEngine;
    
    public static class TextureGenerator
    {
        public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height)
        {
            Texture2D texture = new Texture2D(width, height);
            texture.filterMode = FilterMode.Point;
            texture.wrapMode = TextureWrapMode.Clamp;
            texture.SetPixels(colourMap);
            texture.Apply();
            return texture;
        }
    
        public static Texture2D TextureFromHeightMap(float[,] heightMap)
        {
            int width = heightMap.GetLength(0);
            int height = heightMap.GetLength(1);
    
            Color[] colourMap = new Color[width * height];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    colourMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
                }
            }
    
            return TextureFromColourMap(colourMap, width, height);
        }
    }

This is the debug log for the entire process when attempting to generate the colormap:

MapDisplay: Start - Components added, generating and displaying map.
UnityEngine.Debug:Log (object)
MapDisplay:Start () (at Assets/MapDisplay.cs:32)

MapDisplay: GenerateAndDisplayMap - Parameters adjusted:
UnityEngine.Debug:Log (object)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:44)
MapDisplay:Start () (at Assets/MapDisplay.cs:33)

mapLength: 256, mapWidth: 256, octaves: 4, lacunarity: 2, persistence: 0.5
UnityEngine.Debug:Log (object)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:45)
MapDisplay:Start () (at Assets/MapDisplay.cs:33)

MapDisplay: GenerateAndDisplayMap - Noise map generated.
UnityEngine.Debug:Log (object)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:48)
MapDisplay:Start () (at Assets/MapDisplay.cs:33)

MapDisplay: DisplayMap - Drawing color map.
UnityEngine.Debug:Log (object)
MapDisplay:DisplayMap (single[,]) (at Assets/MapDisplay.cs:63)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:49)
MapDisplayEditor:OnInspectorGUI () (at Assets/Editor/MapDisplayEditor.cs:15)
UnityEditor.EditorGUI/PopupCallbackInfo:SetEnumValueDelegate (object,string[],int)

MapDisplay: GenerateTextureFromColourMap - Generating color map.
UnityEngine.Debug:Log (object)
MapDisplay:GenerateTextureFromColourMap (single[,]) (at Assets/MapDisplay.cs:78)
MapDisplay:DisplayMap (single[,]) (at Assets/MapDisplay.cs:64)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:49)
MapDisplayEditor:OnInspectorGUI () (at Assets/Editor/MapDisplayEditor.cs:15)
UnityEditor.EditorGUI/PopupCallbackInfo:SetEnumValueDelegate (object,string[],int)

MapDisplay: GenerateTextureFromColourMap - Color map generated.
UnityEngine.Debug:Log (object)
MapDisplay:GenerateTextureFromColourMap (single[,]) (at Assets/MapDisplay.cs:96)
MapDisplay:DisplayMap (single[,]) (at Assets/MapDisplay.cs:64)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:49)
MapDisplayEditor:OnInspectorGUI () (at Assets/Editor/MapDisplayEditor.cs:15)
UnityEditor.EditorGUI/PopupCallbackInfo:SetEnumValueDelegate (object,string[],int)

MapDisplay: DisplayMap - Texture applied to sprite renderer.
UnityEngine.Debug:Log (object)
MapDisplay:DisplayMap (single[,]) (at Assets/MapDisplay.cs:69)
MapDisplay:GenerateAndDisplayMap () (at Assets/MapDisplay.cs:49)
MapDisplayEditor:OnInspectorGUI () (at Assets/Editor/MapDisplayEditor.cs:15)
UnityEditor.EditorGUI/PopupCallbackInfo:SetEnumValueDelegate (object,string[],int)

Upvotes: 0

Views: 54

Answers (0)

Related Questions