Skip to content

Commit 6b3cde7

Browse files
authored
Merge pull request #7 from coryleach/dev
Updated RNG and testing out Wave function collapse methods
2 parents 93b12cc + c851b8b commit 6b3cde7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1853
-354
lines changed

Demo/Demo-URP.unitypackage

32.8 KB
Binary file not shown.

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ Library of utilitities for procedural generation
1717
#### Using UnityPackageManager (for Unity 2019.3 or later)
1818
Open the package manager window (menu: Window > Package Manager)<br/>
1919
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
20-
https://github.com/coryleach/UnityProcgen.git#0.0.7<br/>
20+
https://github.com/coryleach/UnityProcgen.git#0.0.8<br/>
2121

2222
#### Using UnityPackageManager (for Unity 2019.1 or later)
2323

2424
Find the manifest.json file in the Packages folder of your project and edit it to look like this:
2525
```js
2626
{
2727
"dependencies": {
28-
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.7",
28+
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.8",
2929
...
3030
},
3131
}

Runtime/Utility/IRandomNumberGenerator.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ public interface IRandomNumberGenerator
1616
float NextFloatZeroToOne();
1717
float NextFloatNegOneToOne();
1818
float NextFloatRange(float min, float max);
19+
double NextDoubleZeroToOne();
20+
double NextDoubleNegOneToOne();
21+
double NextDoubleRange(double min, double max);
1922
bool RollChance(float probabilityOfReturningTrue);
2023
Vector2 NextDirection2D();
2124
Vector3 NextDirection3D();
2225
}
23-
}
26+
}

Runtime/Utility/Noise.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,18 @@ public static class Noise
5252
return map;
5353
}
5454

55-
private static float FalloffCurve(float value, float a = 3, float b = 2.2f)
55+
public static float GenerateFalloffPoint(int x, int y, int width, int height, float a = 3f, float b = 2.2f, Vector2 offset = default)
56+
{
57+
var x2 = x + Mathf.RoundToInt(width * offset.x);
58+
var y2 = y + Mathf.RoundToInt(height * offset.y);
59+
60+
var valueY = (y2 + 0.5f) / (float) height * 2 - 1;
61+
var valueX = (x2 + 0.5f) / (float) width * 2 - 1;
62+
var value = Mathf.Max(Mathf.Abs(valueX), Mathf.Abs(valueY));
63+
return 1 - FalloffCurve(value, a, b);
64+
}
65+
66+
public static float FalloffCurve(float value, float a = 3, float b = 2.2f)
5667
{
5768
return Mathf.Pow(value, a) / (Mathf.Pow(value, a) + Mathf.Pow(b - b * value, a));
5869
}

Runtime/Utility/NoiseRng.cs

-197
This file was deleted.

Runtime/Utility/PoissonDiskSampling.cs

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
namespace Gameframe.Procgen
66
{
7-
7+
88
public static class PoissonDiskSampling
99
{
1010
public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed, int maxSamplesPerPoint = 30,
1111
Func<Vector2, bool> validate = null)
1212
{
13-
var rng = new System.Random(seed);
13+
var rng = new RandomGeneratorStruct((uint)seed);
1414
var points = new List<Vector2>();
1515
var cellSize = radius / Mathf.Sqrt(2);
1616
var gridWidth = Mathf.CeilToInt(size.x / cellSize);
@@ -24,17 +24,17 @@ public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed,
2424
while (spawnPoints.Count > 0)
2525
{
2626
//Get a random spawn point from the list
27-
var spawnIndex = rng.Next(0, spawnPoints.Count);
27+
var spawnIndex = rng.NextIntRange(0, spawnPoints.Count-1);
2828
var spawnCenter = spawnPoints[spawnIndex];
2929
var accepted = false;
3030

3131
for (int i = 0; i < maxSamplesPerPoint; i++)
3232
{
3333
//Get a random direction vector
34-
var angle = (float) (rng.NextDouble() * Mathf.PI * 2);
34+
var angle = (float) (rng.NextFloatZeroToOne() * Mathf.PI * 2);
3535
var dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
3636
//Get point along that direction vector between radius and 2radius distance away
37-
var distance = (float) (radius + rng.NextDouble() * radius * 2);
37+
var distance = (float) (radius + rng.NextFloatZeroToOne() * radius * 2);
3838
var candidate = spawnCenter + dir * distance;
3939

4040
//If point is valid we can accept it stop sampling
@@ -60,14 +60,14 @@ public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed,
6060
return points;
6161
}
6262

63-
public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size, int seed,
64-
int maxSamplesPerPoint = 30, Func<Vector2Int, bool> validate = null)
63+
public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size, int seed, int maxSamplesPerPoint = 30, Func<Vector2Int, bool> validate = null)
6564
{
66-
var rng = new System.Random(seed);
65+
var rng = new RandomGeneratorStruct((uint)seed);
6766
var points = new List<Vector2Int>();
6867
var cellSize = radius / Mathf.Sqrt(2);
6968
var gridWidth = Mathf.CeilToInt(size.x / cellSize);
7069
var gridHeight = Mathf.CeilToInt(size.y / cellSize);
70+
7171
//Grid values equal to zero mean there is no point in that grid cell
7272
var grid = new int[gridWidth, gridHeight];
7373
var spawnPoints = new List<Vector2Int>();
@@ -77,17 +77,17 @@ public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size,
7777
while (spawnPoints.Count > 0)
7878
{
7979
//Get a random spawn point from the list
80-
var spawnIndex = rng.Next(0, spawnPoints.Count);
80+
var spawnIndex = rng.NextIntRange(0, spawnPoints.Count - 1);
8181
var spawnCenter = spawnPoints[spawnIndex];
8282
var accepted = false;
8383

8484
for (int i = 0; i < maxSamplesPerPoint; i++)
8585
{
8686
//Get a random direction vector
87-
var angle = (float) (rng.NextDouble() * Mathf.PI * 2);
87+
var angle = (float) (rng.NextFloatZeroToOne() * Mathf.PI * 2);
8888
var dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
8989
//Get point along that direction vector between radius and 2radius distance away
90-
var distance = (float) (radius + rng.NextDouble() * radius * 2);
90+
var distance = (float) (radius + rng.NextFloatZeroToOne() * radius * 2);
9191
var pt = spawnCenter + dir * distance;
9292
var candidate = new Vector2Int((int) pt.x, (int) pt.y);
9393

@@ -200,4 +200,4 @@ private static bool IsValid(Vector2 pt, Vector2 sampleRegionSize, float cellSize
200200

201201
}
202202

203-
}
203+
}

0 commit comments

Comments
 (0)