-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Example | ||
# Island Example | ||
|
||
You can run this example (assuming you have go 1.7) using the following commands. | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/zfedoran/go-wfc/pkg/wfc" | ||
) | ||
|
||
// Use 2 color lookups to generate adjacency hash values | ||
var constraintFn = wfc.GetConstraintFunc(2) | ||
|
||
func collapseWave(tileset_folder, output_image string) { | ||
// This is just a `[]image.Image`, you can use whatever loader function you'd like | ||
images, err := wfc.LoadImageFolder(tileset_folder) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// The random seed to use when collapsing the wave | ||
// (given the same seed number, the Collapse() fn would generate the same state every time) | ||
seed := int(time.Now().UnixNano()) | ||
|
||
width := 8 | ||
height := 8 | ||
|
||
// Setup the initialized state | ||
wave := wfc.NewWithCustomConstraints(images, width, height, constraintFn) | ||
wave.Initialize(seed) | ||
|
||
water := wfc.GetConstraintFromHex("95282254") | ||
|
||
// Top | ||
for i := 0; i < width; i++ { | ||
slot := wave.PossibilitySpace[i] | ||
modules := make([]*wfc.Module, 0) | ||
for _, m := range slot.Superposition { | ||
if m.Adjacencies[wfc.Up] == water { | ||
modules = append(modules, m) | ||
} | ||
} | ||
slot.Superposition = modules | ||
} | ||
|
||
// Bottom | ||
for i := 0; i < width; i++ { | ||
slot := wave.PossibilitySpace[i+width*(height-1)] | ||
modules := make([]*wfc.Module, 0) | ||
for _, m := range slot.Superposition { | ||
if m.Adjacencies[wfc.Down] == water { | ||
modules = append(modules, m) | ||
} | ||
} | ||
slot.Superposition = modules | ||
} | ||
|
||
// Left | ||
for i := 0; i < height; i++ { | ||
slot := wave.PossibilitySpace[i*width] | ||
modules := make([]*wfc.Module, 0) | ||
for _, m := range slot.Superposition { | ||
if m.Adjacencies[wfc.Left] == water { | ||
modules = append(modules, m) | ||
} | ||
} | ||
slot.Superposition = modules | ||
} | ||
|
||
// Right | ||
for i := 0; i < height; i++ { | ||
slot := wave.PossibilitySpace[(i+1)*width-1] | ||
modules := make([]*wfc.Module, 0) | ||
for _, m := range slot.Superposition { | ||
if m.Adjacencies[wfc.Right] == water { | ||
modules = append(modules, m) | ||
} | ||
} | ||
slot.Superposition = modules | ||
} | ||
|
||
// Collapse the wave function (make up to 100 attempts) | ||
err = wave.Collapse(200) | ||
if err != nil { | ||
// don't panic here, we want to generate the image anyway | ||
fmt.Printf("unable to generate: %v", err) | ||
} | ||
|
||
// Lets generate an image | ||
output := wave.ExportImage() | ||
output_file := fmt.Sprintf(output_image, seed) | ||
|
||
wfc.SaveImage(output_file, output) | ||
fmt.Printf("Image saved to: %s\n", output_file) | ||
} | ||
|
||
func printAdjacencyHashValues(input_tileset string) { | ||
fmt.Printf("Adjacency hash values:\n\n") | ||
|
||
images, err := wfc.LoadImageFolder(input_tileset) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// We could use pretty table to do this, but this is just a demo and I don't | ||
// want the extra dependency. | ||
|
||
fmt.Println("|-------|----------|----------|") | ||
fmt.Println("|Tile\t|Direction |Hash |") | ||
fmt.Println("|-------|----------|----------|") | ||
for i, img := range images { | ||
for _, d := range wfc.Directions { | ||
fmt.Printf("|%d\t|%s\t | %s | %dx%d\n", i, d.ToString(), constraintFn(img, d), img.Bounds().Max.X, img.Bounds().Max.Y) | ||
} | ||
fmt.Printf("|- - - -|- - - - - |- - - - - |\n") | ||
} | ||
fmt.Printf("|-------|----------|----------|\n\n") | ||
} | ||
|
||
func main() { | ||
input_tileset := "./internal/input" | ||
output_image := "./internal/output/%d.png" | ||
|
||
// Print the adjacency hash values for the provided tileset. | ||
printAdjacencyHashValues(input_tileset) | ||
|
||
// Generate an image from the tileset. | ||
collapseWave(input_tileset, output_image) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Platform Example | ||
|
||
![sample](internal/output/wave.png?raw=true) | ||
|
||
You can run this example (assuming you have go 1.7) using the following commands. | ||
|
||
```bash | ||
cd .. | ||
go mod tidy | ||
go run example/main.go | ||
``` | ||
|
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.