Skip to content

Commit

Permalink
Merge pull request #7 from coryleach/dev
Browse files Browse the repository at this point in the history
Updated RNG and testing out Wave function collapse methods
  • Loading branch information
coryleach authored Mar 7, 2023
2 parents 93b12cc + c851b8b commit 6b3cde7
Show file tree
Hide file tree
Showing 49 changed files with 1,853 additions and 354 deletions.
Binary file modified Demo/Demo-URP.unitypackage
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Library of utilitities for procedural generation
#### Using UnityPackageManager (for Unity 2019.3 or later)
Open the package manager window (menu: Window > Package Manager)<br/>
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
https://github.com/coryleach/UnityProcgen.git#0.0.7<br/>
https://github.com/coryleach/UnityProcgen.git#0.0.8<br/>

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

Find the manifest.json file in the Packages folder of your project and edit it to look like this:
```js
{
"dependencies": {
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.7",
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.8",
...
},
}
Expand Down
5 changes: 4 additions & 1 deletion Runtime/Utility/IRandomNumberGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public interface IRandomNumberGenerator
float NextFloatZeroToOne();
float NextFloatNegOneToOne();
float NextFloatRange(float min, float max);
double NextDoubleZeroToOne();
double NextDoubleNegOneToOne();
double NextDoubleRange(double min, double max);
bool RollChance(float probabilityOfReturningTrue);
Vector2 NextDirection2D();
Vector3 NextDirection3D();
}
}
}
13 changes: 12 additions & 1 deletion Runtime/Utility/Noise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,18 @@ public static class Noise
return map;
}

private static float FalloffCurve(float value, float a = 3, float b = 2.2f)
public static float GenerateFalloffPoint(int x, int y, int width, int height, float a = 3f, float b = 2.2f, Vector2 offset = default)
{
var x2 = x + Mathf.RoundToInt(width * offset.x);
var y2 = y + Mathf.RoundToInt(height * offset.y);

var valueY = (y2 + 0.5f) / (float) height * 2 - 1;
var valueX = (x2 + 0.5f) / (float) width * 2 - 1;
var value = Mathf.Max(Mathf.Abs(valueX), Mathf.Abs(valueY));
return 1 - FalloffCurve(value, a, b);
}

public static float FalloffCurve(float value, float a = 3, float b = 2.2f)
{
return Mathf.Pow(value, a) / (Mathf.Pow(value, a) + Mathf.Pow(b - b * value, a));
}
Expand Down
197 changes: 0 additions & 197 deletions Runtime/Utility/NoiseRng.cs

This file was deleted.

24 changes: 12 additions & 12 deletions Runtime/Utility/PoissonDiskSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Gameframe.Procgen
{

public static class PoissonDiskSampling
{
public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed, int maxSamplesPerPoint = 30,
Func<Vector2, bool> validate = null)
{
var rng = new System.Random(seed);
var rng = new RandomGeneratorStruct((uint)seed);
var points = new List<Vector2>();
var cellSize = radius / Mathf.Sqrt(2);
var gridWidth = Mathf.CeilToInt(size.x / cellSize);
Expand All @@ -24,17 +24,17 @@ public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed,
while (spawnPoints.Count > 0)
{
//Get a random spawn point from the list
var spawnIndex = rng.Next(0, spawnPoints.Count);
var spawnIndex = rng.NextIntRange(0, spawnPoints.Count-1);
var spawnCenter = spawnPoints[spawnIndex];
var accepted = false;

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

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

public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size, int seed,
int maxSamplesPerPoint = 30, Func<Vector2Int, bool> validate = null)
public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size, int seed, int maxSamplesPerPoint = 30, Func<Vector2Int, bool> validate = null)
{
var rng = new System.Random(seed);
var rng = new RandomGeneratorStruct((uint)seed);
var points = new List<Vector2Int>();
var cellSize = radius / Mathf.Sqrt(2);
var gridWidth = Mathf.CeilToInt(size.x / cellSize);
var gridHeight = Mathf.CeilToInt(size.y / cellSize);

//Grid values equal to zero mean there is no point in that grid cell
var grid = new int[gridWidth, gridHeight];
var spawnPoints = new List<Vector2Int>();
Expand All @@ -77,17 +77,17 @@ public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size,
while (spawnPoints.Count > 0)
{
//Get a random spawn point from the list
var spawnIndex = rng.Next(0, spawnPoints.Count);
var spawnIndex = rng.NextIntRange(0, spawnPoints.Count - 1);
var spawnCenter = spawnPoints[spawnIndex];
var accepted = false;

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

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

}

}
}
Loading

0 comments on commit 6b3cde7

Please sign in to comment.