百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
W

wfc

> 编程语言
开源

C 语言中的波函数坍塌库,以及一个命令行工具

366 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

C 语言中的波函数坍塌库,以及一个命令行工具

wfc

Single-file Wave Function Collapse library in C, plus a command-line tool

  • License: MIT
  • Version: 1.0

Supports the overlapping WFC method. The method takes a small input image and generates a larger output image which is locally similar to the input image. A few examples of input/output pairs:

The WFC is often used for procedural map generation, but is not limited to this use-case.

The library is very performant and includes a number of optimizations not found in other implementations. As an example, generating a 128x128 image from cave.png (308 unique tiles) takes 371ms. Solve times (3x3 patterns, flipped and rotated) on an Intel Core Ultra 9 285K with clang -O3:

Sample Tiles Size Solve (ms)
cave.png 308 128x128 371
cave.png 308 256x256 1729
twolines2.png 76 128x128 58
twolines2.png 76 256x256 514
wrinkles.png 51 128x128 37
wrinkles.png 51 256x256 356

LANGUAGE BINDINGS

  • Rust -- by Noah Ryan

HOW TO USE THE LIBRARY

One file in your project should include wfc.h like this:

c
        #define WFC_IMPLEMENTATION
        #include "wfc.h"

Other files can also include and use wfc.h but they shouldn't define WFC_IMPLEMENTATION macro.

Alternatively, you can build a traditional .o file with make wfc.o and use wfc.h as a regular header file.

Usage:

…

By default you work with struct wfc_image for inputs and outputs.

c
        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:

c
        wfc_run(wfc, different_seed);

Working with image files

wfc can optionally use stb_image.h and stb_image_write.h to provide convenience functions for working directly with image files.

You will normally place stb_image.h and stb_image_write.h in the same directory as wfc.h and include their implementations in one of the project files:

c
        #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:

c
        #define WFC_IMPLEMENTATION
        #define WFC_USE_STB
        #include "wfc.h"

Usage:

c
        struct wfc_image *input_image = wfc_img_load("input.png");
        struct wfc *wfc = wfc_overlapping(
            ...
            input_image,
            ...
        );

        wfc_run(wfc, seed);  // Run Wave Function Collapse
                             // seed controls the random generation
        wfc_export(wfc, "output.png");
        wfc_img_destroy(input_image);
        wfc_destroy(wfc);

Extra functions enabled by the inclusion of stb:

c
        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_image_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 samples/wrinkles.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

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Cccommand-line-toollibraryprocedural-generation

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言