Its me, the2dguy! If you saw my recent post in #help-and-feedback:cool-creations, I showcased my 2D colored terrain generator, so I thought, why not make it open sourced!
perlin noise (docs) is a cool math algorithm used to produce natural appearing textures which is a Procedural generation algorithm.
For generating terrain, i first started by creating a canvas. The canvas acts as a grid which will have different cells (tiles). Each cell will have its own color. Using perlin noise, we’ll have really cool patterns which will be our terrain! I use os.time() to get randomseeds so you’ll never encounter the same terrain again!
I used a few constant variables:
Vector2 CellSize
- Size of each cell in pixels.
number CanvasHeight
- Height of the canvas (y)
number CanvasWidth
- Width of the canvas (x)
number Scale
- Scale is used to determine the size of terrain regions. The closer it is to 0, the bigger regions of terrain we get.
Using the above constant values, using a loop I place the cells in a uniform order, which will form a grid. I color these tiles along the way using perlin noise and seeds! (The cells are congruent squares)
Using the above method, a new terrain map is generated. The colors of each terrain type can also be customized as desired. The amount of land and water can also be customized by editing the source.
Module
I created a module for the same, you can find those below.
The testing place is uncopylocked here for reference:
Example
local Terrain = script.Parent.Terrain -- canvas Frame
local cellsize = Vector2.new(15,15) -- size of each cell
local scale = 0.02 -- for perlin noise pattern scale. the closer it is to 0, the larger the terrain land
local TerrainModule = require(path.to.module)
local newTerrain = TerrainModule.new(Terrain)
newTerrain:gen(cellsize, scale) -- generates the terrain
newTerrain:callback(function()
print("this is the callback function! this will be printed when the terrain has been generated!")
end)
Source
Thats all from me today.
Hopefully this helps some of you out, I’ll be back with another post regarding forces on a 2D plane soon. See ya next time.
(If you have any questions or feedback, you are free to share them in my DMs or the replies)