A set of functions and classes for generating Perlin noise and images of the world.
Before using this code, you need to install the cv2 library, to do this, enter this command in the shell:
pip install -v "opencv-python==4.9.0.80"
After that, you can copy the code from the repository file WorldGenerator.py
.
You can use this code to generate and display the world:
import WorldGenerator
import cv2
img = WorldGenerator.World(512).standard_generation() #Generating an RGB image
cv2.imshow("result", img[:, :, ::-1]) # Converting RGB image to BGR and displaying using cv2
cv2.waitKey()
As a result, the code outputs a similar image:
To create an image of the world, you need to use the World
class.
Parameters of the World
class:
- size: int - the size of the image generated by the class
- smooth: int - image smoothing level; standard 50
- height: int - the height of the world; if 0, water and land are approximately equal, the lower the height the more water, the higher the height the less water; standard 0
- temperature : int - the temperature of the world; the higher the temperature, the more deserts and less water and glaciers, the lower the temperature, the fewer deserts and more glaciers; standard 0
- no_rivers: bool - excludes rivers from image generation; standard False
- no_mountains: bool - excludes mountains from image generation; standard False
- no_glaciers: bool - excludes glaciers from image generation; standard False
- no_deserts: bool - excludes deserts from image generation; standard False
After setting up the world object, you can generate an image based on the data that you entered when creating the world object.
The easiest way to do this is to run the standard_generation
function of the World
class.
This function creates 3 Perlin noises and based on them creates a color RGB image.
world = World(512, height=-1, temperature=-1) # creating a world object
image = world.standard_generation() # image generation
After that, you can display the generated image, for example, using the cv2 library (in this case, you will need to convert the image to BGR format, since cv2 works with it) or matplotlib.
In fact, the standard_generation
function is just a set of make_layer
and join_layers
functions,
which will be described in detail later.
If only the standard_generation
function is not enough for you and you want
to fully control the image generation process, you need to use
the make_layer
, join_layers
and coloring_map
functions.
Perlin noise consists of several layers of noise, each of which has different levels of "pixelation". Examples of images that make up Perlin noise:
To get Perlin noise, you need to combine several similar noises.
To create a single layer of perlin noise, use the make_layer
function of the World
class. This function accepts parameters:
- step: int - the number of pixels in one row of noise; the smaller the number, the more noise will be similar to the first noise in the images above; the larger the step, the smaller the pixels and the more noise will be similar to the last noise from the images above
- layer_type: str - the type of noise generated; it can be
height
/h
(normal noise, as in the images),mountains rivers
/m r
(the same asheight
) ortemperature
/t
(pixels in the middle are lighter than above or below to center the temperature it was high)
The make_layer functions are run one after the other to get all the necessary noise.
After this is done, you need to combine these noises using the join_layers
function of the World
class.
This function takes only one layers_type parameter (it takes the same values as the make_layer
function) and,
depending on this parameter, saves the resulting Perlin noise to a specific list.
Creating and displaying Perlin noise for temperature:
world = WorldGenerator.World(512)
world.make_layer(32, "t") # creating three layers for Perlin noise
world.make_layer(64, "t")
world.make_layer(128, "t")
world.join_layers("t") # combining layers into Perlin noise
cv2.imshow("result", world.temperature_noise) # the temperature noise is stored in the temperature_noise
cv2.waitKey()
In the same way, Perlin noises are created for heights and rivers with mountains.
When all the necessary Perlin noises are created, you need to combine them into a color image using
the coloring_map
function of the World
class. This function does not accept parameters and
returns a single image of the world.
world = WorldGenerator.World(512)
world.make_layer(4, "h") # creating height noise
world.make_layer(16, "h")
world.make_layer(32, "h")
world.make_layer(64, "h")
world.make_layer(128, "h")
world.join_layers("h")
world.make_layer(32, "t") # creating temperature noise
world.make_layer(64, "t")
world.make_layer(128, "t")
world.join_layers("t")
world.make_layer(16, "m r") # creating rivers and mountains noise
world.make_layer(32, "m r")
world.make_layer(64, "m r")
world.make_layer(128, "m r")
world.join_layers("m r")
image = world.coloring_map() # combining noise into a color image
cv2.imshow("result", image[:, :, ::-1]) # displaying the result
cv2.waitKey()
There are also several functions outside the World
class: noise_generation
and perlin_noise
.
The noise_generation
function generates one layer of perlin noise and returns it. Accepts the same parameters
as World.make_layer
(instead of layer_type in this generation_type function) with another one - additional
(added to all noise pixels). The generation_type parameter can be standard
(as height
) or
gradient
(as temperature
).
image = WorldGenerator.noise_generation(512, 64, 0, "standard")
cv2.imshow("result", image)
cv2.waitKey()
Also, if you only need Perlin noise, you can use the perlin_noise
function.
It accepts parameters:
- size: int - the size of the generated image
- smooth: int - noise smoothing level; standard 50
- steps: list - a list of step parameters for noise layers, also controls the number of layers in the noise;
standard
[4, 16, 32, 64, 128]
- generation_type: str - the type of noise generated; can be
standard
orgradient
image = WorldGenerator.perlin_noise(512, steps=[16, 32, 64])
cv2.imshow("result", image)
cv2.waitKey()