To create a simple terrain generator using Python and Matplotlib, we can use Perlin noise with parameters that define the variety of terrain and its smoothness. Here are the steps to follow:
- Import the necessary libraries at the beginning of the code:
import noise
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
- Define the shape, scale, octaves, persistence, and lacunarity of the terrain:
shape = (50, 50)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0
- Initialise a numpy array that will contain the values of our world, iterate through the empty array, and fill it with Perlin noise:
world = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
world[i][j] = noise.pnoise2(i / scale,
j / scale,
octaves = octaves,
persistence = persistence,
lacunarity = lacunarity,
repeatx = 1024,
repeaty = 1024,
base = 42)
- Plot the 2D terrain using Matplotlib:
plt.imshow(world, cmap = 'terrain')
plt.show()
- Initialise two more arrays that will contain the x-y coordinates of our world and plot it in 3D with Matplotlib:
lin_x = np.linspace(0, 1, shape[0], endpoint = False)
lin_y = np.linspace(0, 1, shape[1], endpoint = False)
x, y = np.meshgrid(lin_x, lin_y)
fig = plt.figure()
ax = fig.add_subplot(111, projection = "3d")
ax.plot_surface(x, y, world, cmap = 'terrain')
plt.show()
- Make an interactive plot using Plotly by converting the Matplotlib colormap to a Plotly-compatible one and create a 3D terrain model:
terrain_cmap = plt.cm.get_cmap('terrain')
def matplotlib_to_plotly(cmap, pl_entries):
h = 1.0 / (pl_entries - 1)
pl_colorscale = []
for k in range(pl_entries):
C = list(map(np.uint8, np.array(cmap(k * h)[:3]) * 255))
pl_colorscale.append([k * h, 'rgb' + str((C[0], C[1], C[2]))])
return pl_colorscale
terrain = matplotlib_to_plotly(terrain_cmap, 255)
fig = go.Figure(data = [go.Surface(colorscale = terrain, z = world)])
fig.update_layout(title = 'Random 3D Terrain')
go_offline.plot(fig, filename='3d-terrain-plotly.html', validate=True, auto_open=False)
- Here is the final code:
# Import the necessary libraries at the beginning of the code
import noise
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
# Define the shape, scale, octaves, persistence, and lacunarity of the terrain.
shape = (50, 50)
scale = 100.0
octaves = 6
persistence = 0.5
lacunarity = 2.0
# Initialise a numpy array that will contain the values of our world, iterate through the empty array, and fill it with Perlin noise.
world = np.zeros(shape)
for i in range(shape[0]):
for j in range(shape[1]):
world[i][j] = noise.pnoise2(i / scale,
j / scale,
octaves = octaves,
persistence = persistence,
lacunarity = lacunarity,
repeatx = 1024,
repeaty = 1024,
base = 42)
# Plot the 2D terrain using Matplotlib
plt.imshow(world, cmap = 'terrain')
plt.show()
# Initialise two more arrays that will contain the x-y coordinates of our world and plot it in 3D with Matplotlib
lin_x = np.linspace(0, 1, shape[0], endpoint = False)
lin_y = np.linspace(0, 1, shape[1], endpoint = False)
x, y = np.meshgrid(lin_x, lin_y)
fig = plt.figure()
ax = fig.add_subplot(111, projection = "3d")
ax.plot_surface(x, y, world, cmap = 'terrain')
plt.show()
# Make an interactive plot using Plotly by converting the Matplotlib colormap to a Plotly-compatible one and create a 3D terrain model
terrain_cmap = plt.cm.get_cmap('terrain')
def matplotlib_to_plotly(cmap, pl_entries):
h = 1.0 / (pl_entries - 1)
pl_colorscale = []
for k in range(pl_entries):
C = list(map(np.uint8, np.array(cmap(k * h)[:3]) * 255))
pl_colorscale.append([k * h, 'rgb' + str((C[0], C[1], C[2]))])
return pl_colorscale
terrain = matplotlib_to_plotly(terrain_cmap, 255)
fig = go.Figure(data = [go.Surface(colorscale = terrain, z = world)])
fig.update_layout(title = 'Random 3D Terrain')
go_offline.plot(fig, filename='3d-terrain-plotly.html', validate=True, auto_open=False)
Note that the above steps are just one possible implementation of a simple terrain generator using Python and Matplotlib. There are many other ways to achieve this, and the choice of implementation will depend on the specific requirements of the project.