Giter Club home page Giter Club logo

world_generator's Introduction

World-generator

A set of functions and classes for generating Perlin noise and images of the world.

Installation

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 .

Quickstart

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:

An example of the program execution result is above

Creating an image of the world

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

For those who don't want to bother

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.

Full control of generation

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:

Noise with large pixels for perlin noise Noise with average pixels for perlin noise Small pixel noise for perlin noise Noise with very small pixels for 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 as height) or temperature/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()

Additional functions

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 or gradient
image = WorldGenerator.perlin_noise(512, steps=[16, 32, 64])

cv2.imshow("result", image)
cv2.waitKey()

world_generator's People

Contributors

alexk-1 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.