diff --git a/procedural-placement/Assets/PointGeneration.cs b/procedural-placement/Assets/PointGeneration.cs index 8664961..02e354d 100644 --- a/procedural-placement/Assets/PointGeneration.cs +++ b/procedural-placement/Assets/PointGeneration.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using NUnit.Framework.Constraints; using UnityEngine; public static class PointGeneration { @@ -11,7 +10,7 @@ public static class PointGeneration { new Vector2( Random.Range(0, regionSize.x * 2), Random.Range(0, regionSize.y * 2) - )); + )); } return points; @@ -35,7 +34,7 @@ public static class PointGeneration { float angle = Random.value * Mathf.PI * 2; // Random radian angle Vector2 dir = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)); Vector2 candidate = spawnCenter + dir * Random.Range(radius, radius * 2); - + if (poisson_valid(candidate, regionSize, cellSize, points, grid, radius)) { points.Add(candidate); spawnPoints.Add(candidate); @@ -56,6 +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 @@ -71,16 +71,16 @@ public static class PointGeneration { for (int x = searchStartX; x <= searchEndX; x++) { for (int y = searchStartY; y <= searchEndY; y++) { + // Acquire the index of the point responsible int pointIndex = grid[x, y] - 1; - if (pointIndex == -1) continue; - + if (pointIndex == -1) continue; // Don't calculate + float sqrDst = (candidate - points[pointIndex]).sqrMagnitude; - if (sqrDst < radius * radius) { + if (sqrDst < radius * radius) return false; - } } } - + return true; } } \ No newline at end of file diff --git a/procedural-placement/Assets/Scenes/SampleScene.unity b/procedural-placement/Assets/Scenes/SampleScene.unity index 45abba1..c78d27b 100644 --- a/procedural-placement/Assets/Scenes/SampleScene.unity +++ b/procedural-placement/Assets/Scenes/SampleScene.unity @@ -149,9 +149,10 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 95149e3ad64fa7149affeaeb9f8e273b, type: 3} m_Name: m_EditorClassIdentifier: - numPoints: 50000 - cubeSize: 0.1 - regionSize: {x: 50, y: 50} + samplingMethod: 1 + numPoints: 428 + sphereSize: 0.18 + regionSize: {x: 180, y: 100} --- !u!4 &2048189740 Transform: m_ObjectHideFlags: 0