Tiled Wave Function Collapse
This library contains helper functions to use the Wave Function Collapse algorithm provided by the wfc
crate on tile-based maps.
You can load layer CSV files like the ones exported from Tiled, and save the result as another CSV or as a Tiled .tmx file for previewing inside the software.
As the underlying library only works on two dimensions, multiple layers are not supported.
Getting Started
The crate includes commented example programs under the examples/
folder.
The simple
example takes a CSV tilemap as its only argument and saves the output of wfc as a CSV as well as a .tmx file.
You can run it with:
cargo run --example=simple example\input.csv example\tilemap.png
(Replace the slashes for non-Windows environments)
This will produce out.tmx
and out.csv
containing the 32x32 tile WFC output.
Code
For convenience I provide code equivalent to the example program here:
let input_path = "example\\input.csv";
let tilemap_path = "example\\tilemap.png";
let attempts = 1000;
let pattern_size = 2;
let output_size = new;
// Extract patterns from input
let pattern = from_csv.expect;
// Run Wave Function Collapse
let grid = pattern.run_collapse.expect;
// Save as CSV
grid_to_csv?;
// Save as Tiled .tmx file
let tset = TileSet ;
grid_to_tiled?;
This assumes you have the necessary import
use *;
Anchoring
The anchor
example shows how to prevent the output from wrapping around the edges of the map.
It's analogous to the example with the same name in the wfc-image
crate.
You can run it with:
cargo run --example=anchor example\input.csv example\tilemap.png
The program uses a Forbid
rule which forces the right and bottom borders of the output to be equal to the bottom right corner of the input.
Since the system wraps around edges automatically, this is the same as forcing the output to be surrounded by a "wall" made of the input's bottom right corner tiles.
You can look at src/forbid_corner.rs
to learn how to construct your own forbid rule. These rules can force wfc
to only allow certain whitelisted tiles in the output for specific coordinates, or to only forbid certain blacklisted tiles at certain coordinates.
The ForceBorderForbid
used in the example takes the tiles in the lower right corner and sets those as the only allowed tiles for each coordinate corresponding to the bottom and right borders of the output.