[otwarty teren] wydzielenie generowania ze skryptow unity part 2

This commit is contained in:
Kacper Donat 2019-11-09 14:58:38 +01:00
parent 7827f6cc21
commit 3c3cbc995b
35 changed files with 93 additions and 180 deletions

View File

@ -1,9 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.Voronoi;
namespace Assets.Map
namespace Assets.Common
{
public class Location
{
@ -37,15 +36,4 @@ namespace Assets.Map
BoundaryEdges = a.Union(b).Except(a.Intersect(b)).ToList();
}
}
public class LocationSite
{
public readonly Site Site;
public Location Location;
public LocationSite(Site site)
{
Site = site;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: def8cfa113744a3bb21d99fddf782916
timeCreated: 1573304795

View File

@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.Voronoi;
using Random = System.Random;
namespace Assets.Map
namespace Assets.Common
{
public class LocationGenerator
{

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1a667fae02f346199ad742a83f9d8610
timeCreated: 1573304795

View File

@ -0,0 +1,15 @@
using Assets.Voronoi;
namespace Assets.Common
{
public class LocationSite
{
public readonly Site Site;
public Location Location;
public LocationSite(Site site)
{
Site = site;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ae1606ff46e64c2a9dc080c61bbd74b8
timeCreated: 1573304795

View File

@ -1,8 +1,7 @@
using System;
using Assets.Common;
using UnityEngine;
namespace Assets.Map
namespace Assets.Common
{
[Serializable]
public class Map
@ -31,13 +30,4 @@ namespace Assets.Map
Seed = seed;
}
}
[Serializable]
public class MapSite
{
public readonly Metadata Metadata = new Metadata();
[field: NonSerialized]
public Map Map { get; internal set; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 087a8dc14fe944dfa2bef7a5fbaefc3d
timeCreated: 1573304795

13
Assets/Common/MapSite.cs Normal file
View File

@ -0,0 +1,13 @@
using System;
namespace Assets.Common
{
[Serializable]
public class MapSite
{
public readonly Metadata Metadata = new Metadata();
[field: NonSerialized]
public Map Map { get; internal set; }
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1bff83ade11b42e49fce3ba4f779b5d2
timeCreated: 1573304795

View File

@ -6,7 +6,7 @@ using Random = UnityEngine.Random;
namespace Editor
{
[CustomEditor(typeof (GraphGenerator))]
[CustomEditor(typeof (LandmassGenerator))]
public class GraphGeneratorUI : UnityEditor.Editor
{
private string _threshold;
@ -14,7 +14,7 @@ namespace Editor
public override void OnInspectorGUI()
{
GraphGenerator generator = (GraphGenerator)target;
LandmassGenerator generator = (LandmassGenerator)target;
_showInfo = EditorGUILayout.Foldout(_showInfo, "Debug info");
@ -65,7 +65,7 @@ namespace Editor
private void OnSceneGUI()
{
GraphGenerator generator = (GraphGenerator)target;
LandmassGenerator generator = (LandmassGenerator)target;
if (generator.VoronoiGenerator != null)
{

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 259153a10cc9545dbb826a5a4c315254
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,9 +0,0 @@
using UnityEngine;
namespace Generators
{
public interface IGenerator
{
double GetValue(Vector2 position);
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 682b56d4654bea3de8b383036947492f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,29 +0,0 @@
using UnityEngine;
namespace Generators
{
public class IslandGenerator : IGenerator
{
private IGenerator noise;
public float Inner { get; set; } = 20;
public float Outer { get; set; } = 23;
public float Threshold { get; set; } = .5f;
public IslandGenerator(IGenerator noise)
{
this.noise = noise;
}
public double GetValue(Vector2 position)
{
var distance = position.magnitude / 10;
distance = (distance - Inner) / (Outer - Inner);
var result = noise.GetValue(position) + .75 * (Mathf.Clamp(0, distance, 1) - 1);
return result > Threshold ? 1 : 0;
}
}
}

View File

@ -1,36 +0,0 @@
using UnityEngine;
namespace Generators
{
public class PerlinNoiseGenerator : IGenerator
{
public float Scale { get; set; } = 0.001f;
public Vector2 Offset { get; set; } = new Vector2(.0f, .0f);
public int Iterations { get; set; } = 8;
public float Decay { get; set; } = .7f;
public float Lacunarity { get; set; } = 1.71f;
public double GetValue(Vector2 position)
{
var amplitude = 1.0;
var scale = Scale;
var result = 0.0;
for (int i = 0; i < Iterations; i++) {
result += amplitude * Calculate(position, scale);
amplitude *= Decay;
scale *= Lacunarity;
}
return result / ((1 - System.Math.Pow(Decay, Iterations)) / (1 - Decay));
}
private double Calculate(Vector2 position, float scale)
{
var final = (position + Offset) * scale;
return Mathf.PerlinNoise(final.x, final.y);
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 80853afcc172aecd3bc2885e9c1f4705
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: b2de5bc0563442539a60e5a00687d27d
timeCreated: 1564082962

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: c1829eb593684199a5177fd08c07a753
timeCreated: 1564083223

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 7a2d1705444344bfa99efc3c210db6f4
timeCreated: 1564324021

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: d1d1ebae421749529153eedaa70e227b
timeCreated: 1564082971

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 3d8829ac873af27488b1412b8db301ab
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,11 +1,8 @@
using Assets.Common;
using Assets.Map;
using Assets.Voronoi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts
@ -100,7 +97,7 @@ namespace Assets.Scripts
public bool displayWorldRoads = false;
}
[RequireComponent(typeof(GraphGenerator))]
[RequireComponent(typeof(LandmassGenerator))]
public class CityGenerator : MonoBehaviour
{
private System.Random _random;
@ -185,8 +182,7 @@ namespace Assets.Scripts
{
var randomLocation = _locationGraph.Vertices[_random.Next(1, _locationGraph.Vertices.Count())];
var count = randomLocation.Sites.Count();
Site randomPoint;
randomPoint = randomLocation.Sites[_random.Next(0, count)];
var randomPoint = randomLocation.Sites[_random.Next(0, count)];
vertices.Add(randomPoint.Index);
}
return vertices;
@ -309,7 +305,7 @@ namespace Assets.Scripts
public void Reset()
{
var graphGenerator = GetComponent<GraphGenerator>();
var graphGenerator = GetComponent<LandmassGenerator>();
_random = new System.Random(graphGenerator.seed);
cities = new List<City>();
}
@ -321,7 +317,7 @@ namespace Assets.Scripts
public void Generate()
{
var graphGenerator = GetComponent<GraphGenerator>();
var graphGenerator = GetComponent<LandmassGenerator>();
graphGenerator.Generate();
_basicGraph = graphGenerator.VoronoiGenerator.Delaunay.Morph(l => l);
_voronoiGraph = graphGenerator.VoronoiGenerator.Voronoi;

View File

@ -1,8 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.Generators;
using Assets.Map;
using Assets.PointDistribution;
using Assets.Voronoi;
using UnityEngine;
using Random = System.Random;
@ -11,7 +10,7 @@ namespace Assets
{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(GraphGenerator))]
[RequireComponent(typeof(LandmassGenerator))]
public class ForestGenerator : MonoBehaviour
{
public GameObject[] trees;
@ -22,7 +21,7 @@ namespace Assets
private Random _random = new Random();
private List<Site> _sites;
private GraphGenerator Generator => GetComponent<GraphGenerator>();
private LandmassGenerator Generator => GetComponent<LandmassGenerator>();
public void Generate()
{

View File

@ -3,8 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Assets.Common;
using Assets.Generators;
using Assets.Map;
using Assets.PointDistribution;
using Assets.Voronoi;
using UnityEditor;
using UnityEngine;
@ -50,7 +49,7 @@ namespace Assets
};
[ExecuteInEditMode]
public class GraphGenerator : MonoBehaviour
public class LandmassGenerator : MonoBehaviour
{
private PoissonDiskSampler _sampler;
private Random _random;
@ -88,7 +87,7 @@ namespace Assets
public void Reset()
{
_random = new System.Random(seed);
_random = new Random(seed);
_sampler = new PoissonDiskSampler(radius);
_sampler.Generator = _random;

View File

@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.Map;
using UnityEngine;
using Random = UnityEngine.Random;
@ -10,7 +8,7 @@ namespace Assets
{
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(GraphGenerator))]
[RequireComponent(typeof(LandmassGenerator))]
public class MapRenderer : MonoBehaviour
{
public float UVScale = 1.0f;
@ -25,7 +23,7 @@ namespace Assets
public void Generate()
{
var graph = GetComponent<GraphGenerator>();
var graph = GetComponent<LandmassGenerator>();
graph.Reset();
graph.Generate();
@ -179,7 +177,7 @@ namespace Assets
private void OnDrawGizmos()
{
var graph = GetComponent<GraphGenerator>();
var graph = GetComponent<LandmassGenerator>();
if (graph?.LocationGenerator?.Result == null) return;

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 72849a31bf1649faa230ac49f1df4d57
timeCreated: 1573303680

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using Assets.Common;
namespace Assets.PointDistribution
{
public interface IPointDistribution
{
IEnumerable<Point> Generate(float width, float height);
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 05c83e14e4294176bfcbac0e8b8f1018
timeCreated: 1573303566

View File

@ -4,9 +4,9 @@ using System.Linq;
using Assets.Common;
using UnityEngine;
namespace Assets.Generators
namespace Assets.PointDistribution
{
public class PoissonDiskSampler
public class PoissonDiskSampler : IPointDistribution
{
public System.Random Generator { get; set; } = new System.Random();
@ -53,11 +53,11 @@ namespace Assets.Generators
var x = (int)Math.Floor(point.x / size);
var y = (int)Math.Floor(point.y / size);
var neighbours = (new List<(int, int)>() {
var neighbours = new List<(int, int)> {
(x - 1, y + 1), (x, y + 1), (x + 1, y + 1),
(x - 1, y), (x, y), (x + 1, y),
(x - 1, y - 1), (x, y - 1), (x + 1, y - 1)
});
};
return neighbours
.Where(p => grid.ContainsKey(p))

View File

@ -0,0 +1,10 @@
namespace Assets.Tools
{
public class TikzGenerator
{
public static void Main()
{
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: bbd6867fa0c423e078e554a9835c397f
guid: 3517404de0c3a15dd8b56257c07ac43a
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@ -1,7 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Assets.Common;
using Assets.Map;
using Priority_Queue;
namespace Assets.Voronoi