Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fdbesanto2/32e5fc4032884c3f66e1ba5ba8f503ea to your computer and use it in GitHub Desktop.
Save fdbesanto2/32e5fc4032884c3f66e1ba5ba8f503ea to your computer and use it in GitHub Desktop.
A program to generate 3d terrain in python
import random
import sys
import noise
import numpy as np
import matplotlib.pyplot
if "--seed" in sys.argv:
try:
seed = int(sys.argv[sys.argv.index("--seed")+1])
except IndexError:
sys.stdout.write(f"Invalid seed")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid seed")
sys.stdout.flush()
sys.exit(1)
else:
seed=int(random.random()*10000)
if "--scale" in sys.argv:
try:
scale = int(sys.argv[sys.argv.index("--scale") + 1])
except IndexError:
sys.stdout.write(f"Invalid scale")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid scale")
sys.stdout.flush()
sys.exit(1)
else:
scale = 1000
if "--shape" in sys.argv:
try:
shape = (int(sys.argv[sys.argv.index("--shape") + 1]), int(sys.argv[sys.argv.index("--shape") + 2]))
except IndexError:
sys.stdout.write(f"Invalid shape")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid shape")
sys.stdout.flush()
sys.exit(1)
else:
shape = (int(scale), int(scale))
if "--octaves" in sys.argv:
try:
shape = int(sys.argv[sys.argv.index("--octaves") + 1])
except IndexError:
sys.stdout.write(f"Invalid octaves")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid octaves")
sys.stdout.flush()
sys.exit(1)
else:
octaves = 6
if "--persistence" in sys.argv:
try:
shape = int(sys.argv[sys.argv.index("--persistence") + 1])
except IndexError:
sys.stdout.write(f"Invalid persistence")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid persistence")
sys.stdout.flush()
sys.exit(1)
else:
persistence = 0.5
if "--lacunarity" in sys.argv:
try:
shape = int(sys.argv[sys.argv.index("--lacunarity") + 1])
except IndexError:
sys.stdout.write(f"Invalid lacunarity")
sys.stdout.flush()
sys.exit(1)
except ValueError:
sys.stdout.write(f"Invalid lacunarity")
sys.stdout.flush()
sys.exit(1)
else:
lacunarity = 2.0
sys.stdout.write(f"Using seed {seed}")
sys.stdout.write("\n")
sys.stdout.flush()
world = np.zeros(shape)
for i in range(shape[0]):
sys.stdout.write(f"\rGenerating map ({str(round((i / shape[0]) * 100, 1))}% Done)")
sys.stdout.flush()
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=seed)
sys.stdout.write(f"\rGenerating map (100% Done)")
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)
matplotlib.rcParams['toolbar'] = 'None'
fig = matplotlib.pyplot.figure(f"3D terrain generator (seed: {seed})")
ax = fig.add_subplot(111, projection="3d")
ax.plot_surface(x, y, world, cmap='terrain')
plotinfo = f"""Terrain Info
seed: {seed}
scale: {scale}
shape: {shape[0]}x{shape[1]}
octaves: {octaves}
persistence: {persistence}
lacunarity: {lacunarity}"""
ax.text2D(-.25, 1, plotinfo, ha='left', va='top', transform=ax.transAxes)
ax.set_axis_off()
matplotlib.pyplot.grid(False)
matplotlib.pyplot.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment