Poisson-Disc Sampling

A while ago I watched a video on this topic on what purpose this would have and while watching it I thought It would be a fun challenge for myself to use the concept of how it works and to figure out myself to implement it.

The basics of what poission disc sampling is, is optimized randomization. To generate multiple object on a grid while keeping a certain distance between each point. And this while also not making it seem repetetive and optimized

The Grid

The main optimization for this is that there is a grid, each tile is the distance allowed between each point. the moment it generates the grid it calculated 2 corners of the tile, these are the min and the max position. the height of these points is calculated by shooting a raycast downwards if it didn’t hit anything the height will be set to the height of the object that contains the script.

The min and max points are used to select a random point inside of the tile so you won’t end up with a grid placement.

 

Placing the points on the grid

When placing the points the script makes a copy of all the spots to keep track of what spots are available and what spots are in use. The first point is always a random tile to start things off. when placed the tile it is placed on will be added to the used list and removed from the available list. As well as adding a setting the position as the last placed position. 

When selecting the next spot it will add a randomized amount to the last placed spot and it will search if that spot is available. It has a certain amount of tries to find a nearby spot if it does not find a new spot it will randomly select a new tile from the available tiles. and continue from there.

When it has selected a new spot it will search each tile around it that is in the used tiles and check if the distance between the new point and the placed point is too small when it is too small it will generate a new spot inside of the tile. when it is not able to find a new spot it will skip this position and search for a new tile