gridbugs / wfc

Crates implementing the Wave Function Collapse algorithm

Home Page:https://gridbugs.org/wave-function-collapse/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use WFC for tiles?

reasv opened this issue · comments

commented

I found out about this library through the blog post
https://gridbugs.org/wave-function-collapse/
It describes the library as being composed of a basic WFC algorithm, which is part of the wfc
crate, and another library that uses the former in order to operate on images, wfc-image.
I have been successful in trying out wfc-image however I need a tile-based approach for my own
application.

I have a 2D tileset and want to create a map using adjacency rules (so basically tiled, not overlapping method)
This is described on the blog:

Note that there are some alternative applications of WFC besides generating similar images, such as arranging hand-crafted tiles with user-specified adjacency rules and frequency hints. These applications would still use the same core algorithm, but the image processor would be different.

In the post there's a function mentioned that would basically do what I want:

fn wfc_core(
    adjacency_rules: AdjacencyRules,
    frequency_rules: FrequencyHints,
    output_size: (u32, u32),
) -> Grid2D<TileIndex> { ... }

However this function doesn't exist inside wfc.
I've tried to understand how wfc-image uses wfc.
It seems like it calls new_wrap_forbid().
But it's not clear to me how I would accomplish my goal in particular, as there's no examples of using wfc by itself.

My question is, is this possible with your library? If so, then could you point me in the right direction as to which functions I should use?

I understand I'd probably have to call new_wrap_forbid().
I don't think I need to set the wrap to anything special, I assume it works with just WrapXY for my case? Same with forbid, I don't know what that is but I assume I can just set it to ForbidNothing.
Output_size and RNG are obvious.
But I don't understand what the GlobalStats part represents.
It must represent the adjacency rules since no other parameter contains those.
How do I construct the adjacency rules? I have explicit rules I already know beforehand.

Thanks in advance for any help you can give me.

commented

Update: after reflecting some more on the code, I've figured out how to use it for this purpose,
and I've written a small demonstration script that computes the wave from adjacency rules and then outputs an image to show the result, with the color of each pixel changing depending on the PatternId that should be there.
It might be useful to include something similar as an example if other people want to use this.

commented

I will post a cleaned up version of the script here, tell me if I should make a pull request.