Skip to content

Commit 96e42c2

Browse files
committed
Starting hex mesh view
1 parent 5479886 commit 96e42c2

File tree

9 files changed

+528
-38
lines changed

9 files changed

+528
-38
lines changed

Runtime/Utility/VoxelMeshUtility.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,42 +31,42 @@ public static VoxelMeshData CreateMeshData(float[,] noiseMap, int chunkX, int ch
3131
var right = mapX + 1 < width ? terrainMap[mapX + 1, mapY] : null;
3232
var left = mapX - 1 >= 0 ? terrainMap[mapX - 1, mapY] : null;
3333

34-
meshData.AddUpQuad(x, y, terrain.Elevation);
34+
meshData.AddUpQuad(x, y, terrain.MinElevation);
3535

36-
if (left != null && left.Elevation < terrain.Elevation)
36+
if (left != null && left.MinElevation < terrain.MinElevation)
3737
{
38-
meshData.AddLeftQuad(x, y, terrain.Elevation, terrain.Elevation - left.Elevation);
38+
meshData.AddLeftQuad(x, y, terrain.MinElevation, terrain.MinElevation - left.MinElevation);
3939
}
4040
else if (edges && mapX - 1 < 0)
4141
{
42-
meshData.AddLeftQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
42+
meshData.AddLeftQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
4343
}
4444

45-
if (front != null && front.Elevation < terrain.Elevation)
45+
if (front != null && front.MinElevation < terrain.MinElevation)
4646
{
47-
meshData.AddFrontQuad(x, y, terrain.Elevation, terrain.Elevation - front.Elevation);
47+
meshData.AddFrontQuad(x, y, terrain.MinElevation, terrain.MinElevation - front.MinElevation);
4848
}
4949
else if (edges && mapY + 1 >= height)
5050
{
51-
meshData.AddFrontQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
51+
meshData.AddFrontQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
5252
}
5353

54-
if (right != null && right.Elevation < terrain.Elevation)
54+
if (right != null && right.MinElevation < terrain.MinElevation)
5555
{
56-
meshData.AddRightQuad(x, y, terrain.Elevation, terrain.Elevation - right.Elevation);
56+
meshData.AddRightQuad(x, y, terrain.MinElevation, terrain.MinElevation - right.MinElevation);
5757
}
5858
else if (edges && mapX + 1 >= width)
5959
{
60-
meshData.AddRightQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
60+
meshData.AddRightQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
6161
}
6262

63-
if (back != null && back.Elevation < terrain.Elevation)
63+
if (back != null && back.MinElevation < terrain.MinElevation)
6464
{
65-
meshData.AddBackQuad(x, y, terrain.Elevation, terrain.Elevation - back.Elevation);
65+
meshData.AddBackQuad(x, y, terrain.MinElevation, terrain.MinElevation - back.MinElevation);
6666
}
6767
else if (edges && mapY - 1 < 0)
6868
{
69-
meshData.AddBackQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
69+
meshData.AddBackQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
7070
}
7171
}
7272
}
@@ -98,42 +98,42 @@ public static VoxelMeshData CreateMeshData(float[] heightMap, int width, int hei
9898
var right = mapX + 1 < width ? terrainMap[mapY * width + mapX + 1] : null;
9999
var left = mapX - 1 >= 0 ? terrainMap[mapY * width + mapX - 1] : null;
100100

101-
meshData.AddUpQuad(x, y, terrain.Elevation);
101+
meshData.AddUpQuad(x, y, terrain.MinElevation);
102102

103-
if (left != null && left.Elevation < terrain.Elevation)
103+
if (left != null && left.MinElevation < terrain.MinElevation)
104104
{
105-
meshData.AddLeftQuad(x, y, terrain.Elevation, terrain.Elevation - left.Elevation);
105+
meshData.AddLeftQuad(x, y, terrain.MinElevation, terrain.MinElevation - left.MinElevation);
106106
}
107107
else if (edges && mapX - 1 < 0)
108108
{
109-
meshData.AddLeftQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
109+
meshData.AddLeftQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
110110
}
111111

112-
if (front != null && front.Elevation < terrain.Elevation)
112+
if (front != null && front.MinElevation < terrain.MinElevation)
113113
{
114-
meshData.AddFrontQuad(x, y, terrain.Elevation, terrain.Elevation - front.Elevation);
114+
meshData.AddFrontQuad(x, y, terrain.MinElevation, terrain.MinElevation - front.MinElevation);
115115
}
116116
else if (edges && mapY + 1 >= height)
117117
{
118-
meshData.AddFrontQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
118+
meshData.AddFrontQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
119119
}
120120

121-
if (right != null && right.Elevation < terrain.Elevation)
121+
if (right != null && right.MinElevation < terrain.MinElevation)
122122
{
123-
meshData.AddRightQuad(x, y, terrain.Elevation, terrain.Elevation - right.Elevation);
123+
meshData.AddRightQuad(x, y, terrain.MinElevation, terrain.MinElevation - right.MinElevation);
124124
}
125125
else if (edges && mapX + 1 >= width)
126126
{
127-
meshData.AddRightQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
127+
meshData.AddRightQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
128128
}
129129

130-
if (back != null && back.Elevation < terrain.Elevation)
130+
if (back != null && back.MinElevation < terrain.MinElevation)
131131
{
132-
meshData.AddBackQuad(x, y, terrain.Elevation, terrain.Elevation - back.Elevation);
132+
meshData.AddBackQuad(x, y, terrain.MinElevation, terrain.MinElevation - back.MinElevation);
133133
}
134134
else if (edges && mapY - 1 < 0)
135135
{
136-
meshData.AddBackQuad(x, y, terrain.Elevation, terrain.Elevation + edgeThickness);
136+
meshData.AddBackQuad(x, y, terrain.MinElevation, terrain.MinElevation + edgeThickness);
137137
}
138138
}
139139
}

