Wave Function Collapse — Tutorial of a Basic Example Implementation in Python

Mateusz Bugaj
The Startup
Published in
5 min readJul 31, 2020

--

WaveFunctionCollapse algorithm created by Maxim Gummin and published on github as open source allows user to generate bitmaps and tilemaps based on given input which is extremely useful in creating tilesets, maps or other forms of art. For more information I highly recommend you to read WaveFunctionCollapse is Constraint Solving in the Wild as well as check out Gimmin’s repo.

What we are trying to achieve in this tutorial:

Example taken straight from Gummin’s github

Notice that in output image there is no red pixel next to white pixel since there is no configuration like this in the sample.

Load sample and extract patterns

Let’s start by defining image sample that we are going to use in this example. It is 4x4 pixels picture of a black square in corner with red center. Small size of this sample will help to simplify and better visualize whole process. We are also not going to use RGB colors so that our sample and later patterns can be written as simple array of integers with one value per pixel. Our sample looks like this:

--

--