How to generate 2d tilemap procedurally programatically?

So lets say there are bunch of basic pixel drawn each specific one space occupied tiles. Made tile palette and each tile sprites by traditional official way.

Then instead of drawing manually this tile one by one by human,

how can I make automated system that will randomly choose and draw tiles properly and make it looks natural and complete entire 2d tilemap?

Are there code or command set unity provides that can access and control, allocate each specific tiles to game scene tilemap?

Tilemap.SetTile() is all you need for this… everything else is just the procgen magic you bring to it.

Tiles/Tilemaps were the “OG Procgen,” way back in the 1970s/1980s games. We called 'em “charsets” or “character generators.”

Here’s a Unity example:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

// @kurtdekker - cheesey-simple procgen with tilemaps
// Fills with dirt, then sprinkles rocks across

public class Tilemap_FillDirtAndRocks : MonoBehaviour
{
    [Header( "Where this is all going.")]
    public Tilemap TheMap;

    [Header("Possible dirt Tiles (drag 'em in)")]
    public TileBase[] TileDirts;
    [Header("Possible rock Tiles (drag 'em in)")]
    public TileBase[] TileRocks;

    [Header("Tile dimensions, either side of 0,0")]
    public int Across = 32;
    public int Down = 24;

    IEnumerator Start()
    {
        // give us time to get ready to see it
        yield return new WaitForSeconds(2.0f);

        // fill with dirt
        for (int j = -Down; j <= +Down; j++)
        {
            for (int i = -Across; i <= +Across; i++)
            {
                Vector3Int Position = new Vector3Int(i, j, 0);

                var chosenDir = TileDirts[Random.Range( 0, TileDirts.Length)];

                TheMap.SetTile(position: Position, tile: chosenDir);
            }
        }

        // give us time to admire it
        yield return new WaitForSeconds(2.0f);

        // sprinkle rocks across
        for (int i = -Across; i <= +Across; i++)
        {
            int j = Random.Range(-Down, +Down + 1);
            {
                Vector3Int Position = new Vector3Int(i, j, 0);

                var chosenRock = TileRocks[Random.Range(0, TileRocks.Length)];

                TheMap.SetTile(position: Position, tile: chosenRock);
            }
        }
    }
}

I didn’t have a rock handy so I used a yellow smiley. You could use two tilemaps stacked on top of each other, or dozens stacked on top of each other. You live in the phattest of times for tilemaps… it’s never been easier!!


8947683--1227891--Screen Shot 2023-04-13 at 7.18.13 PM.png 8947683--1227894--Screen Shot 2023-04-13 at 7.18.00 PM.png

Ok, thanks, it seems work. But if I have some various graphics on base tile slices, just Random choosing result in improper looking when they combined be neighbored.

So I think here the AI should judge each tile graphic’s sprite’s 4 directional (up, down, left, right) border’s pixel values (color value? or whatever) and only choose similar ones (or exact same one?) when select adjacent tile.

How can we make this function to AI?

Then, if this system strictly applied, then only similar border graphic tiles will be selected, so then pre made graphics should be united as one border graphics, or AI should have another function that at some times it should start with completely new and original tile? Then when? and what if it looks unnatural… problem complex…

But first, how can access to each 1 tile’s 4 directional borders pixel graphic value?

That is procgen in a nutshell… “problem complex”

I’ve spent a lifetime with procgen in one form or another and I don’t consider myself any kind of an authority.

But a lot of my games use procgen and are generating content with it right now as we speak.

Apple Appstore:
https://itunes.apple.com/us/developer/kurt-dekker/id680019078

Google Play Store:
https://play.google.com/store/apps/developer?id=Kurt+Dekker

Itch.io:
https://kurtdekker.itch.io/

Unity has RuleTiles you can use for smoothing but to do anything truly spectacular, you need spectacular algorithms and interesting approaches, and of course well-planned artwork perfectly matched to optimize and synergize with the code and algorithms you choose.

What I posted above was literally five minutes of effort.

Many mobile games! how to monetize them by the way? Add ads or in-game purchase items?

I just searched procgen word. So it seems randomly generated map game?
I don’t think it’s enough to leave a meaningful impression on users with only the length and width of the map, and that much randomness.

Oh certainly not what I posted. That was an example. As I said it’s up to you to bring the magic.

For examples of games that are procgen, look at Minecraft, Valheim, No Man’s Sky, Spelunky, Binding of Isaac, etc. I mean there are literally millions of games written that use procgen to make very interesting content.

I don’t monetize them, that would be too much hassle. :slight_smile:

I made these games for learning and hobby and just for the fun of playing them.

I’m particularly proud of my procgen lunar surfaces:

https://www.youtube.com/watch?v=34wbtTIWdDQ

Plays really well on a tablet, two thumbs, never the same world!

Yes specially minecraft I know well and played, but its hard to know and code the procgen map made system.

The logic, form, and code actually implemented in each game are very different, to group them into one category called procgen, so in the end, the important thing is how and what code to build a random environment.