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 System.Collections.Generic;
using NUnit.Framework.Constraints;
using UnityEngine; using UnityEngine;
public static class PointGeneration { public static class PointGeneration {
@@ -56,6 +55,7 @@ public static class PointGeneration {
private static bool IsValidPoint(Vector2 point, Vector2 region) { private static bool IsValidPoint(Vector2 point, Vector2 region) {
return point.x >= 0 && point.y >= 0 && point.x < region.x && point.y < region.y; 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, private static bool poisson_valid(Vector2 candidate, Vector2 regionSize, float cellSize, List<Vector2> points,
int[,] grid, float radius) { int[,] grid, float radius) {
// Check that the point is valid // Check that the point is valid
@@ -71,15 +71,15 @@ public static class PointGeneration {
for (int x = searchStartX; x <= searchEndX; x++) { for (int x = searchStartX; x <= searchEndX; x++) {
for (int y = searchStartY; y <= searchEndY; y++) { for (int y = searchStartY; y <= searchEndY; y++) {
// Acquire the index of the point responsible
int pointIndex = grid[x, y] - 1; int pointIndex = grid[x, y] - 1;
if (pointIndex == -1) continue; if (pointIndex == -1) continue; // Don't calculate
float sqrDst = (candidate - points[pointIndex]).sqrMagnitude; float sqrDst = (candidate - points[pointIndex]).sqrMagnitude;
if (sqrDst < radius * radius) { if (sqrDst < radius * radius)
return false; return false;
} }
} }
}
return true; return true;
} }

View File

@@ -149,9 +149,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 95149e3ad64fa7149affeaeb9f8e273b, type: 3} m_Script: {fileID: 11500000, guid: 95149e3ad64fa7149affeaeb9f8e273b, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
numPoints: 50000 samplingMethod: 1
cubeSize: 0.1 numPoints: 428
regionSize: {x: 50, y: 50} sphereSize: 0.18
regionSize: {x: 180, y: 100}
--- !u!4 &2048189740 --- !u!4 &2048189740
Transform: Transform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0