fix poisson disc sampling

This commit is contained in:
Xevion
2020-05-05 21:15:42 -05:00
parent 1051fd3fde
commit 74e0bdd47e
2 changed files with 12 additions and 11 deletions

View File

@@ -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<Vector2> 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;
}
}

View File

@@ -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