Wave Function Collapse (WFC) is a procedural generation algorithm that allows creating patterns or images based on a set of constraints.
The original implementation of WFC was created by Max Gumin in C#, and it is currently available in his Github repository under the MIT license.
WFC is an algorithm that uses a technique called “wave propagation” to generate patterns. It starts with a blank image divided into cells and defines certain constraints that must be met in each of the cells.
Next, it analyzes the possible combinations of patterns that meet these constraints, and uses a backtracking algorithm to discard those that do not meet the established constraints.
This process is repeated until only one possible combination remains, which will be the solution generated by the algorithm.
This technique has been used in various applications, such as creating textures in video games, generating maps and levels, among others.
How to use WaveFunctionCollapse
If you want to try WFC, you need to download the library from Github. Once the library is downloaded, you can use the algorithm in your project. Below is an example of how to use WFC in C#:
Here are some examples of how to use WaveFunctionCollapse taken from the library’s documentation
// Load the input image
Bitmap inputImage = new Bitmap("input.png");
// Create a new instance of WaveFunctionCollapse
WaveFunctionCollapse wfc = new WaveFunctionCollapse(inputImage, tileSize: 4, randomSeed: 12345);
// Run the WaveFunctionCollapse algorithm
bool result = wfc.Run();
if (result)
{
// If the execution is successful, you can obtain the generated matrix with the GetOutput method
Bitmap outputImage = wfc.GetOutput();
outputImage.Save("output.png");
}
It is a powerful and, frankly, very fun tool for procedural content generation in C#. We can generate a wide variety of patterns and structures from an input image.
WaveFunctionCollapse is Open Source, and all the code and documentation is available in the project’s repository at https://github.com/mxgmn/WaveFunctionCollapse