Skip to content

Commit 5d05717

Browse files
author
Cory Leach
committed
FalloffLayerGenerator and NoiseMapLayerGenerator
Creating generic falloff and noise map layers because they are not necessarily only used for height maps. Documentation for TextureUtility More clear names for TextureUtilityMethods Started work on visualizer for FloatMaps Added Tags to layers so we can potentially query for tagged layers in map data
1 parent efb4cd0 commit 5d05717

19 files changed

+346
-60
lines changed

Runtime/Utility/TextureUtility.cs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
namespace Gameframe.Procgen
55
{
6-
76
public static class TextureUtility
87
{
9-
public static Texture2D GetHeightMap(float[,] heightMap)
8+
/// <summary>
9+
/// Creates a texture from a 2 dimensional array of floats
10+
/// </summary>
11+
/// <param name="floatMap">2 dimensional array of floats</param>
12+
/// <param name="a">left hand side of texture color lerp</param>
13+
/// <param name="b">right hand side of texture color lerp</param>
14+
/// <returns>Texture2D filled with colors visualizing the 2d array</returns>
15+
public static Texture2D CreateFrom2dFloatMap(float[,] floatMap, Color a, Color b)
1016
{
11-
var width = heightMap.GetLength(0);
12-
var height = heightMap.GetLength(1);
17+
var width = floatMap.GetLength(0);
18+
var height = floatMap.GetLength(1);
1319

1420
var texture = new Texture2D(width, height);
1521
var colorMap = new Color[width * height];
@@ -18,7 +24,7 @@ public static Texture2D GetHeightMap(float[,] heightMap)
1824
{
1925
for (int x = 0; x < width; x++)
2026
{
21-
colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
27+
colorMap[y * width + x] = Color.Lerp(a, b, floatMap[x, y]);
2228
}
2329
}
2430

@@ -29,19 +35,53 @@ public static Texture2D GetHeightMap(float[,] heightMap)
2935
return texture;
3036
}
3137

32-
public static Texture2D GetHeightMap(float[] heightMap, int width, int height)
38+
/// <summary>
39+
/// Creates a texture from a 2 dimensional array of floats
40+
/// Maps float values 0->1 onto a texture be lerping pixel color from black and white
41+
/// </summary>
42+
/// <param name="floatMap">2 dimensional array of floats</param>]
43+
/// <returns>Texture2D filled with colors visualizing the 2d array</returns>
44+
public static Texture2D CreateFrom2dFloatMap(float[,] floatMap)
45+
{
46+
return CreateFrom2dFloatMap(floatMap, Color.black, Color.white);
47+
}
48+
49+
/// <summary>
50+
/// Create texture from a float map
51+
/// Maps float values 0->1 onto a texture be lerping pixel color from black and white
52+
/// </summary>
53+
/// <param name="floatMap">array of floats</param>
54+
/// <param name="width">width of map/texture</param>
55+
/// <param name="height">height of map/texture</param>
56+
/// <returns>a Texture2D filled with colors representing the map</returns>
57+
public static Texture2D CreateFromFloatMap(float[] floatMap, int width, int height)
58+
{
59+
return CreateFromFloatMap(floatMap, width, height, Color.black, Color.white);
60+
}
61+
62+
/// <summary>
63+
/// Create texture from a float map
64+
/// Maps float values 0->1 onto a texture be lerping pixel color from color 'a' to color 'b'
65+
/// </summary>
66+
/// <param name="floatMap">array of floats</param>
67+
/// <param name="width">width of map/texture</param>
68+
/// <param name="height">height of map/texture</param>
69+
/// <param name="a">Color that maps to 0</param>
70+
/// <param name="b">Color that maps to 1</param>
71+
/// <returns>a Texture2D filled with colors representing the map</returns>
72+
public static Texture2D CreateFromFloatMap(float[] floatMap, int width, int height, Color a, Color b)
3373
{
3474
var texture = new Texture2D(width, height);
3575
var colorMap = new Color[width * height];
3676

37-
if (heightMap.Length < colorMap.Length)
77+
if (floatMap.Length < colorMap.Length)
3878
{
3979
throw new Exception("Height size does not match texture size");
4080
}
4181

42-
for (int i = 0; i < heightMap.Length; i++)
82+
for (var i = 0; i < floatMap.Length; i++)
4383
{
44-
colorMap[i] = Color.Lerp(Color.black, Color.white, heightMap[i]);
84+
colorMap[i] = Color.Lerp(a, b, floatMap[i]);
4585
}
4686

4787
texture.filterMode = FilterMode.Point;
@@ -51,7 +91,14 @@ public static Texture2D GetHeightMap(float[] heightMap, int width, int height)
5191
return texture;
5292
}
5393

54-
public static Texture2D GetColorMap(Color[] colorMap, int width, int height)
94+
/// <summary>
95+
/// Creates a texture from an array of colors
96+
/// </summary>
97+
/// <param name="colorMap">Array of colors</param>
98+
/// <param name="width">width of color map and texture</param>
99+
/// <param name="height">height of color map and texture</param>
100+
/// <returns>Texture2D filled with the color map</returns>
101+
public static Texture2D CreateFromColorMap(Color[] colorMap, int width, int height)
55102
{
56103
var texture = new Texture2D(width, height);
57104
texture.filterMode = FilterMode.Point;
@@ -61,6 +108,11 @@ public static Texture2D GetColorMap(Color[] colorMap, int width, int height)
61108
return texture;
62109
}
63110

111+
/// <summary>
112+
/// Writes the texture to disk in png format
113+
/// </summary>
114+
/// <param name="tex2d">Texture to write to disk</param>
115+
/// <param name="fullPath">Full path to write file to</param>
64116
public static void SaveTextureAsPNG(Texture2D tex2d, string fullPath)
65117
{
66118
var bytes = tex2d.EncodeToPNG();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using UnityEngine;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
public class FloatLayerVisualizer : MonoBehaviour
6+
{
7+
[SerializeField]
8+
private WorldMapLayerGenerator _layerGenerator;
9+
}
10+
}

Runtime/Visualizers/FloatLayerVisualizer.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
1-
namespace Gameframe.Procgen
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Gameframe.Procgen
25
{
36
public interface IWorldMapLayerData
47
{
8+
string Tag { get; set; }
9+
}
10+
11+
[Serializable]
12+
public abstract class WorldMapLayerData : IWorldMapLayerData
13+
{
14+
[SerializeField]
15+
private string tag;
16+
public string Tag
17+
{
18+
get => tag;
19+
set => tag = value;
20+
}
521
}
622
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using UnityEngine;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
[CreateAssetMenu(menuName = "Gameframe/Procgen/Layers/Falloff Map")]
6+
public class FalloffLayerGenerator : WorldMapLayerGenerator
7+
{
8+
[SerializeField] private bool randomizeOffset = false;
9+
10+
[SerializeField] private float minOffsetY = 0f;
11+
[SerializeField] private float minOffsetX = 0f;
12+
13+
[SerializeField] private float maxOffsetY = 1f;
14+
[SerializeField] private float maxOffsetX = 1f;
15+
16+
[SerializeField] private Vector2 falloffOffset = Vector2.zero;
17+
18+
[SerializeField] private float falloffA = 3f;
19+
20+
[SerializeField] private float falloffB = 2.2f;
21+
22+
[SerializeField] private bool applyCurve = false;
23+
[SerializeField] private AnimationCurve curve = AnimationCurve.Linear(0,0,1, 1);
24+
25+
private float[] Generate(int width, int height, uint seed)
26+
{
27+
var offset = falloffOffset;
28+
29+
if (randomizeOffset)
30+
{
31+
var rng = new RandomGeneratorStruct(seed);
32+
offset.y = rng.NextFloatRange(minOffsetY, maxOffsetY);
33+
offset.x = rng.NextFloatRange(minOffsetX, maxOffsetX);
34+
}
35+
36+
var floatMap = new float[width*height];
37+
38+
for (var y = 0; y < height; y++)
39+
{
40+
for (var x = 0; x < width; x++)
41+
{
42+
var i = y * width + x;
43+
floatMap[i] = Noise.GenerateFalloffPoint(x , y , width, height, falloffA, falloffB, offset);
44+
if (applyCurve)
45+
{
46+
floatMap[i] = curve.Evaluate(floatMap[i]);
47+
}
48+
}
49+
}
50+
51+
return floatMap;
52+
}
53+
54+
protected override IWorldMapLayerData GenerateLayer(WorldMapData mapData, int layerSeed)
55+
{
56+
var map = Generate(mapData.width,mapData.height, (uint)layerSeed);
57+
return new FloatMapLayerData
58+
{
59+
FloatMap = map
60+
};
61+
}
62+
}
63+
}

Runtime/WorldMap/Layers/FalloffLayerGenerator.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using UnityEngine;
3+
4+
namespace Gameframe.Procgen
5+
{
6+
[Serializable]
7+
public class FloatMapLayerData : WorldMapLayerData, IFloatMapLayerData
8+
{
9+
[SerializeField]
10+
private float[] floatMap;
11+
public float[] FloatMap
12+
{
13+
get => floatMap;
14+
set => floatMap = value;
15+
}
16+
}
17+
18+
public interface IFloatMapLayerData
19+
{
20+
public float[] FloatMap { get; }
21+
}
22+
}

Runtime/WorldMap/Layers/FloatMapLayerData.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
namespace Gameframe.Procgen
22
{
33
[System.Serializable]
4-
public class HeightMapLayerData : IWorldMapLayerData
4+
public class HeightMapLayerData : WorldMapLayerData
55
{
66
public float[] heightMap;
77
}
8-
}
8+
}

Runtime/WorldMap/Layers/HeightMapLayerGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Gameframe.Procgen
66
{
7-
[CreateAssetMenu(menuName = "Gameframe/Procgen/Layers/HeightMapLayerGenerator")]
7+
[CreateAssetMenu(menuName = "Gameframe/Procgen/Layers/Height Map")]
88
public class HeightMapLayerGenerator : WorldMapLayerGenerator
99
{
1010
public enum MapType
@@ -98,9 +98,9 @@ public HeightMapLayerData Generate(int width, int height, uint seed)
9898
};
9999
}
100100

101-
public override void AddToWorld(WorldMapData worldMapData)
101+
protected override IWorldMapLayerData GenerateLayer(WorldMapData mapData, int layerSeed)
102102
{
103-
worldMapData.layers.Add(Generate(worldMapData.width,worldMapData.height,(uint)worldMapData.seed));
103+
return Generate(mapData.width,mapData.height,(uint)layerSeed);
104104
}
105105

106106
private void OnValidate()

0 commit comments

Comments
 (0)