4
4
5
5
namespace Gameframe . Procgen
6
6
{
7
-
7
+
8
8
public static class PoissonDiskSampling
9
9
{
10
10
public static List < Vector2 > GeneratePoints ( float radius , Vector2 size , int seed , int maxSamplesPerPoint = 30 ,
11
11
Func < Vector2 , bool > validate = null )
12
12
{
13
- var rng = new System . Random ( seed ) ;
13
+ var rng = new RandomGeneratorStruct ( ( uint ) seed ) ;
14
14
var points = new List < Vector2 > ( ) ;
15
15
var cellSize = radius / Mathf . Sqrt ( 2 ) ;
16
16
var gridWidth = Mathf . CeilToInt ( size . x / cellSize ) ;
@@ -24,17 +24,17 @@ public static List<Vector2> GeneratePoints(float radius, Vector2 size, int seed,
24
24
while ( spawnPoints . Count > 0 )
25
25
{
26
26
//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 ) ;
28
28
var spawnCenter = spawnPoints [ spawnIndex ] ;
29
29
var accepted = false ;
30
30
31
31
for ( int i = 0 ; i < maxSamplesPerPoint ; i ++ )
32
32
{
33
33
//Get a random direction vector
34
- var angle = ( float ) ( rng . NextDouble ( ) * Mathf . PI * 2 ) ;
34
+ var angle = ( float ) ( rng . NextFloatZeroToOne ( ) * Mathf . PI * 2 ) ;
35
35
var dir = new Vector2 ( Mathf . Sin ( angle ) , Mathf . Cos ( angle ) ) ;
36
36
//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 ) ;
38
38
var candidate = spawnCenter + dir * distance ;
39
39
40
40
//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,
60
60
return points ;
61
61
}
62
62
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 )
65
64
{
66
- var rng = new System . Random ( seed ) ;
65
+ var rng = new RandomGeneratorStruct ( ( uint ) seed ) ;
67
66
var points = new List < Vector2Int > ( ) ;
68
67
var cellSize = radius / Mathf . Sqrt ( 2 ) ;
69
68
var gridWidth = Mathf . CeilToInt ( size . x / cellSize ) ;
70
69
var gridHeight = Mathf . CeilToInt ( size . y / cellSize ) ;
70
+
71
71
//Grid values equal to zero mean there is no point in that grid cell
72
72
var grid = new int [ gridWidth , gridHeight ] ;
73
73
var spawnPoints = new List < Vector2Int > ( ) ;
@@ -77,17 +77,17 @@ public static List<Vector2Int> GenerateIntPoints(float radius, Vector2Int size,
77
77
while ( spawnPoints . Count > 0 )
78
78
{
79
79
//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 ) ;
81
81
var spawnCenter = spawnPoints [ spawnIndex ] ;
82
82
var accepted = false ;
83
83
84
84
for ( int i = 0 ; i < maxSamplesPerPoint ; i ++ )
85
85
{
86
86
//Get a random direction vector
87
- var angle = ( float ) ( rng . NextDouble ( ) * Mathf . PI * 2 ) ;
87
+ var angle = ( float ) ( rng . NextFloatZeroToOne ( ) * Mathf . PI * 2 ) ;
88
88
var dir = new Vector2 ( Mathf . Sin ( angle ) , Mathf . Cos ( angle ) ) ;
89
89
//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 ) ;
91
91
var pt = spawnCenter + dir * distance ;
92
92
var candidate = new Vector2Int ( ( int ) pt . x , ( int ) pt . y ) ;
93
93
@@ -200,4 +200,4 @@ private static bool IsValid(Vector2 pt, Vector2 sampleRegionSize, float cellSize
200
200
201
201
}
202
202
203
- }
203
+ }
0 commit comments