This is collection of precedural generation scripts intended to be used as as a base for 2D tile-based Unity games. It makes use of Unity's Job system, which allows for Safe multithreaded code, and Unity's Burst compiler which translates from IL/. NET bytecode to highly optimized native code using LLVM. The short version of what this does is as follows:
- Two simplex noise textures are generated using Burst compiled Jobs.
- Those simplex noise textures undergo multiple image filtering techniques depending on user specifications.
- A separate Burst job then references one simplex texture to get an x coordinate, and the other to get a y coordinate.
- That x and y coordinate maps to a texture (pix[x,y]) that defines the output pixel color.
in order to use these scripts, you must have the follorwing packages installed in your project:
- Editor Coroutines
- Mathematics
- Collections
- Burst
Generated Image | Resulting World Generation |
Sparse Forest Biome | Dense Forest Biome |
Your first order of business will be to add ProceduralIslandGenerator.cs as a component to a gameobject that has an Image component and is parented to a valid canvas. From there you will see an object field on the ProcerualIslandGenerator that takes a Texture2D reference. Add the Island Generator Moisture-Height Map.png to your project as a Texture2D and assign it as the ProcerualIslandGenerator's Moisture Height Graph variable.
Next, you must create a new Biome Asset. In the project Hierarchy (in assets), right-click Create->Biome. The Biome has a default floor tile, which will be placed by the ProceduralIslandGenerator before populating the biome with Foliage and prefabs. It has a list of floor tiles which are considered valid floor tiles for the Biome. It also has a list of prefab objects which can be spawned into the biome, each with their own set of tile conditions and spawn chances. You wouldn't want a fish tile to spawn on land, for instance, if water and land were both found in the biome. The Biome Foliage represents a list of tiles that can replace the default floor tile or be rendered to a higher tilemap layer to add variety.
You will then map each pixel color found in the height moisture graph to a biome of your choice/ creation. If a color is not mapped to a biome, the assigned default biome will be placed. If no default biome is assigned, the tiles will be null (unassigned). You may experiment with different parameters for the height and moisture noise textures used to generate the island, and you will find interesting and varied end-results. If you wish to save the noise textures use for the island, assign a valid filename and filepath in the noise parameters, and check the bool to Save the image.
The heightmap | The moisture map |
When you are ready, click the Generate Island Texture Button. If you have elected to save the noise textures, they will be saved at this time. Then, you can place the tiles and the gameobjects in the scene referencing the texture by pressing "Place Island."
- The tilemaps used by the procedural island generator must be sorted by their tilemap renderer's sorting order, in ascending order.
- Do not run the Island Generation script in quick succession. I haven't added protection against spam clicking.
- Please enjoy this code to the fullest!