Переглянути джерело

Procedural Generation 4/21

Nathan Gusatto 2 місяців тому
батько
коміт
018342f3b6

+ 28 - 2
Assets/Scenes/SampleScene.unity

@@ -542,15 +542,41 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 9640738350c442c44925c119cbc05f4c, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
+  drawMode: 1
   mapWidth: 100
   mapHeight: 100
-  noiseScale: 30
+  noiseScale: 25
   octaves: 4
   persistance: 0.5
   lacunarity: 2
-  seed: 0
+  seed: 15
   offset: {x: 0, y: 0}
   autoUpdate: 1
+  regions:
+  - name: Water Deep
+    height: 0.3
+    colour: {r: 0.22352938, g: 0.32254782, b: 0.85882354, a: 0}
+  - name: Water Shallow
+    height: 0.4
+    colour: {r: 0.22352941, g: 0.42745098, b: 0.85882354, a: 0}
+  - name: Sand
+    height: 0.45
+    colour: {r: 0.754717, g: 0.7326778, b: 0.38803846, a: 0}
+  - name: Grass
+    height: 0.55
+    colour: {r: 0.21026166, g: 0.7075472, b: 0.33747417, a: 0}
+  - name: Grass 2
+    height: 0.6
+    colour: {r: 0.07845318, g: 0.4056604, b: 0.16155343, a: 0}
+  - name: Rock
+    height: 0.7
+    colour: {r: 0.4245283, g: 0.27288505, b: 0.18623175, a: 0}
+  - name: Rock2
+    height: 0.9
+    colour: {r: 0.31132078, g: 0.2118798, b: 0.17181383, a: 0}
+  - name: Snow
+    height: 1
+    colour: {r: 0.7924528, g: 0.76148623, b: 0.6765753, a: 0}
 --- !u!4 &1447407353
 Transform:
   m_ObjectHideFlags: 0

+ 2 - 19
Assets/Scripts/MapDisplay.cs

@@ -4,26 +4,9 @@ public class MapDisplay : MonoBehaviour
 {
     public Renderer textureRenderer;
 
-    public void DrawNoiseMap(float[,] noiseMap)
+    public void DrawTexture(Texture2D texture)
     {
-        int width = noiseMap.GetLength(0);
-        int height = noiseMap.GetLength(1);
-
-        Texture2D texture = new(width, height);
-
-        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, noiseMap[x, y]);
-            }
-        }
-        texture.SetPixels(colourMap);
-        texture.Apply();
-
         textureRenderer.sharedMaterial.mainTexture = texture;
-        textureRenderer.transform.localScale = new Vector3(width, 1, height);
+        textureRenderer.transform.localScale = new Vector3(texture.width, 1, texture.height);
     }
 }

+ 39 - 1
Assets/Scripts/MapGenerator.cs

@@ -2,6 +2,10 @@ using UnityEngine;
 
 public class MapGenerator : MonoBehaviour
 {
+
+    public enum DrawMode { NoiseMap, ColourMap};
+    public DrawMode drawMode;
+
     public int mapWidth;
     public int mapHeight;
     public float noiseScale;
@@ -17,12 +21,38 @@ public class MapGenerator : MonoBehaviour
 
     public bool autoUpdate;
 
+    public TerrainType[] regions;
+
     public void GenerateMap()
     {
         float[,] noiseMap = Noise.GenerateNoiseMap(mapWidth, mapHeight, seed, noiseScale, octaves, persistance, lacunarity, offset);
 
+        Color[] colourMap = new Color[mapWidth * mapHeight];
+
+        for(int y=0; y<mapHeight; y++)
+        {
+            for (int x=0; x<mapWidth; x++)
+            {
+                float currentHeight = noiseMap[x, y];
+                for (int i = 0; i < regions.Length; i++)
+                {
+                    if(currentHeight <= regions[i].height)
+                    {
+                        colourMap[y*mapWidth + x] = regions[i].colour;
+                        break;
+                    }
+                }
+            }
+        }
+
         MapDisplay display = FindFirstObjectByType<MapDisplay>();
-        display.DrawNoiseMap(noiseMap);
+        if(drawMode == DrawMode.NoiseMap)
+        {
+            display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap));
+        } else if (drawMode == DrawMode.ColourMap)
+        {
+            display.DrawTexture(TextureGenerator.TextureFromColourMap(colourMap, mapWidth, mapHeight));
+        }
     }
 
     private void OnValidate()
@@ -47,3 +77,11 @@ public class MapGenerator : MonoBehaviour
         }
     }
 }
+
+[System.Serializable]
+public struct TerrainType
+{
+    public string name;
+    public float height;
+    public Color colour;
+}

+ 31 - 0
Assets/Scripts/TextureGenerator.cs

@@ -0,0 +1,31 @@
+using UnityEngine;
+
+public static class TextureGenerator
+{
+    public static Texture2D TextureFromColourMap(Color[] colourMap, int width, int height)
+    {
+        Texture2D texture = new(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);
+    }
+}

+ 2 - 0
Assets/Scripts/TextureGenerator.cs.meta

@@ -0,0 +1,2 @@
+fileFormatVersion: 2
+guid: cf87420f29445be4ba4c8d702699f915