Giter VIP home page Giter VIP logo

rwfc's Introduction

rwfc

A (very much work in progress) implementation of the Wavefunction Collapse Algorithm for procedural generation in R.

You can play around with a demo here keep in mind large grids do take longer to solve:

Usage

1 - Install the package:

from github:

devtools::install_github('pedrocoutinhosilva/rwfc')

2 - Include the library into your project:

# global.R
library(rwfc)

3 - Create a set of tiles that you would like to use. Each tile should contain some data and a set of "keys" that represent each of the sides of the tile. These keys can be any string of numbers of characters, starting with the up side.

Its important to note that keys should be written as if going clockwise around the tile, not simply left to right.

Take for example the following tile:

Each letter represents a material in the tile, A for wall, B for brick and C for space. This makes the top socket CBA and the right socket ABC, since we are moving around the tile clockwise and adding letters as the materials appear.

This is important since when tiles connect, they sockets need to be "mirrored" to actually fit.

You can create tiles with the wfc_tile function:

wfc_tile("tile035", c("ABA", "ABA", "ABA", "ABA"))

If your tile can be rotated, you can include the data for each 45deg rotation as part of a third argument:

wfc_tile("tile020", c("AAA", "ABC", "CBA", "AAA"), c("tile022", "tile060", "tile058"))

4 - After defining your tiles, you can then initiaize a object that will handle the generation of the final tiled grid:

final_grid <- wfc_grid(width, height, tiles)

5 - You can trigger the generation of the tiled grid step by step of all at once:

# Calculate the next tile
final_grid$step()

# Calculate the final grid
final_grid$solve()

At an point you can use the wfc_grid object to access its state:

state <- final_grid$get_grid()

cell <- state$[[target_column]][[target_row]]

cell$get_possible_tiles() #return the current tiles that cell can be

tile <- cell$get_possible_tiles()[[1]] # if only one tile exists,
tile$get_data() # we can retrieve the data from that tile and know the value of a cell in the grid

6 - You can reset the grid and start a new generation by resetting the grid object

final_grid$reset()

TODO

Code is still a mess and in dire need of documentation.

Currently only a simple tile model is implememented, processing images into tiles might come soon (tm) in the future.

Implementing the generation of tiles directly from a image as in the original implementation. For its mostly fun with premade tiles.

References

rwfc's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.