A Unity implementation of the Wave Function Collapse algorithm for procedural tile map generation with interactive UI.
- Three-Tile Color System: Generate maps with blue, green, and yellow tiles
- Image Import: Import any image and generate maps based on its patterns and colors
- Adjacency Rules:
- Yellow can touch blue and green
- Blue and green cannot touch each other
- Blue can touch yellow
- Green can touch yellow
- Seed-Based Generation: Input custom seeds for reproducible map generation
- Adjustable Map Size: Configure map dimensions (default: 20x20, range: 5-100)
- Custom Tileset Support: Define your own tiles and adjacency rules via JSON
- Interactive Unity UI: Modern UI with TextMeshPro controls
Assets/
├── Scripts/
│ ├── WaveFunctionCollapse.cs - Core WFC algorithm
│ ├── WFCManager.cs - Main UI controller and map generator
│ ├── TileConfig.cs - Tile configuration classes
│ ├── ImageToWFC.cs - Image analysis for WFC generation
│ └── ImageWFCExample.cs - Example script for programmatic image import
├── Scenes/
│ └── MainScene.unity - Main scene with UI and map display
└── Prefabs/
└── Tile.prefab - Tile prefab for rendering
- Unity 2021.3 or later
- TextMeshPro package (should be auto-imported)
- Open Unity Hub
- Click "Add" and select this project folder
- Open the project with Unity 2021.3 or later
- Open the
Assets/Scenes/MainScene.unityscene
The MainScene should contain:
- Canvas with UI elements:
- Seed input field (TMP_InputField)
- Width input field (TMP_InputField)
- Height input field (TMP_InputField)
- Tileset mode dropdown (TMP_Dropdown)
- Custom tileset input (TMP_InputField, multiline)
- Generate button
- Random Seed button
- Current seed text display
- WFCManager GameObject with the
WFCManager.csscript attached - Map Container Empty GameObject to hold generated tiles
- Main Camera set to Orthographic mode
Attach the WFCManager.cs script to a GameObject and assign:
- All UI references (input fields, buttons, text)
- Map Container transform
- Tile prefab (optional, will create quads if not provided)
- Default tile configurations (blue, green, yellow)
- Press Play in the Unity Editor
- The map will generate automatically with default settings
- Modify seed, width, height values in the UI
- Click "Generate Map" to create a new map
- Click "Random Seed" to generate a random seed value
You can import an image and generate maps based on its patterns:
- In the WFCManager inspector, find the "Image Import" section
- Assign a Texture2D to the "Source Image" field
- Set "Image Tile Size" (1 = per-pixel analysis, higher values = larger pattern tiles)
- In the Tileset Mode dropdown, select "From Image" (option 2)
- Click "Generate Map" to create a map based on the image patterns
Important: The source image must have proper import settings:
- Select the image in the Project window
- In the Inspector, configure these settings:
- Read/Write Enabled: ✓ Check this box
- Compression: None (to preserve exact colors)
- Filter Mode: Point (no filter) - prevents color bleeding
- Format: Automatic or RGBA32 for best color accuracy
- Click "Apply"
Note: If you're getting extra colors that aren't in your source image, make sure:
- Filter Mode is set to "Point (no filter)" - other modes blend pixels causing color variations
- Compression is set to "None" - compression can introduce color artifacts
- Your image is saved in a lossless format (PNG recommended, not JPEG)
- Create an empty GameObject
- Add the
ImageWFCExamplecomponent - Assign your source image (must be readable)
- Configure generation settings (output size, seed, tile size)
- Press Play and then press Spacebar to generate
The image analyzer:
- Scans the source image and identifies unique colors/tiles
- Analyzes adjacency patterns between neighboring tiles
- Builds a ruleset based on which colors appear next to each other
- Uses WFC to generate new maps that follow the same patterns
Example use cases:
- Import a small hand-drawn tile pattern to generate larger variations
- Analyze existing game maps to create similar procedural maps
- Use pixel art as input to generate similar-looking patterns
To use a custom tileset, select "Custom Tileset" mode and enter JSON:
{
"tiles": [
{
"tileName": "blue",
"tileColor": {"r": 0.2, "g": 0.6, "b": 0.86, "a": 1.0},
"allowedNeighbors": ["yellow", "red"]
},
{
"tileName": "green",
"tileColor": {"r": 0.18, "g": 0.8, "b": 0.44, "a": 1.0},
"allowedNeighbors": ["yellow", "red"]
},
{
"tileName": "yellow",
"tileColor": {"r": 0.95, "g": 0.77, "b": 0.06, "a": 1.0},
"allowedNeighbors": ["blue", "green", "red"]
},
{
"tileName": "red",
"tileColor": {"r": 0.91, "g": 0.3, "b": 0.24, "a": 1.0},
"allowedNeighbors": ["blue", "green", "yellow"]
}
]
}The Wave Function Collapse algorithm works by:
- Starting with all tiles in a superposition (all possibilities)
- Collapsing the cell with minimum entropy (fewest options)
- Propagating constraints to neighboring cells
- Repeating until the entire grid is collapsed
This implementation uses a seeded random number generator for reproducible results.
- Blue: Can be adjacent to blue and yellow tiles
- Green: Can be adjacent to green and yellow tiles
- Yellow: Can be adjacent to blue, green, and yellow tiles
This ensures that blue and green tiles never touch each other, while yellow acts as a separator.
MIT License