$ show mxgmn/WaveFunctionCollapse
This repository implements the Wave Function Collapse (WFC) algorithm, a constraint-based procedural content generation technique. The system takes an input bitmap (or a tileset with adjacency rules) and generates a larger output bitmap that is locally similar to the input, meaning every NΓN pattern in the output appears somewhere in the input. It supports two primary models: the OverlappingModel, which extracts patterns from a sample image, and the SimpleTiledModel, which uses predefined tiles and adjacency constraints defined in XML. The algorithm operates by maintaining a 'wave' of possible states for each output cell, iteratively collapsing the cell with the lowest Shannon entropy and propagating constraints until a fully observed state is reached or a contradiction occurs. The architecture is a straightforward console application written in C# targeting .NET 10. The entry point is Program.cs, which reads a configuration file (samples.xml) specifying model parameters and input samples. The core algorithm is encapsulated in the abstract Model class, with concrete implementations in OverlappingModel.cs and SimpleTiledModel.cs. Image loading and saving are handled by the BitmapHelper class using the SixLabors.ImageSharp library. The system outputs PNG images to an 'output' directory. External dependencies are minimal, relying only on ImageSharp for bitmap I/O. The codebase is well-structured and demonstrates clean separation of concerns. The algorithm implementation is faithful to the original paper and includes optimizations such as the AC-4 constraint propagation algorithm and multiple heuristic strategies (entropy, MRV, scanline). The tile symmetry system in SimpleTiledModel reduces the number of explicit adjacency rules needed. While the repository is primarily a reference implementation, its componentsβparticularly the helper utilities and the core propagation logicβare reusable in other procedural generation projects.
The LLM identified 2 files worth highlighting for reuse:
Provides reusable utility functions: a weighted random selection method (Random extension on double[]), a generic XML attribute parser (Get<T>), and a bitmap loading/saving wrapper (BitmapHelper). The weighted random selection is a common pattern for probabilistic algorithms and can be extracted independently. The XML helper simplifies reading typed attributes from XElement. BitmapHelper abstracts ImageSharp operations, making it easy to swap image libraries.
Abstract base class implementing the core WFC algorithm: wave initialization, observation (lowest entropy selection), propagation (AC-4 constraint propagation), and contradiction handling. The propagation logic uses a stack-based approach and maintains compatibility counts for efficiency. The entropy calculation and heuristic selection (Entropy, MRV, Scanline) are cleanly separated. While tightly coupled to the specific problem, the pattern of maintaining a wave of possibilities and propagating constraints is reusable for other constraint satisfaction problems.