From 085ab2d62d3206999680e756f1b274ae5243ec65 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 10 Nov 2019 13:34:01 +0100 Subject: [PATCH] [otwarty teren] wydzielenie generowania ze skryptow unity part 3 --- ...{UnityPointExtensions.cs => Extensions.cs} | 0 ...tExtensions.cs.meta => Extensions.cs.meta} | 0 Assets/Common/Map.cs | 4 +- Assets/Common/MapSite.cs | 12 +++- Assets/Scripts/AnnotationPass.meta | 3 + .../Scripts/AnnotationPass/LocationsPass.cs | 12 ++++ .../AnnotationPass/LocationsPass.cs.meta | 3 + Assets/Scripts/IAnnotationPass.cs | 9 +++ Assets/Scripts/IAnnotationPass.cs.meta | 3 + Assets/Scripts/IGridGenerator.cs | 9 +++ Assets/Scripts/IGridGenerator.cs.meta | 3 + Assets/Scripts/IRenderPass.cs | 10 ++++ Assets/Scripts/IRenderPass.cs.meta | 3 + Assets/Scripts/LandmassGenerator.cs | 33 +++++------ Assets/Scripts/MapGenerator.cs | 40 +++++++++++++ Assets/Scripts/MapGenerator.cs.meta | 3 + .../PointDistribution/IPointDistribution.cs | 2 +- .../PointDistribution/PoissonDiskSampler.cs | 8 +-- .../PointDistribution/RandomDistribution.cs | 24 ++++++++ .../RandomDistribution.cs.meta | 3 + .../PointDistribution/RegularDistribution.cs | 26 ++++++++ .../RegularDistribution.cs.meta | 3 + Assets/Scripts/VoronoiGridGenerator.cs | 59 +++++++++++++++++++ Assets/Scripts/VoronoiGridGenerator.cs.meta | 3 + 24 files changed, 250 insertions(+), 25 deletions(-) rename Assets/Common/{UnityPointExtensions.cs => Extensions.cs} (100%) rename Assets/Common/{UnityPointExtensions.cs.meta => Extensions.cs.meta} (100%) create mode 100644 Assets/Scripts/AnnotationPass.meta create mode 100644 Assets/Scripts/AnnotationPass/LocationsPass.cs create mode 100644 Assets/Scripts/AnnotationPass/LocationsPass.cs.meta create mode 100644 Assets/Scripts/IAnnotationPass.cs create mode 100644 Assets/Scripts/IAnnotationPass.cs.meta create mode 100644 Assets/Scripts/IGridGenerator.cs create mode 100644 Assets/Scripts/IGridGenerator.cs.meta create mode 100644 Assets/Scripts/IRenderPass.cs create mode 100644 Assets/Scripts/IRenderPass.cs.meta create mode 100644 Assets/Scripts/MapGenerator.cs create mode 100644 Assets/Scripts/MapGenerator.cs.meta create mode 100644 Assets/Scripts/PointDistribution/RandomDistribution.cs create mode 100644 Assets/Scripts/PointDistribution/RandomDistribution.cs.meta create mode 100644 Assets/Scripts/PointDistribution/RegularDistribution.cs create mode 100644 Assets/Scripts/PointDistribution/RegularDistribution.cs.meta create mode 100644 Assets/Scripts/VoronoiGridGenerator.cs create mode 100644 Assets/Scripts/VoronoiGridGenerator.cs.meta diff --git a/Assets/Common/UnityPointExtensions.cs b/Assets/Common/Extensions.cs similarity index 100% rename from Assets/Common/UnityPointExtensions.cs rename to Assets/Common/Extensions.cs diff --git a/Assets/Common/UnityPointExtensions.cs.meta b/Assets/Common/Extensions.cs.meta similarity index 100% rename from Assets/Common/UnityPointExtensions.cs.meta rename to Assets/Common/Extensions.cs.meta diff --git a/Assets/Common/Map.cs b/Assets/Common/Map.cs index fd3f0e1..85a8004 100644 --- a/Assets/Common/Map.cs +++ b/Assets/Common/Map.cs @@ -21,8 +21,8 @@ namespace Assets.Common } } - public Mesh Mesh { get; set; } - + public Graph Boundaries { get; set; } + public readonly Metadata Metadata = new Metadata(); public Map(int seed) diff --git a/Assets/Common/MapSite.cs b/Assets/Common/MapSite.cs index 403dffc..8f9c359 100644 --- a/Assets/Common/MapSite.cs +++ b/Assets/Common/MapSite.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; namespace Assets.Common { @@ -6,7 +7,16 @@ namespace Assets.Common public class MapSite { public readonly Metadata Metadata = new Metadata(); - + + public MapSite(Point center, IEnumerable boundary) + { + Center = center; + Boundary = boundary; + } + + public Point Center { get; set; } + public IEnumerable Boundary { get; set; } + [field: NonSerialized] public Map Map { get; internal set; } } diff --git a/Assets/Scripts/AnnotationPass.meta b/Assets/Scripts/AnnotationPass.meta new file mode 100644 index 0000000..3311740 --- /dev/null +++ b/Assets/Scripts/AnnotationPass.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 32fcc0d2a1b24d48b9525945a88ec3a2 +timeCreated: 1573389130 \ No newline at end of file diff --git a/Assets/Scripts/AnnotationPass/LocationsPass.cs b/Assets/Scripts/AnnotationPass/LocationsPass.cs new file mode 100644 index 0000000..273994e --- /dev/null +++ b/Assets/Scripts/AnnotationPass/LocationsPass.cs @@ -0,0 +1,12 @@ +using Assets.Common; + +namespace Assets.AnnotationPass +{ + public class LocationsPass : IAnnotationPass + { + public void Annotate(Map map) + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/AnnotationPass/LocationsPass.cs.meta b/Assets/Scripts/AnnotationPass/LocationsPass.cs.meta new file mode 100644 index 0000000..fe10e0e --- /dev/null +++ b/Assets/Scripts/AnnotationPass/LocationsPass.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: dbe0bf7461994dc6b4754a5d7927bc04 +timeCreated: 1573389159 \ No newline at end of file diff --git a/Assets/Scripts/IAnnotationPass.cs b/Assets/Scripts/IAnnotationPass.cs new file mode 100644 index 0000000..0df85e7 --- /dev/null +++ b/Assets/Scripts/IAnnotationPass.cs @@ -0,0 +1,9 @@ +using Assets.Common; + +namespace Assets +{ + public interface IAnnotationPass + { + void Annotate(Map map); + } +} \ No newline at end of file diff --git a/Assets/Scripts/IAnnotationPass.cs.meta b/Assets/Scripts/IAnnotationPass.cs.meta new file mode 100644 index 0000000..baf5e33 --- /dev/null +++ b/Assets/Scripts/IAnnotationPass.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f29f3a8ef63c47e4acb0f0fe114e7cda +timeCreated: 1573323034 \ No newline at end of file diff --git a/Assets/Scripts/IGridGenerator.cs b/Assets/Scripts/IGridGenerator.cs new file mode 100644 index 0000000..63621ef --- /dev/null +++ b/Assets/Scripts/IGridGenerator.cs @@ -0,0 +1,9 @@ +using Assets.Common; + +namespace Assets +{ + public interface IGridGenerator + { + Map CreateGrid(double width, double height); + } +} \ No newline at end of file diff --git a/Assets/Scripts/IGridGenerator.cs.meta b/Assets/Scripts/IGridGenerator.cs.meta new file mode 100644 index 0000000..14ca5e5 --- /dev/null +++ b/Assets/Scripts/IGridGenerator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: bbbede9954254e208a6e9701f51a6d9b +timeCreated: 1573323655 \ No newline at end of file diff --git a/Assets/Scripts/IRenderPass.cs b/Assets/Scripts/IRenderPass.cs new file mode 100644 index 0000000..eb22532 --- /dev/null +++ b/Assets/Scripts/IRenderPass.cs @@ -0,0 +1,10 @@ +using Assets.Common; +using UnityEngine; + +namespace Assets +{ + public interface IRenderPass + { + void Render(Map map, Component component); + } +} \ No newline at end of file diff --git a/Assets/Scripts/IRenderPass.cs.meta b/Assets/Scripts/IRenderPass.cs.meta new file mode 100644 index 0000000..8b05702 --- /dev/null +++ b/Assets/Scripts/IRenderPass.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f18534dae89f40afa4fceb93555d1d4f +timeCreated: 1573323220 \ No newline at end of file diff --git a/Assets/Scripts/LandmassGenerator.cs b/Assets/Scripts/LandmassGenerator.cs index f82df23..f040606 100644 --- a/Assets/Scripts/LandmassGenerator.cs +++ b/Assets/Scripts/LandmassGenerator.cs @@ -51,7 +51,7 @@ namespace Assets [ExecuteInEditMode] public class LandmassGenerator : MonoBehaviour { - private PoissonDiskSampler _sampler; + private IPointDistribution _sampler; private Random _random; private VoronoiGenerator _voronoiGenerator; @@ -83,26 +83,17 @@ namespace Assets void Start() { Reset(); - } + } public void Reset() { - _random = new Random(seed); + _random = new Random(seed); + _sampler = new PoissonDiskSampler(radius) { Generator = _random }; + _points = new List(); - _sampler = new PoissonDiskSampler(radius); - _sampler.Generator = _random; - - _points = new List(); - _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).Select(x => new Vector3((float)x.x, (float)x.y))); - Stage = GenerationStage.Voronoi; - - _voronoiGenerator = new VoronoiGenerator(_points.Select(x => new Point(x.x, x.y)).ToList()); + + _voronoiGenerator = null; _locationGenerator = null; _locationGraph = null; @@ -113,11 +104,19 @@ namespace Assets public void Generate() { var stopwatch = new Stopwatch(); - stopwatch.Start(); debug.generationTime = 0; + _points = new List(); + _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).Select(x => new Vector3((float)x.x, (float)x.y))); + + while (Stage != GenerationStage.Done) { Step(); diff --git a/Assets/Scripts/MapGenerator.cs b/Assets/Scripts/MapGenerator.cs new file mode 100644 index 0000000..1817344 --- /dev/null +++ b/Assets/Scripts/MapGenerator.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Assets.Common; + +namespace Assets +{ + public class MapGenerator + { + private IGridGenerator _gridGenerator; + private ICollection _annotationPasses = new List(); + private Map _result; + + public IEnumerable AnnotationPasses => _annotationPasses.AsEnumerable(); + public Map Result => _result; + public bool IsDone => _result != null; + + public MapGenerator(IGridGenerator gridGenerator) + { + _gridGenerator = gridGenerator; + } + + public void AddAnnotationPass(IAnnotationPass pass) + { + _annotationPasses.Add(pass); + } + + public Map Generate(double width, double height) + { + if (_result != null) + return _result; + + _result = _gridGenerator.CreateGrid(width, height); + foreach (var pass in _annotationPasses) + pass.Annotate(_result); + + return _result; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/MapGenerator.cs.meta b/Assets/Scripts/MapGenerator.cs.meta new file mode 100644 index 0000000..edfb9fb --- /dev/null +++ b/Assets/Scripts/MapGenerator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fcd81ef2365c437e82657a21efe03131 +timeCreated: 1573387486 \ No newline at end of file diff --git a/Assets/Scripts/PointDistribution/IPointDistribution.cs b/Assets/Scripts/PointDistribution/IPointDistribution.cs index 4e2309b..d9d0bc3 100644 --- a/Assets/Scripts/PointDistribution/IPointDistribution.cs +++ b/Assets/Scripts/PointDistribution/IPointDistribution.cs @@ -5,6 +5,6 @@ namespace Assets.PointDistribution { public interface IPointDistribution { - IEnumerable Generate(float width, float height); + IEnumerable Generate(double width, double height); } } \ No newline at end of file diff --git a/Assets/Scripts/PointDistribution/PoissonDiskSampler.cs b/Assets/Scripts/PointDistribution/PoissonDiskSampler.cs index c424504..2daef2a 100644 --- a/Assets/Scripts/PointDistribution/PoissonDiskSampler.cs +++ b/Assets/Scripts/PointDistribution/PoissonDiskSampler.cs @@ -13,9 +13,9 @@ namespace Assets.PointDistribution private int k; private float r; - private float Random(float max, float min = 0) + private double Random(double max, double min = 0) { - return (float)Generator.NextDouble() * (max - min) + min; + return Generator.NextDouble() * (max - min) + min; } public PoissonDiskSampler(float r, int k = 30) @@ -24,7 +24,7 @@ namespace Assets.PointDistribution this.r = r; } - public IEnumerable Generate(float width, float height) + public IEnumerable Generate(double width, double height) { var size = r / Mathf.Sqrt(2); @@ -79,7 +79,7 @@ namespace Assets.PointDistribution while (active.Count > 0 && watchdog-- > 0) { - var current = Mathf.FloorToInt(Random(active.Count)); + var current = (int)Math.Floor(Random(active.Count)); var point = active[current]; var i = 0; diff --git a/Assets/Scripts/PointDistribution/RandomDistribution.cs b/Assets/Scripts/PointDistribution/RandomDistribution.cs new file mode 100644 index 0000000..959a6cb --- /dev/null +++ b/Assets/Scripts/PointDistribution/RandomDistribution.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using Assets.Common; + +namespace Assets.PointDistribution +{ + public class RandomDistribution : IPointDistribution + { + public readonly int N; + public readonly Random Random; + + public RandomDistribution(int n, Random random) + { + Random = random ?? new Random(); + N = n; + } + + public IEnumerable Generate(double width, double height) + { + for (int x = 0; x < N; x++) + yield return new Point(Random.NextDouble() * width, Random.NextDouble()); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/PointDistribution/RandomDistribution.cs.meta b/Assets/Scripts/PointDistribution/RandomDistribution.cs.meta new file mode 100644 index 0000000..48f8c22 --- /dev/null +++ b/Assets/Scripts/PointDistribution/RandomDistribution.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6f13b49c7b224b778fdc727d6edfc0a6 +timeCreated: 1573309049 \ No newline at end of file diff --git a/Assets/Scripts/PointDistribution/RegularDistribution.cs b/Assets/Scripts/PointDistribution/RegularDistribution.cs new file mode 100644 index 0000000..e224dff --- /dev/null +++ b/Assets/Scripts/PointDistribution/RegularDistribution.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using Assets.Common; + +namespace Assets.PointDistribution +{ + public class RegularDistribution : IPointDistribution + { + public readonly float Radius; + + public RegularDistribution(float radius) + { + Radius = radius; + } + + public IEnumerable Generate(double width, double height) + { + int xMax = (int)Math.Ceiling(width / Radius); + int yMax = (int)Math.Ceiling(height / Radius); + + for (int x = 0; x < xMax + 1; x++) + for (int y = 0; y < yMax + 1; y++) + yield return new Point(x * width / xMax, y * height / yMax); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/PointDistribution/RegularDistribution.cs.meta b/Assets/Scripts/PointDistribution/RegularDistribution.cs.meta new file mode 100644 index 0000000..1208821 --- /dev/null +++ b/Assets/Scripts/PointDistribution/RegularDistribution.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ed0e523b34b34c5e8b57af43b0ca65cb +timeCreated: 1573308219 \ No newline at end of file diff --git a/Assets/Scripts/VoronoiGridGenerator.cs b/Assets/Scripts/VoronoiGridGenerator.cs new file mode 100644 index 0000000..22ea4a6 --- /dev/null +++ b/Assets/Scripts/VoronoiGridGenerator.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using Assets.Common; +using Assets.PointDistribution; +using Assets.Voronoi; + +namespace Assets +{ + public class VoronoiGridGenerator : IGridGenerator + { + public const string VoronoiSiteProperty = "VoronoiSite"; + + private readonly int _seed; + private readonly IPointDistribution _sampler; + + public VoronoiGridGenerator(int seed, IPointDistribution sampler) + { + _seed = seed; + _sampler = sampler; + } + + public Map CreateGrid(double width, double height) + { + Map result = new Map(_seed); + + var points = GeneratePoints(width, height); + var generator = new VoronoiGenerator(points); + + generator.Generate(); + + result.Boundaries = (Graph)generator.Voronoi.Clone(); + result.Sites = generator.Delaunay.Morph(s => + { + var site = new MapSite(s.Point, new List()); + + site.Metadata.SetProperty(VoronoiSiteProperty, s); + + return site; + }); + + return result; + } + + private List GeneratePoints(double width, double height) + { + var points = new List + { + new Point(-width / 8, height / 2 + 0.1f), + new Point(width * 1.125f, height / 2 - 0.1f), + new Point(width / 2 - 0.1f, -height / 8), + new Point(width / 2 + 0.1f, height * 1.125f) + }; + + points.AddRange(_sampler.Generate(width, height)); + + return points; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/VoronoiGridGenerator.cs.meta b/Assets/Scripts/VoronoiGridGenerator.cs.meta new file mode 100644 index 0000000..cf166ab --- /dev/null +++ b/Assets/Scripts/VoronoiGridGenerator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: e233e6fb732c448bbbbc9980e8e67b80 +timeCreated: 1573324130 \ No newline at end of file