npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wave-function-collapse-simple

v0.0.115

Published

wave function collapse algorithm for cuboids with backtracking and entropy minimization, 3D and 2D versions. works by either matching edge content or matching arbitrary string labels for each cube face.

Downloads

5

Readme

wave-function-collapse-simple

wave function collapse algorithm for cuboids with backtracking and entropy minimization, 3D and 2D versions. works by either matching edge content or matching arbitrary string labels for each cube face.

Installation

npm i wave-function-collapse-simple

Usage

2D example

var {Tile3D, waveFunctionCollapse2D, printOutputField2D } = require('wave-function-collapse-simple');

var TILE_SIZE = 2; //width/height of the tiles in voxels
var N_STATES = 2; //number of states per voxel
//generate a big number of random tiles. if we manually build our tiles we dont need so many
var TILES = new Array(5000).fill(1).map(function(one,i){
    var tile = new Tile3D(TILE_SIZE, TILE_SIZE, 1);
    //tile.getPixel(x, y, z=0)
    //tile.setPixel(x, y, z, v, skipMemo=true)
    //tile.clone()
    return tile.randomize(N_STATES);
});

var outputSize = [8,8];
var useMemos = false; //if false use voxel-edge-matching, do not auto-save string hashes of faces / compare by string hash
//if true, maintain a string hash of content per-face, compare using the strings
// - note that these can be updated for custom behavior, we can ignore the voxels if we want
// - the face hashes are an obj like this...
// TILE.faces = {
//     'x=0': '',
//     'x=max': '',
//     'y=0': '',
//     'y=max': '',
//     'z=0': '',
//     'z=max': '',
// };

var timeoutMs = 1000; //optional , default 1000
var resultField = waveFunctionCollapse2D(TILES, outputSize, useMemos, timeoutMs);

if(!resultField.timedOut){
    printOutputField2D(resultField /*, silent=false */)
}else{
    console.log('timed out');
}

//see also getOutputFieldGrid2D --> raw result as 2d array

//2x2 tile example with 8x8 tiles in the field

// ###  ##    #####
// ##          ###
// ##          ###
// ##########  ##
// ##########  ##
// ...

//5x5 tile example with 8x8 tiles in the field

// ###### #  # #   ##       ##    # ##  ##
// #######  ## ###  ###    # ### # ##  ##
// #   #  ##   ## ####  #   ##    #######
// ##   ## ####   ########  # #  ## #### #
// #   ## # ###  ### #####   ####### ###  #
// #   ## # ###  ### #####   ####### ###  #
// #     #    #  ##   ##  ### # ### #  ## #
// #       ####### # ###    ## ### #  # ##
// # #   # #####   ##   ## ##########  ## #
// #  #       ### #  ## ###  #### ##  ##
// #  #       ### #  ## ###  #### ##  ##
// #   ###  ###     # ### ### # ## #### # #
// ...

3D example - similar to 2D

var {Tile3D, waveFunctionCollapse3D, printOutputField3D, getNonZeroVoxelsFromTiles} =
    require('wave-function-collapse-simple');

//very slow / difficult for sizes greater than 3 if doing it randomly, just cuz of stats...
// can do 2x2x2 field of 3x3x3, if we have on order of 512x512 (262k samples) or up to 1M samples
var TILE_SIZE = 2;
var N_STATES = 2;
var TILES = new Array(200).fill(1).map(function(one,i){
    return new Tile3D(TILE_SIZE, TILE_SIZE, TILE_SIZE).randomize(N_STATES) ;//generateRandomTile3D(TILE_SIZE,TILE_SIZE,TILE_SIZE);
});

var timeOutMs = 1000; //optional , default 1000
var RES = waveFunctionCollapse3D(TILES, [2, 2, 2], true, timeOutMs)

if(!RES.timedOut){
    console.log(RES)
    printOutputField3D(RES);
}else{
    console.log('timed out');
    throw 'timed out';
}

//see also getOutputFieldGrid3D --> raw result as 3d array

// can also view result in ascii-raytracer ... 
// var blocks = getNonZeroVoxelsFromTiles(RES); //get aabbs from set of tiles -- each aabb is [[minX,minY,minZ],[maxX,maxY,maxZ]]
// var art = require('ascii-raytracer');
// var config = {
//     bricks: blocks,
//     resolution: 64,
//     aspectRatio: 1.0
// }
// art.runScene(config);

//result...
//notice how lsyers 2 and 3 are identical 
//because they must 'match up' in 3d 

// Layer 1:
//
// #
// ###
// ###
// #  #
//
// Layer 2:
//
// #  #
// #
// #
//
//
// Layer 3:
//
// #  #
// #
// #
//
//
// Layer 4:
//
// #  #
// ####
// ####
// ###

See Also

stonks