Anon
Anon

Reputation: 19

Why do most seeds result in the perlin noise become vertical lines?

I'm trying my hand at perlin noise so that i could make dnd maps, but my python project seems to always come up with either perfectly vertical lines or noise that comes out extremely vertical.Typical generation Typical half generation


from asyncio import WindowsProactorEventLoopPolicy
import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2
import random
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

def update_plot():
    shuffle_and_plot()

def generate_perlin_map(width, height, scaling, octaves=6, persistence=0.6, lacunarity=5, threshold=0.7, seed=None):
    """Generates a Perlin noise heat map with thresholding and an optional random seed."""
    if seed is not None:
        random.seed(seed)
    
    noise_map = np.zeros((height, width))
    for y in range(height):
        for x in range(width):
            noise_map[y][x] = pnoise2(x / width, y / height, octaves=10, persistence=.8, lacunarity=2, base=seed)
    
    # Normalize values to range 0-1
    noise_map = (noise_map - noise_map.min()) / (noise_map.max() - noise_map.min())
    
    # Apply thresholding
    #noise_map = np.where(noise_map > threshold, .5, 0)
    return noise_map

def plot_heatmap(noise_map):
    """Displays the generated heat map."""
    plt.imshow(noise_map, cmap='gray', interpolation='nearest')
    plt.title("Perlin Noise Heat Map")
    plt.show()

def shuffle_and_plot():
    """Regenerates the noise map with a new seed and plots it."""
    global noise_map
    new_seed = random.randint(0, 10000)  # Randomize seed for different patterns
    noise_map = generate_perlin_map(width = 1000, height = 1000, scaling = .02, seed=new_seed)
    plot_heatmap(noise_map)

root = tk.Tk()
root.title("Perlin Noise Generator")
button = tk.Button(root, text="Shuffle", command=update_plot)
button.pack()
root.mainloop()

I have tried increasing and decreasing the width, height, octaves, persistence and lacunarity, as well as adding a scaling variable for the width and heights, but it always results in the vertical lines (at least on most seeds).

Upvotes: 1

Views: 45

Answers (1)

Christian Karcher
Christian Karcher

Reputation: 3721

You stepwidth of x / width with a width of 10000 is causing this problem, which is "too zoomed into the perlin noise". You are basically seeing just a part of a hill instead of the whole landscape.

Adding an additional scale factor helps to get into a good "perlin distance".

enter image description here

Here's a minimal example to get you going:

import numpy as np
import matplotlib.pyplot as plt
from noise import pnoise2

# vary this scale to change the "zoom factor"
scale = 10

n_y = 1000
n_x = 1000

perlin_map = np.zeros((n_x, n_y))

max_n = max(n_y, n_x)
for i in range(n_y):
    for j in range(n_x):
        perlin_map[i, j] = pnoise2(
            j / max_n * scale,
            i / max_n * scale,
        )
plt.imshow(perlin_map)
plt.show()

Upvotes: 0

Related Questions