diff --git a/Assets/Editor/GraphGeneratorUI.cs b/Assets/Editor/GraphGeneratorUI.cs index 4a55bf2..d93aea7 100644 --- a/Assets/Editor/GraphGeneratorUI.cs +++ b/Assets/Editor/GraphGeneratorUI.cs @@ -2,6 +2,7 @@ using System; using Assets; using UnityEditor; using UnityEngine; +using Random = UnityEngine.Random; namespace Editor { @@ -21,6 +22,13 @@ namespace Editor if (GUILayout.Button("Reset")) { generator.Reset(); } + + if (GUILayout.Button("Generate Random")) + { + generator.seed = (int)(Random.value * int.MaxValue); + generator.Reset(); + generator.Generate(); + } if (GUILayout.Button("Generate")) { generator.Generate(); diff --git a/Assets/Map/Location.cs b/Assets/Map/Location.cs deleted file mode 100644 index 51f6298..0000000 --- a/Assets/Map/Location.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Assets.Common; -using Assets.Voronoi; -using UnityEngine; - -namespace Assets.Map -{ - public class Location - { - public readonly Point Center; - public Color Type; - - public Location(Point center) - { - Center = center; - } - } -} \ No newline at end of file diff --git a/Assets/Map/LocationGenerator.cs b/Assets/Map/LocationGenerator.cs index 26c85ba..312a7b6 100644 --- a/Assets/Map/LocationGenerator.cs +++ b/Assets/Map/LocationGenerator.cs @@ -1,5 +1,4 @@ using System; -using System.Collections; using System.Collections.Generic; using UnityEngine; using Random = System.Random; @@ -8,26 +7,26 @@ namespace Assets.Map { public class LocationGenerator { - private Graph _graph; - private readonly List<(int, Color)> _queue = new List<(int, Color)>(); + private Graph _graph; + private readonly List<(int, Location)> _queue = new List<(int, Location)>(); private Random _random; public bool Done => _queue.Count == 0; - public Graph Result => _graph; + public Graph Result => _graph; - public LocationGenerator(Graph graph, Random random = null) + public LocationGenerator(Graph graph, Random random = null) { _graph = graph; _random = random ?? new Random(); } - public void Init(params (int, Color)[] start) + public void Init(params (int, Location)[] start) { - foreach (var pair in start) - _queue.Add(pair); + foreach (var (vertex, location) in start) + SetLocation(vertex, location); } - private (int, Color) Dequeue() + private (int, Location) Dequeue() { while (_queue.Count > 0) { @@ -36,7 +35,7 @@ namespace Assets.Map _queue.RemoveAt(index); - if (_graph.Vertices[pair.Item1].Type == Color.clear) return pair; + if (_graph.Vertices[pair.Item1].Location == null) return pair; } throw new Exception("No more elements."); @@ -46,17 +45,22 @@ namespace Assets.Map { try { - var (vertex, color) = Dequeue(); + var (vertex, location) = Dequeue(); - _graph.Vertices[vertex].Type = color; - - foreach (var neighbour in _graph.Neighbours(vertex)) - if (_graph.Vertices[neighbour].Type == Color.clear) - _queue.Add((neighbour, color)); + SetLocation(vertex, location); } catch (Exception ex) { } } + + private void SetLocation(int vertex, Location location) + { + _graph.Vertices[vertex].Location = location; + + foreach (var neighbour in _graph.Neighbours(vertex)) + if (_graph.Vertices[neighbour].Location == null) + _queue.Add((neighbour, location)); + } } } \ No newline at end of file diff --git a/Assets/Map/LocationSite.cs b/Assets/Map/LocationSite.cs new file mode 100644 index 0000000..3891c43 --- /dev/null +++ b/Assets/Map/LocationSite.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; +using Assets.Common; +using Assets.Voronoi; +using UnityEngine; + +namespace Assets.Map +{ + public class Location + { + public List Sites; + public List BoundaryPoints; + public LocationType Type; + } + + public class LocationSite + { + public readonly Site Site; + public Location Location; + + public LocationSite(Site site) + { + Site = site; + } + } +} \ No newline at end of file diff --git a/Assets/Map/Location.cs.meta b/Assets/Map/LocationSite.cs.meta similarity index 100% rename from Assets/Map/Location.cs.meta rename to Assets/Map/LocationSite.cs.meta diff --git a/Assets/Map/Map.cs b/Assets/Map/Map.cs index 1a32a07..b147e2b 100644 --- a/Assets/Map/Map.cs +++ b/Assets/Map/Map.cs @@ -5,6 +5,6 @@ namespace Assets.Map [Serializable] public class Map { - public readonly Graph Sections = new Graph(); + public readonly Graph Sections = new Graph(); } } \ No newline at end of file diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index dfa711d..349e453 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -248,20 +248,31 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 06dfaa94f8713f12fbe207b61b694c90, type: 3} m_Name: m_EditorClassIdentifier: - seed: 500 + sections: + - name: + color: {r: 1, g: 0, b: 0, a: 1} + - name: + color: {r: 0.18382645, g: 1, b: 0, a: 1} + - name: + color: {r: 1, g: 0, b: 0.74144936, a: 1} + - name: + color: {r: 0, g: 0.06698942, b: 1, a: 1} + - name: + color: {r: 0, g: 1, b: 0.94361734, a: 1} + seed: 120 size: {x: 200, y: 200} debug: enabled: 1 - displayPoints: 1 - displayBeachLine: 1 + displayPoints: 0 + displayBeachLine: 0 displayParabolas: 0 displayVertices: 0 displayNeighbours: 0 displayLabels: 0 displayHalfEdges: 0 - displayEdges: 1 + displayEdges: 0 displayLocations: 1 - radius: 8 + radius: 5 --- !u!33 &528199734 MeshFilter: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/GraphGenerator.cs b/Assets/Scripts/GraphGenerator.cs index 8a1ceeb..1920a43 100644 --- a/Assets/Scripts/GraphGenerator.cs +++ b/Assets/Scripts/GraphGenerator.cs @@ -27,7 +27,7 @@ namespace Assets } [Serializable] - public class Section + public class LocationType { public string name; public Color color; @@ -44,14 +44,16 @@ namespace Assets [RequireComponent(typeof(MeshRenderer), typeof(MeshFilter)), ExecuteInEditMode] public class GraphGenerator : MonoBehaviour { - MeshFilter _filter; private PoissonDiskSampler _sampler; + private Random _random; - List _points = new List(); - public List
sections = new List
(); - private VoronoiGenerator _voronoiGenerator; private LocationGenerator _locationGenerator; + + private List _points = new List(); + private List _locations = new List(); + + public List types = new List(); public VoronoiGenerator VoronoiGenerator => _voronoiGenerator; public LocationGenerator LocationGenerator => _locationGenerator; @@ -59,20 +61,15 @@ namespace Assets public int seed = Environment.TickCount; public Vector2 size = new Vector2(); public GraphGeneratorDebug debug; - public GenerationStage Stage { get; internal set; } = GenerationStage.Start; + public GenerationStage Stage { get; private set; } = GenerationStage.Start; [Range(2.0f, 64.0f)] public float radius = 8; private double _directrix = 0.0f; - private Random _random; - private Graph _locations; - void Start() { - _filter = GetComponent(); - Reset(); } @@ -84,16 +81,20 @@ namespace Assets _sampler.Generator = _random; _points = new List(); - _points.Add(new Vector3(0, -0.1f)); - _points.Add(new Vector3(size.x, 0.1f)); - _points.Add(new Vector3(size.x, size.y - 0.1f)); - _points.Add(new Vector3(0, size.y + 0.1f)); + _points.Add(new Vector3(-size.x/8, size.y / 2 + 0.1f)); + _points.Add(new Vector3(size.x * 1.125f, size.y / 2 - 0.1f)); + _points.Add(new Vector3(size.x / 2 - 0.1f, -size.y/8)); + _points.Add(new Vector3(size.x / 2 + 0.1f, size.y * 1.125f)); + _points.AddRange(_sampler.Generate(size.x, size.y)); Stage = GenerationStage.Voronoi; _voronoiGenerator = new VoronoiGenerator(_points.Select(x => new Point(x.x, x.y)).ToList()); _locationGenerator = null; + + _locations = new List() { new Location() { Type = new LocationType() { color = Color.clear, name = "Ocean" } } }; + _locations.AddRange(types.Select(type => new Location { Type = type })); } public void Generate() @@ -115,10 +116,10 @@ namespace Assets { Stage = GenerationStage.Location; - _locationGenerator = new LocationGenerator(_voronoiGenerator.Delaunay.Morph(p => new Location(p)), _random); + _locationGenerator = new LocationGenerator(_voronoiGenerator.Delaunay.Morph(site => new LocationSite(site)), _random); _locationGenerator.Init( - sections.Select(section => (_random.Next(_voronoiGenerator.Delaunay.Vertices.Count), section.color)) - .Concat(new List<(int, Color)> { (0, Color.white), (1, Color.white), (2, Color.white), (3, Color.white)}) + _locations.Select(location => (_random.Next(_voronoiGenerator.Delaunay.Vertices.Count), location)) + .Concat(new List { 0, 1, 2, 3 }.Select(i => (i, _locations[0]))) // borders should be water .ToArray() ); } @@ -139,13 +140,13 @@ namespace Assets if (debug.displayPoints) { Gizmos.color = Color.white; - DisplayGraphVertices(_voronoiGenerator.Delaunay); + DisplayGraphVertices(_voronoiGenerator.Delaunay.Morph(x => x.Point)); } if (debug.displayNeighbours) { Gizmos.color = Color.yellow; - DisplayGraphEdges(_voronoiGenerator.Delaunay); + DisplayGraphEdges(_voronoiGenerator.Delaunay.Morph(x => x.Point)); } if (debug.displayVertices) @@ -205,10 +206,10 @@ namespace Assets if (debug.displayLocations && LocationGenerator != null) { - foreach (var location in LocationGenerator.Result.Vertices.Where(l => l.Type != Color.white)) + foreach (var location in LocationGenerator.Result.Vertices.Where(l => l.Location != null && l.Location != _locations[0])) { - Gizmos.color = location.Type; - Gizmos.DrawSphere(location.Center.ToVector3(), 2); + Gizmos.color = location.Location.Type.color; + Gizmos.DrawSphere(location.Site.Point.ToVector3(), 2); } } } diff --git a/Assets/Voronoi/VoronoiGenerator.cs b/Assets/Voronoi/VoronoiGenerator.cs index d85c5ac..709fc4b 100644 --- a/Assets/Voronoi/VoronoiGenerator.cs +++ b/Assets/Voronoi/VoronoiGenerator.cs @@ -50,7 +50,7 @@ namespace Assets.Voronoi public IPriorityQueue Queue { get; internal set; } public Graph Voronoi; - public Graph Delaunay; + public Graph Delaunay; public List HalfEdges; @@ -72,10 +72,10 @@ namespace Assets.Voronoi Line = new BeachLine(); Voronoi = new Graph(); - Delaunay = new Graph(); + Delaunay = new Graph(); HalfEdges = new List(); - Delaunay.Vertices.AddRange(from site in Sites select site.Point); + Delaunay.Vertices.AddRange(Sites); foreach (var site in Sites) {