mxgmn / WaveFunctionCollapse

Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanics
Other
23.05k stars 1.23k forks source link

Generate from a pre-defined output image #85

Open SKFrozenCloud opened 1 year ago

SKFrozenCloud commented 1 year ago

Hi,

I'm trying to implement a way to give the algorithm a pre-defined output image containing fixed (already put in place) tiles (in overlap mode).

I do not understand the code very well but I have some ideas.

So on this line the output image is created: https://github.com/mxgmn/WaveFunctionCollapse/blob/a6f79f0f1a4220406220782b71d3fcc73a24a4c2/OverlappingModel.cs#L116 Is it possible to fill the observed array with our pre-defined image (before running the algorithm)? What does the array observed do? Another option would be to put it in inside the init or observer function.

EDIT:

My use-case is specifying an output image where the border is filled with "water" so that you can guarantee that the algorithm always generates "islands". Same goes with "rooms".

mxgmn commented 1 year ago

Hi! To constrain the output, you need to:

  1. Ban all unwanted options.
  2. Call Propagate() to propagate the bans.

For an example, see how I constrain ground pixels here.

SKFrozenCloud commented 1 year ago

Could you just explain the snippet of code you linked so that I can write my own?

mxgmn commented 1 year ago

Sure!

if (ground)
{
    for (int x = 0; x < MX; x++)
    {
        for (int t = 0; t < T - 1; t++) Ban(x + (MY - 1) * MX, t);
        for (int y = 0; y < MY - 1; y++) Ban(x + y * MX, T - 1);
    }
    Propagate();
}

This code executes in examples where we need to fix the ground level. That is, in examples like Skyline, Flowers and Platformer, where we see the picture from the side and not from the top. If we omit this code, the ground level could appear anywhere in the output (like in the middle of an image), or there could even be multiple ground levels. Try changing to ground="False" in samples.xml and see what happens.

Ground pattern is the last pattern in the input, so its index equals T-1.

  1. In the first interior for in the code, we ban all non-ground (that is, t != T-1) patterns from the ground level.
  2. In the second for, we ban the ground pattern from non-ground (that is, y != MY-1) levels.

And then we propagate the bans.

In your example, to generate islands, you need to ban all non-water patterns from the border of the image, and then propagate.

I guess I'll reopen the issue because this is a common question.

SKFrozenCloud commented 1 year ago

Thanks, now I understand.