Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声

  1. User

    用python实现一个可传参frequency、lacunarity、persistence、octaves、seed的2D柏林噪声

  2. Assistant

    以下是一个使用Python实现2D柏林噪声的例子,其中包括了可传参的frequency、lacunarity、persistence、octaves和seed参数:

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    def generate_noise(width, height, frequency, lacunarity, persistence, octaves, seed=0):
        np.random.seed(seed)
    
        # Initialize noise array
        noise = np.zeros((height, width))
    
        # Loop through octaves
        for i in range(octaves):
            # Calculate current octave parameters
            freq = frequency * lacunarity ** i
            ampl = persistence ** i
    
            # Generate octave noise
            octave = np.random.rand(height, width)
    
            # Apply frequency and amplitude to octave noise
            x = np.linspace(0, 1, width, endpoint=False)
            y = np.linspace(0, 1, height, endpoint=False)
            x, y = np.meshgrid(x, y)
            octave = octave + (np.sin((x+0.1) * freq*10 * np.pi) + np.sin((y+0.1) * freq*10 * np.pi)) / 2 * ampl
    
            # Add octave noise to overall noise
            noise += octave
    
        # Normalize noise to range [0, 1]
        noise = (noise - noise.min()) / (noise.max() - noise.min())
    
        return noise
    
    
    # Example usage
    width = 200
    height = 200
    frequency = 1
    lacunarity = 2
    persistence = 0.5
    octaves = 6
    seed = 0
    
    noise = generate_noise(width, height, frequency, lacunarity, persistence, octaves, seed)
    
    plt.imshow(noise, cmap='gray')
    plt.show()
    

    在例子中,我们先定义了一个名为generate_noise的函数来生成噪声。该函数使用numpy库生成一个指定大小的0数组,然后通过循环产生指定层数的特定频率和幅度的随机噪声,最后将所有层的噪声加在一起并将结果规范化到0到1的范围内。我们还通过matplotlib库的imshow函数将结果可视化。

    我们可以运行该程序并使用不同的frequency、lacunarity、persistence、octaves和seed参数来生成不同的噪声效果。

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.