fimama.perlin

 1import numpy as np
 2from noise import pnoise2
 3
 4from .configuration import PerlinParameters
 5
 6
 7def perlin_map(
 8    width: int,
 9    height: int,
10    params: PerlinParameters
11) -> np.ndarray:
12    """
13    Generate a heightmap with Perlin noise.
14
15    Parameters
16    ----------
17    width : int, optional
18        Width of the heightmap in cells.
19    height : int, optional
20        Height of the heightmap in cells.
21    params : PerlinParameters
22        Parameters for generating the Perlin noise.
23
24    Returns
25    -------
26    np.ndarray
27        Normalised heightmap. The values will be in the range [0.0, 1.0].
28    """
29    terrain = np.zeros((height, width))
30
31    # TODO: run this in parallel
32    for i in range(height):
33        for j in range(width):
34            terrain[i][j] = pnoise2(
35                i / params.scale,
36                j / params.scale,
37                octaves=params.octaves,
38                persistence=params.persistence,
39                lacunarity=params.lacunarity,
40                repeatx=width,
41                repeaty=height,
42                base=params.base
43            )
44
45    # Normalize terrain values to 0–1
46    normalized_terrain = terrain - terrain.min()
47    normalized_terrain = normalized_terrain / (terrain.max() - terrain.min())
48
49    return normalized_terrain
def perlin_map( width: int, height: int, params: fimama.configuration.PerlinParameters) -> numpy.ndarray:
 8def perlin_map(
 9    width: int,
10    height: int,
11    params: PerlinParameters
12) -> np.ndarray:
13    """
14    Generate a heightmap with Perlin noise.
15
16    Parameters
17    ----------
18    width : int, optional
19        Width of the heightmap in cells.
20    height : int, optional
21        Height of the heightmap in cells.
22    params : PerlinParameters
23        Parameters for generating the Perlin noise.
24
25    Returns
26    -------
27    np.ndarray
28        Normalised heightmap. The values will be in the range [0.0, 1.0].
29    """
30    terrain = np.zeros((height, width))
31
32    # TODO: run this in parallel
33    for i in range(height):
34        for j in range(width):
35            terrain[i][j] = pnoise2(
36                i / params.scale,
37                j / params.scale,
38                octaves=params.octaves,
39                persistence=params.persistence,
40                lacunarity=params.lacunarity,
41                repeatx=width,
42                repeaty=height,
43                base=params.base
44            )
45
46    # Normalize terrain values to 0–1
47    normalized_terrain = terrain - terrain.min()
48    normalized_terrain = normalized_terrain / (terrain.max() - terrain.min())
49
50    return normalized_terrain

Generate a heightmap with Perlin noise.

Parameters
  • width (int, optional): Width of the heightmap in cells.
  • height (int, optional): Height of the heightmap in cells.
  • params (PerlinParameters): Parameters for generating the Perlin noise.
Returns
  • np.ndarray: Normalised heightmap. The values will be in the range [0.0, 1.0].