https://github.com/krychu/wfc Skip to content Sign up * Why GitHub? + Features + Mobile + Actions + Codespaces + Packages + Security + Code review + Issues + Integrations + GitHub Sponsors + Customer stories * Team * Enterprise * Explore + Explore GitHub + Learn and contribute + Topics + Collections + Trending + Learning Lab + Open source guides + Connect with others + The ReadME Project + Events + Community forum + GitHub Education + GitHub Stars program * Marketplace * Pricing + Plans + Compare plans + Contact Sales + Education [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} krychu / wfc Public * * Notifications * Fork 5 * Star 192 * Wave Function Collapse library in C, plus a command-line tool 192 stars 5 forks Star Notifications * Code * Issues 2 * Pull requests 1 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 1 branch 0 tags Code Latest commit @krychu krychu Docs updates ... 701daac Jan 3, 2022 Docs updates 701daac Git stats * 25 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time examples Samples and doc fixes Apr 26, 2021 samples Delete mosaic.png Apr 26, 2021 Makefile cleanup Nov 11, 2020 Readme.md Docs updates Jan 3, 2022 wfc.h docs Apr 27, 2021 wfctool.c Samples and doc fixes Apr 26, 2021 View code wfc HOW TO USE THE LIBRARY Working with image files COMMAND-LINE TOOL THANKS Readme.md wfc Single-file Wave Function Collapse library in C, plus a command-line tool * License: MIT * Version: 0.01 This is an early version that supports the overlapping WFC method. The method takes an input image and generates output image which is locally similar to the input image. Here're a few examples of input/ output pairs: [116161084-6f546e00-a6f3-11eb-87d8-3ed9c2c7e26c] For a good read on WFC see this article. It's particularly useful for procedural generation of game levels. HOW TO USE THE LIBRARY One file in your project should include wfc.h like this: #define WFC_IMPLEMENTATION #include "wfc.h" Other files can also include and use wfc.h but they shouldn't define WFC_IMPLEMENTATION macro. Usage: struct wfc *wfc = wfc_overlapping( 128, // Output image width in pixels 128, // Output image height in pixels input_image, // Input image that will be cut into tiles 3, // Tile width in pixels 3, // Tile height in pixels 1, // Expand input image on the right and bottom 1, // Add horizontal flips of all tiles 1, // Add vertical flips of all tiles 1 // Add n*90deg rotations of all tiles ); wfc_run(wfc, -1); // Run Wave Function Collapse // -1 means no limit on iterations struct wfc_image *output_image = wfc_output_image(wfc); wfc_destroy(wfc); // use output_image->data // wfc_img_destroy(output_image); By default you work with struct wfc_image for inputs and outputs. struct wfc_image { unsigned char *data; int component_cnt; int width; int height; } data is tightly packed without padding. Each pixel consists of component_cnt components (e.g., four components for rgba format). The output image will have the same number of components as the input image. wfc_run returns 0 if it cannot find a solution. You can try again like so: wfc_init(wfc); wfc_run(wfc, -1); Working with image files wfc can optionally use stb_image.h and stb_write.h to provide convenience functions for working directly with image files. You will normally place stb_image.h and stb_write.h in the same directory as wfc.h and include their implementations in one of the project files: #define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image.h" #include "stb_image_write.h" Further, you will instruct wfc.h to use stb: #define WFC_IMPLEMENTATION #define WFC_USE_STB #include "wfc.h" Usage: struct wfc_image *input_image = wfc_img_load("input.png"); struct wfc *wfc = wfc_overlapping( ... input_image, ... ); wfc_run(wfc, -1); // Run Wave Function Collapse // -1 means no restriction on number of iterations wfc_export(wfc, "output.png"); wfc_img_destroy(input_image); wfc_destroy(wfc); Extra functions enabled by the inclusion of stb: struct wfc_image *image = wfc_img_load("image.png") wfc_img_save(image, "image.png") wfc_export(wfc, "output.png") wfc_export_tiles(wfc, "directory") // don't forget to wfc_img_destroy(image) loaded images COMMAND-LINE TOOL The command-line tool uses the library and allows to generate WFC images. The tool depends on stb_image.h and stb_write.h. Place both files in the same directory as wfctool.c. make ./wfc Run ./wfc to see available options Basic usage: ./wfc -m overlapping -w 128 -h 128 input.png output.png THANKS Thanks for using wfc. If you find any bugs, have questions, or feedback please let me know. Also, if you'd like to share your works it's very appreciated. samp.krystian at gmail.com About Wave Function Collapse library in C, plus a command-line tool Topics c library procedural-generation command-line-tool procgen wfc wave-function-collapse Resources Readme Stars 192 stars Watchers 3 watching Forks 5 forks Releases No releases published Sponsor this project * https://paypal.me/krystiansamp Packages 0 No packages published Languages * C 99.9% * Makefile 0.1% * (c) 2022 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.