mirror of
https://github.com/Xevion/procedural-placement.git
synced 2025-12-06 13:16:02 -06:00
comments, add wireframe option for radius display
This commit is contained in:
@@ -86,9 +86,8 @@ public static class PointGeneration {
|
|||||||
|
|
||||||
public static List<Vector2> improved_poisson_sampling(int numPoints, Vector2 regionSize, float radius,
|
public static List<Vector2> improved_poisson_sampling(int numPoints, Vector2 regionSize, float radius,
|
||||||
int attempts = 30) {
|
int attempts = 30) {
|
||||||
float cellSize = radius / Mathf.Sqrt(2);
|
float cellSize = radius / Mathf.Sqrt(2); // Size of each cell in the region
|
||||||
|
|
||||||
// A grid for
|
|
||||||
int[,] grid = new int[Mathf.CeilToInt(regionSize.x / cellSize), Mathf.CeilToInt(regionSize.y / cellSize)];
|
int[,] grid = new int[Mathf.CeilToInt(regionSize.x / cellSize), Mathf.CeilToInt(regionSize.y / cellSize)];
|
||||||
List<Vector2> points = new List<Vector2>();
|
List<Vector2> points = new List<Vector2>();
|
||||||
List<Vector2> spawnPoints = new List<Vector2>();
|
List<Vector2> spawnPoints = new List<Vector2>();
|
||||||
@@ -100,19 +99,19 @@ public static class PointGeneration {
|
|||||||
bool candidateAccepted = false;
|
bool candidateAccepted = false;
|
||||||
|
|
||||||
float seed = Random.value;
|
float seed = Random.value;
|
||||||
float epsilon = 0.000000001f;
|
float epsilon = 0.0000001f;
|
||||||
|
|
||||||
|
// Try to place a new point near
|
||||||
for (int i = 0; i < attempts; i++) {
|
for (int i = 0; i < attempts; i++) {
|
||||||
|
// Old poisson sampling math
|
||||||
// float angle = Random.value * Mathf.PI * 2; // Random radian angle
|
// float angle = Random.value * Mathf.PI * 2; // Random radian angle
|
||||||
// Vector2 dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
|
// Vector2 dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
|
||||||
// Vector2 candidate = spawnCenter + dir * Random.Range(radius, radius * 2);
|
// Vector2 candidate = spawnCenter + dir * Random.Range(radius, radius * 2);
|
||||||
|
|
||||||
float theta = 2 * Mathf.PI * (seed + i / attempts);
|
float theta = 2 * Mathf.PI * (seed + i / attempts);
|
||||||
Vector2 candidate = spawnCenter + (radius + epsilon) * new Vector2(Mathf.Sin(theta), Mathf.Cos(theta));
|
Vector2 candidate = spawnCenter + (radius + epsilon) * new Vector2(Mathf.Sin(theta), Mathf.Cos(theta));
|
||||||
// Vector2 candidate = new Vector2(
|
|
||||||
// spawnCenter.x + r * Mathf.Sin(theta),
|
// Check that the point is valid, make the corresponding changes
|
||||||
// spawnCenter.y + r * Mathf.Cos(theta)
|
|
||||||
// );
|
|
||||||
|
|
||||||
if (poisson_valid(candidate, regionSize, cellSize, points, grid, radius)) {
|
if (poisson_valid(candidate, regionSize, cellSize, points, grid, radius)) {
|
||||||
points.Add(candidate);
|
points.Add(candidate);
|
||||||
spawnPoints.Add(candidate);
|
spawnPoints.Add(candidate);
|
||||||
@@ -122,6 +121,7 @@ public static class PointGeneration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If no suitable new point could be found, remove it from the running
|
||||||
if (!candidateAccepted) {
|
if (!candidateAccepted) {
|
||||||
spawnPoints.RemoveAt(spawnIndex);
|
spawnPoints.RemoveAt(spawnIndex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ public class PointRendering : MonoBehaviour {
|
|||||||
public float radius = 3;
|
public float radius = 3;
|
||||||
[Tooltip("The number of attempts the sampling algorithm will give to place another point")]
|
[Tooltip("The number of attempts the sampling algorithm will give to place another point")]
|
||||||
public int retryAttempts = 30;
|
public int retryAttempts = 30;
|
||||||
|
[Tooltip("Draw a wireframe sphere showing the radius between each sphere.")]
|
||||||
|
public bool drawSphere = false;
|
||||||
|
|
||||||
// Store the previous value, only useful for the inspector
|
// Store the previous value, only useful for the inspector
|
||||||
private SamplingTypes _samplingMethod;
|
private SamplingTypes _samplingMethod;
|
||||||
@@ -72,7 +74,12 @@ public class PointRendering : MonoBehaviour {
|
|||||||
// Render spheres at every point
|
// Render spheres at every point
|
||||||
if (_points == null) return;
|
if (_points == null) return;
|
||||||
var pos = transform.position;
|
var pos = transform.position;
|
||||||
foreach (var point in _points)
|
foreach (var point in _points) {
|
||||||
Gizmos.DrawSphere(new Vector3(point.x + pos.x - (regionSize.x / 2), point.y + pos.y - (regionSize.y / 2)), sphereSize);
|
var center = new Vector3(point.x + pos.x - (regionSize.x / 2), point.y + pos.y - (regionSize.y / 2));
|
||||||
|
Gizmos.DrawSphere(center,
|
||||||
|
sphereSize);
|
||||||
|
if(drawSphere)
|
||||||
|
Gizmos.DrawWireSphere(center, radius / 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user