mxgmn/WaveFunctionCollapse

Public

Bitmap & tilemap generation from a single example with the help of ideas from quantum mechanics

  • Stars25.5k+22 tuần này
  • Forks1.4k
  • Watchers485
  • Issues8
  • Ngày tạo30/09/2016
  • Cập nhật gần nhất15/10/2026
  • Ngôn ngữC#
  • Giấy phép—
README.md63.8 KBRaw

WaveFunctionCollapse

This program generates bitmaps that are locally similar to the input bitmap.

main collage

main gif

Local similarity means that

  • (C1) The output should contain only those NxN patterns of pixels that are present in the input.
  • (Weak C2) Distribution of NxN patterns in the input should be similar to the distribution of NxN patterns over a sufficiently large number of outputs. In other words, probability to meet a particular pattern in the output should be close to the density of such patterns in the input.

In the examples a typical value of N is 3.

local similarity

WFC initializes output bitmap in a completely unobserved state, where each pixel value is in superposition of colors of the input bitmap (so if the input was black & white then the unobserved states are shown in different shades of grey). The coefficients in these superpositions are real numbers, not complex numbers, so it doesn't do the actual quantum mechanics, but it was inspired by QM. Then the program goes into the observation-propagation cycle:

  • On each observation step an NxN region is chosen among the unobserved which has the lowest Shannon entropy. This region's state then collapses into a definite state according to its coefficients and the distribution of NxN patterns in the input.
  • On each propagation step new information gained from the collapse on the previous step propagates through the output.

On each step the number of non-zero coefficients decreases and in the end we have a completely observed state, the wave function has collapsed.

It may happen that during propagation all the coefficients for a certain pixel become zero. That means that the algorithm has run into a contradiction and can not continue. The problem of determining whether a certain bitmap allows other nontrivial bitmaps satisfying condition (C1) is NP-hard, so it's impossible to create a fast solution that always finishes. In practice, however, the algorithm runs into contradictions surprisingly rarely.

Wave Function Collapse algorithm has been implemented in

Nội dung README thuộc về mxgmn và cộng đồng đóng góp, giấy phép Không có giấy phép. Nguồn: github.com/mxgmn/WaveFunctionCollapse