Runtime/WorldMap/Layers/HeightMapLayerGenerator.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public enum MapType
1212
Falloff,
1313
NoiseWithFalloff
1414
}
15+
16+
[SerializeField] private bool step = false;
17+
18+
[SerializeField] private int stepCount = 10;
1519

1620
[SerializeField] private Vector2 offset = Vector2.zero;
1721

@@ -56,7 +60,14 @@ public HeightMapLayerData Generate(int width, int height, int seed)
5660
{
5761
for (int x = 0; x < width; x++)
5862
{
59-
heightMap[y * width + x] = noiseMap[x, y];
63+
var val = noiseMap[x, y];
64+
65+
if (step)
66+
{
67+
val = Mathf.RoundToInt(val * stepCount) / (float)stepCount;
68+
}
69+
70+
heightMap[y * width + x] = val;
6071
}
6172
}
6273

Runtime/WorldMap/Terrain/TerrainTable.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ private void OnValidate()
196196
{
197197
regions[i].Floor = regions[i - 1].Threshold;
198198
}
199+
200+
if (i > 0 && regions[i].MinElevation < regions[i - 1].MaxElevation)
201+
{
202+
regions[i].MinElevation = regions[i - 1].MaxElevation;
203+
}
204+
205+
if (regions[i].MaxElevation < regions[i].MinElevation)
206+
{
207+
regions[i].MaxElevation = regions[i].MinElevation;
208+
}
199209
}
200210
}
201211

Runtime/WorldMap/Terrain/TerrainType.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ public float Threshold
3434
set => _threshold = value;
3535
}
3636

37-
[FormerlySerializedAs("_meshHeight")] [SerializeField]
38-
private float _elevation;
39-
public float Elevation => _elevation;
40-
41-
/*[SerializeField] private Color _lowColor = Color.black;
42-
public Color LowColor => _lowColor;
37+
[FormerlySerializedAs("_elevation")] [SerializeField]
38+
private float _minElevation;
39+
public float MinElevation
40+
{
41+
get => _minElevation;
42+
set => _minElevation = value;
43+
}
4344

44-
[FormerlySerializedAs("_color")] [SerializeField]
45-
private Color _highColor = Color.white;
46-
public Color HighColor => _highColor;*/
45+
[SerializeField]
46+
private float _maxElevation;
47+
public float MaxElevation
48+
{
49+
get => _maxElevation;
50+
set => _maxElevation = value;
51+
}
4752

4853
[SerializeField]
4954
private Gradient gradient;

Runtime/WorldMap/Views/WoldMapTerrainMeshView.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public class WoldMapTerrainMeshView : MonoBehaviour, IWorldMapView
1414
[SerializeField] private float heightScale = 1;
1515

1616
[SerializeField] private TerrainTable terrainTable = null;
17+
18+
[SerializeField] private bool smooth = false;
19+
20+
[SerializeField] private bool useColorGradient = false;
1721

1822
//This is here just to get the enabled checkbox in editor
1923
private void Start()
@@ -36,11 +40,30 @@ public void DisplayMap(WorldMapData worldMapData)
3640
else
3741
{
3842
var meshData = TerrainMeshUtility.GenerateMesh(heightMap,worldMapData.width,worldMapData.height,levelOfDetail,
39-
x => terrainTable.GetTerrainType(x).Elevation * heightScale,
40-
x => terrainTable.GetTerrainType(x).ColorGradient.Evaluate(1));
43+
x =>
44+
{
45+
var terrainType = terrainTable.GetTerrainType(x);
46+
if (smooth)
47+
{
48+
var t = Mathf.InverseLerp(terrainType.Floor, terrainType.Threshold, x);
49+
return Mathf.Lerp(terrainType.MinElevation, terrainType.MaxElevation, t) * heightScale;
50+
}
51+
return terrainTable.GetTerrainType(x).MinElevation * heightScale;
52+
},
53+
x =>
54+
{
55+
var terrainType = terrainTable.GetTerrainType(x);
56+
if (!useColorGradient)
57+
{
58+
return terrainType.ColorGradient.Evaluate(0);
59+
}
60+
var t = Mathf.InverseLerp(terrainType.Floor, terrainType.Threshold, x);
61+
return terrainType.ColorGradient.Evaluate(t);
62+
});
4163
_meshFilter.mesh = meshData.CreateMesh();
4264
}
4365

