From 1c6c997db99e9cd3134bb11f11382e68ba711505 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 5 May 2020 23:35:21 -0500 Subject: [PATCH] update sphere rendering and random point sampling to compensate for poisson sampling method, add radius and retry attempt properties for poisson --- .../Assets/PointGeneration.cs | 8 +++---- procedural-placement/Assets/PointRendering.cs | 24 +++++++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/procedural-placement/Assets/PointGeneration.cs b/procedural-placement/Assets/PointGeneration.cs index 02e354d..544e26c 100644 --- a/procedural-placement/Assets/PointGeneration.cs +++ b/procedural-placement/Assets/PointGeneration.cs @@ -8,15 +8,15 @@ public static class PointGeneration { for (var i = 0; i < numPoints; i++) { points.Add( new Vector2( - Random.Range(0, regionSize.x * 2), - Random.Range(0, regionSize.y * 2) + Random.Range(0, regionSize.x), + Random.Range(0, regionSize.y) )); } return points; } - public static List poisson_sampling(int numPoints, Vector2 regionSize, int radius, int attempts = 30) { + public static List poisson_sampling(int numPoints, Vector2 regionSize, float radius, int attempts = 30) { float cellSize = radius / Mathf.Sqrt(2); // A grid for @@ -55,7 +55,7 @@ public static class PointGeneration { private static bool IsValidPoint(Vector2 point, Vector2 region) { return point.x >= 0 && point.y >= 0 && point.x < region.x && point.y < region.y; } - + private static bool poisson_valid(Vector2 candidate, Vector2 regionSize, float cellSize, List points, int[,] grid, float radius) { // Check that the point is valid diff --git a/procedural-placement/Assets/PointRendering.cs b/procedural-placement/Assets/PointRendering.cs index 0d4d06e..6e22d91 100644 --- a/procedural-placement/Assets/PointRendering.cs +++ b/procedural-placement/Assets/PointRendering.cs @@ -19,29 +19,39 @@ public class PointRendering : MonoBehaviour { public float sphereSize = 1; [Tooltip("The Size of the region points are generated in")] public Vector2 regionSize = Vector2.one; + [Tooltip("The radius between each point in Poisson disc sampling")] + public float radius = 3; + [Tooltip("The number of attempts the sampling algorithm will give to place another point")] + public int retryAttempts = 30; // Store the previous value, only useful for the inspector private SamplingTypes _samplingMethod; private int _prevNumPoints; private Vector2 _prevRegionSize; + private float _radius; + private int _retryAttempts; private List _points; private void OnValidate() { - // Check to see if point position related values changed. CubeSize is rendering only, so we ignore it. + // Check to see if point position related values changed. SphereSize is rendering only, so we ignore it. if (numPoints != _prevNumPoints || _prevRegionSize != regionSize - || samplingMethod != _samplingMethod) { - // Update the tracking values + || samplingMethod != _samplingMethod || radius != _radius || retryAttempts != _retryAttempts) { + // Update the property tracking values _prevNumPoints = numPoints; _prevRegionSize = regionSize; _samplingMethod = samplingMethod; - + _radius = radius; + _retryAttempts = retryAttempts; + + // Handling for different types of point sampling methods switch (samplingMethod) { case SamplingTypes.Random: _points = PointGeneration.random_sampling(numPoints, regionSize); break; case SamplingTypes.Poisson: - _points = PointGeneration.poisson_sampling(numPoints, regionSize, 1, 30); + _points = PointGeneration.poisson_sampling(numPoints, regionSize, radius, retryAttempts); + numPoints = _points.Count; break; default: throw new ArgumentOutOfRangeException(); @@ -52,12 +62,12 @@ public class PointRendering : MonoBehaviour { private void OnDrawGizmos() { // Draw a wireframe around the entire region Gizmos.DrawWireCube(this.transform.position, - new Vector3(regionSize.x * 2 + sphereSize, regionSize.y * 2 + sphereSize, sphereSize)); + new Vector3(regionSize.x + sphereSize, regionSize.y + sphereSize, sphereSize)); // Render spheres at every point if (_points == null) return; var pos = transform.position; foreach (var point in _points) - Gizmos.DrawSphere(new Vector3(point.x + pos.x, point.y + pos.y, pos.z), sphereSize); + Gizmos.DrawSphere(new Vector3(point.x + pos.x - (regionSize.x / 2), point.y + pos.y - (regionSize.y / 2)), sphereSize); } } \ No newline at end of file