An Unreal plugin for procedural generation using Wave Function Collapse algorithms.
Note: This plugin currently conflicts with the built-in
WaveFunctionCollapseplugin, which uses a ShortName ofWFC. Disable that plugin or change this plugin's module name if both are needed.
- Game and level generation oriented features.
- Shape agnostic grid (2D, 3D, and others).
- Modular constraint setup.
WFCPlugin-2DGrid.mp4
-
The
UWFCGeneratorbrings together several pieces needed to go from source data to a grid of selected tiles.- A
UWFCModeldefines all available tiles. UWFCGridConfigspecifies which grid class to use, grid dimensions, etc- An array of
UWFCConstraintobjects set the rules for how tiles can be placed.- What tiles can be adjacent to each other?
- How many times can a tile be used?
- One or more
UWFCCellSelectorobjects that handle picking which cell to collapse next.- The most common method is based on entropy -- basically the cells that have the least amount of possible solutions are collapsed first, since they're likely to cause conflicts if put off until later.
- A
-
With those pieces in place, the generator can be run until all cells have selected a tile.
-
During each update...
- Constraints are applied, which eliminate possibilities from each grid cell.
- Selection is run, which picks a specific tile to use for a single cell.
-
If any contradictions occur (a cell ends up with no possible tiles), the generator errors out.
- There's no backtracking (yet), so the workaround is to re-run the generator, and try to improve constraint and tile setups to avoid the likelihood of contradictions.
-
All these pieces are basic
UObjectsand can be used manually in various ways if needed. -
Async generation is not yet supported.
-
The
UWFCGeneratorComponentprovides a simple interface to run everything from an Actor. -
It requires a
UWFCAssetas input, which defines all the generator requirements listed above. -
UWFCAssetModel(subclass ofUWFCModel) makes it easier to define tiles using data assets.UWFCTileSetcontains an array ofUWFCTileAssetwhich define things like which actor class to spawn for a tile, edge tags for automatically supporting adjacency constraints, etc- This model handles expanding these tile assets into the individual tiles using during generation.
- If a tile asset can be rotated, permutations are created for each rotation.
- If a tile asset spans more than 1 grid cell (like a big 3x3 piece in a 2D grid), the individual tiles making up a big tile are defined, and adjacency rules created to make sure the groups of tiles are selected together.
-
The
UWFCGeneratorComponentonly handles running the generator, but aAWFCTestingActoris provided as an example for spawning tile actors after each grid cell has a tile selected.- It's expected that you handle spawning or loading content however you need using the
OnCellSelectedEventorOnFinishedEventof the generator component.
- It's expected that you handle spawning or loading content however you need using the
- Create a
UWFCAsset - Add some constraints, such as an edge and boundary constraint which provide basic rules for respecting adjacency rules.
- Pick a cell selector,
WFCEntropyCellSelectoris the most common. - Select a grid config class (2D or 3D), and set the grid dimensions.
- Create a
UWFCTileSetdata asset and assign it to the WFC asset. - Create some
UWFCTileAsset2DorUWFCTileAsset3Dtiles (whichever matches the grid config) and assign them to the set. - For each tile asset...
- Create an actor to spawn with some visuals to represent the tile and assign it.
- Set the edge type tags which will be used by the adjacency constraint to make sure only similar edges are next to each other.
- Add a
AWFCTestingActorto the level and assign the WFC asset to its generator component. - Hit Play, the WFCTestingActor will automatically run the generator on BeginPlay, and selected tiles will be spawned.
Check out the
Grid2DTestorGrid3DTesttest levels for full examples, as well as theirWFC_Test2DandWFC_Test3Dexample WFC assets.