4466
}
67+
4568
}
4669
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using ICSharpCode.NRefactory.Ast;
4+
using UnityEngine;
5+
using UnityEngine.Serialization;
6+
7+
namespace Gameframe.Procgen
8+
{
9+
public class WorldMapBorderView : MonoBehaviour, IWorldMapView
10+
{
11+
[SerializeField]
12+
private LineRenderer prefab = null;
13+
14+
[SerializeField]
15+
private Vector3 offset;
16+
17+
[SerializeField]
18+
private Color[] regionColors = new Color[0];
19+
20+
[FormerlySerializedAs("lines")] [SerializeField]
21+
private List<LineRenderer> lineRenderers = new List<LineRenderer>();
22+
23+
[SerializeField] private int minLineLength = 2;
24+
25+
private void Start()
26+
{
27+
if (prefab != null)
28+
{
29+
prefab.gameObject.SetActive(false);
30+
}
31+
}
32+
33+
[ContextMenu("Clear Lines")]
34+
public void ClearLines()
35+
{
36+
foreach (var line in lineRenderers)
37+
{
38+
if (Application.isPlaying)
39+
{
40+
Destroy(line.gameObject);
41+
}
42+
else
43+
{
44+
DestroyImmediate(line.gameObject);
45+
46+
}
47+
}
48+
lineRenderers.Clear();
49+
}
50+
51+
public void DisplayMap(WorldMapData mapData)
52+
{
53+
if (!enabled || prefab == null)
54+
{
55+
return;
56+
}
57+
58+
prefab.gameObject.SetActive(false);
59+
60+
ClearLines();
61+
62+
var positionOffset = new Vector3(mapData.width*-0.5f,0, mapData.height*0.5f) + offset;
63+
64+
var regionLayer = mapData.GetLayer<RegionMapLayerData>();
65+
foreach (var region in regionLayer.regions)
66+
{
67+
//Each region could have more than one border line to draw
68+
//Border point list is unordered so we need to also figure out what the lines are
69+
70+
//Convert border points to 3D space
71+
var points = new List<Vector3>(region.borderPoints.Select((pt) => new Vector3(pt.x,0,-pt.y) + positionOffset));
72+
73+
//Extract lines from list of points by getting closest points
74+
var lines = new List<List<Vector3>>();
75+
var currentLine = new List<Vector3>();
76+
lines.Add(currentLine);
77+
var currentPt = points[0];
78+
points.RemoveAt(0);
79+
while (points.Count > 0)
80+
{
81+
float minDistance = float.MaxValue;
82+
int minIndex = -1;
83+
84+
for (int i = 0; i < points.Count; i++)
85+
{
86+
var distance = (currentPt - points[i]).sqrMagnitude;
87+
if (distance < minDistance)
88+
{
89+
minDistance = distance;
90+
minIndex = i;
91+
}
92+
}
93+
94+
95+
if (minDistance > 2)
96+
{
97+
//Start a new line if the closest points was more than a certain distance away
98+
lines.Add(currentLine);
99+
currentLine = new List<Vector3>();
100+
}
101+
102+
currentPt = points[minIndex];
103+
currentLine.Add(points[minIndex]);
104+
points.RemoveAt(minIndex);
105+
}
106+
107+
var regionColor = regionColors.Length > 0 ? regionColors[(region.id - 1) % regionColors.Length] : Color.white;
108+
109+
foreach (var line in lines)
110+
{
111+
//Don't draw anything with less than 2 points
112+
if (line.Count < minLineLength)
113+
{
114+
continue;
115+
}
116+
117+
var lineRenderer = Instantiate(prefab, transform);
118+
lineRenderer.transform.localEulerAngles = new Vector3(90,0,0);
119+
lineRenderer.startColor = regionColor;
120+
lineRenderer.endColor = regionColor;
121+
122+
//lineRenderer.positionCount = region.borderPoints.Count;
123+
lineRenderer.positionCount = line.Count;
124+
lineRenderer.SetPositions(line.ToArray());
125+
126+
var startPt = line[0];
127+
var endPt = line[line.Count - 1];
128+
//If the end is close enough to the start then complete the loop
129+
if ((startPt - endPt).sqrMagnitude <= 2)
130+
{
131+
lineRenderer.loop = true;
132+
}
133+
134+
lineRenderer.gameObject.SetActive(true);
135+
lineRenderers.Add(lineRenderer);
136+
}
137+
138+
}
139+
}
140+
}
141+
}
142+

Runtime/WorldMap/Views/WorldMapBorderView.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.

0 commit comments

Comments
 (0)