Skip to content

Commit

Permalink
Merge branch 'main' of github.com:zfedoran/go-wfc
Browse files Browse the repository at this point in the history
  • Loading branch information
zfedoran committed Apr 28, 2022
2 parents 98d2a4d + 74de52f commit 9066448
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/zfedoran/go-wfc/pkg/wfc.svg)](https://pkg.go.dev/github.com/zfedoran/go-wfc/pkg/wfc)
[![Go Report Card](https://goreportcard.com/badge/github.com/zfedoran/go-wfc)](https://goreportcard.com/report/github.com/zfedoran/go-wfc)

# go-wfc
Randomly generated constraint based tile maps.
Procedurally-generated tile maps using wave function collapse.

## Demos

Live demo (wasm):
* https://zfedoran.github.io/go-wfc-example/

Live algorithm animation (wasm):
* https://zfedoran.github.io/go-wfc-algorithm/


## Overview
This package uses the *Wave Function Collapse* algorithm as described by Oskar
Stålberg.
This package uses the *Wave Function Collapse* algorithm as described by [Oskar
Stålberg](https://www.youtube.com/watch?v=0bcZb-SsnrA&t=350s).

The wave function collapse algorithm is a recursive algorithm that picks a
random tile for a slot on the output image and removes impossible neighbors
Expand All @@ -15,7 +27,7 @@ the [original work](https://github.com/shawnridgeway/wfc).

<img src="/doc/images/banner.jpg?raw=true" width="80%">

### Why
## Why?

There is already a wfc golang library so why another one? The existing one is a
lot more generic and quite a bit slower as a result. Also, the tile setup for
Expand All @@ -25,7 +37,7 @@ to follow and modify.
This variation follows Oskars work and aims to simplify the original work for
easy integration into games.

### Tiles
## Tiles

You'll need a set of tiles, also referred to as `modules`. These will
need to be designed to fit together. The tiles should have matching
Expand All @@ -47,7 +59,7 @@ duplicate the image reference when calling the initialize method.
* Unlike the original WFC implementation, no manual setup or description files
are needed.

### Adjacencies / Constraints
## Adjacencies / Constraints

The wave function collapse algorithm requires some kind of adjacency mapping in
order to remove impossible tiles and stitch together a possible output using the
Expand All @@ -73,7 +85,7 @@ a hash that represents that edge. Any tiles that have the same hash value in the
opposite direction are considered possible adjacencies automatically.


### Contradictions
## Contradictions

It is possible that the wave can collapse into a state that has a contradiction.
For example, sky beneath the ground. If a contradiction is found, the algorithm
Expand Down Expand Up @@ -186,6 +198,10 @@ func collapseWave(tileset_folder, output_image string) {
Complete source can be found here:
[example/main.go](example/main.go)

Also, check out the animated version:
https://github.com/zfedoran/go-wfc-example


## Custom Constraints
If you'd like to customize or change this logic, you are able to pass in a
custom constraint function.
Expand Down
2 changes: 1 addition & 1 deletion pkg/wfc/wave.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func New(tiles []image.Image, width, height int) *Wave {

// NewWithCustomConstraints creates a new wave collapse function with the given
// adjacency constraint calculation function. Use this if you'd like custom
// logic for specifing constraints.
// logic for specifying constraints.
func NewWithCustomConstraints(tiles []image.Image, width, height int, fn ConstraintFunc) *Wave {
wave := &Wave{
Width: width,
Expand Down

0 comments on commit 9066448

Please sign in to comment.