[Project Record] Procedural Content Generation (PCG) and Fast Poisson Disk Sampling Algorithm to Realize Object Placement in Unity

background

  In today's game development process, creating a content-rich virtual world has always been a very time-consuming task. Game makers hope to add more diverse content to the game in a shorter time, and procedural content generation technology provides a solution for making complex virtual worlds in a limited time.

process content generation

  Procedural Content Generation (PCG), which can also be translated as procedural content generation, mainly refers to the automatic generation of game content through algorithms and input parameters, including terrain, maps, levels, stories, dialogues, tasks, characters, etc. PCG in a broad sense is not limited to the game industry. For example, in the animation field, Pixar uses the RenderMan animation tool to define textures and materials through algorithms.
  Here I have to mention the excellent game Minecraft. The entire game world is generated algorithmically in the game, including plains, forests, hills, lakes, rivers, caves, oceans, villages, dungeons, etc.

Screenshot of Minecraft game (picture source network)

  Houdini is another procedural generation tool that is often mentioned.

Ubisoft shares how procedural tools are used to generate natural environments in Far Cry 5 (GDC 2018)

Fast Poisson Disk Sampling Algorithm

  Fast Poisson Disk Sampling, the algorithm is used to generate a point distribution with overall regularity and local changes in space, which is characterized by a dense distribution of sampling points and a certain distance between them. The core idea is to grid the space, and there is at most one sampling point in each grid, so as to quickly detect the distance between sampling points (only need to detect whether there is a distance conflict with the sampling points in the nearby grid). The basic steps of the algorithm are as follows:

  • If the sampling radius is r and the dimension is n, the grid is divided with r/√n as the grid width, and the diagonal length of the grid is r.
  • Pick a point at random and add it to the "active list".
  • When the "active list" is not empty, randomly select a point from the active list, and randomly select a new sampling point from the circle with the point as the center and radius r to 2r. If the sampling point meets the distance requirement, add it to the "active list" and repeat this step; if the sampling point does not meet the distance requirement, repeat the selection at most k times, if all are not satisfied, move the selected point out of the "active list".

  Here is a simple process demonstration in Unity (the grid is evenly divided, and it is blurry after converting to gif compression, resulting in a few lines missing):

Object placement

  Based on the fast Poisson disk sampling algorithm and extended to a certain extent, by sampling on a two-dimensional plane and using ray detection to map it to a three-dimensional space, the automatic placement of multiple types of objects in Unity is realized. Objects can be correctly placed on rough ground, and different objects can have different distributions, and all objects are kept at a reasonable distance.

Configuration parameters and generation effects

Please add a picture description

Comparison of effects before and after sampling in the actual game
(The grass in the middle represents the plain, and the grass and mud on both sides represent the hills, and the generation effects of the two are different)

  By configuring parameters, it is possible to achieve various styles and avoid specific areas:

Comparison of effects before and after sampling in the demo

  Finally, add a simple adjacent grid detection map (as follows), because the relationship between the grid width and detection radius in the original algorithm is definite (the detection radius is the sampling radius r, and the grid width is r/√n), Therefore, the grids that may cause distance conflicts are also determined. However, after the expansion, this relationship becomes uncertain, and it is necessary to additionally judge the grid to be used for distance detection. The left side of the figure below is the grid that can actually generate distance conflicts, and the right side of the figure is the grid that is actually used for distance detection in order to simplify the algorithm.

something else

  The placement of objects in the project is based on the existing terrain. In addition, terrain and terrain objects (such as vegetation) can be generated simultaneously through height maps, density maps, etc.
  When I write about terrain generation, it reminds me of a simple voxel terrain generated based on Perlin noise that I did a few years ago (the whole world is the same terrain, without multi-level superposition of noise or further processing to make other terrains Effect). At that time, it was naturally because of mc. After all, my plan to enter the game industry was inseparable from mc.


  Attach the original text of fast Poisson disk sampling: https://www.cct.lsu.edu/~fharhad/ganbatte/siggraph2007/CD2/content/sketches/0250.pdf

Guess you like

Origin blog.csdn.net/qq_43459138/article/details/125393993