Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

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

  1. User

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

  2. Assistant

    以下是一个实现了这些参数的柏林噪声生成器代码:

    import numpy as np
    from numpy.random import default_rng
    
    
    def perlin_noise_2d(shape, frequency=16, lacunarity=2, persistence=0.5, octaves=1, seed=None):
        """
        Generates 2D Perlin noise with the given parameters.
        :param shape: Shape of the output noise array.
        :param frequency: Initial frequency of the noise.
        :param lacunarity: Factor by which frequency is multiplied with each octave.
        :param persistence: Factor by which amplitude (strength) is multiplied with each octave.
        :param octaves: Number of octaves used for generating the noise.
        :param seed: Seed for the random number generator.
        """
        rng = default_rng(seed)
    
        # Generate random gradient vectors for each point on the grid
        gradients_shape = (shape[0]+1, shape[1]+1, 2)
        gradients = rng.standard_normal(gradients_shape)
    
        # Generate a grid of points of shape (shape[0], shape[1], 2) and normalize it
        points = np.stack(np.meshgrid(np.arange(shape[0]), np.arange(shape[1])), axis=-1)
        points = points.astype(np.float32) / frequency
    
        # Generate a grid of vectors pointing from each gradient point to the input points
        vectors = np.zeros((shape[0]+1, shape[1]+1, shape[0], shape[1], 2), dtype=np.float32)
        vectors[..., 0] = np.arange(shape[0]+1).reshape((-1, 1, 1, 1))
        vectors[..., 1] = np.arange(shape[1]+1).reshape((1, -1, 1, 1))
        vectors -= points.reshape((1, 1, *points.shape))
        vectors /= frequency
    
        # Generate a weight grid for each octave
        weights = persistence ** np.arange(octaves)
    
        # Generate the noise grid by summing the values of each octave
        noise = np.zeros(shape, dtype=np.float32)
        for octave in range(octaves):
            # Compute the dot product between each gradient vector and the vector pointing from each gradient point to the input points
            dots = np.sum(gradients[:-1, :-1] * vectors, axis=-1)
    
            # Interpolate the values of each dot product using the smoothstep function
            interpolate = lambda t: 6 * t**5 - 15 * t**4 + 10 * t**3
            x, y = np.meshgrid(interpolate(points[..., 0] % 1),
                               interpolate(points[..., 1] % 1),
                               indexing='ij')
            dots = dots.transpose((2, 3, 0, 1))
            values = (dots[..., :-1, :-1] * (1-x) * (1-y) +
                      dots[..., :-1, 1:] * (1-x) * y +
                      dots[..., 1:, :-1] * x * (1-y) +
                      dots[..., 1:, 1:] * x * y)
    
            # Add the weighted values to the noise grid
            noise += weights[octave] * values
    
            # Update the frequency and amplitude (strength) for the next octave
            frequency *= lacunarity
            weights[octave] *= persistence
    
            # Update the grid of input points and the grid of vectors pointing from each gradient point to the input points
            points *= lacunarity
            vectors *= lacunarity
    
        # Normalize the noise grid to range from 0 to 1
        noise -= noise.min()
        noise /= noise.max()
    
        return noise
    

    可以使用以下示例代码调用该函数以生成柏林噪声:

    import matplotlib.pyplot as plt
    
    shape = (512, 512)
    noise = perlin_noise_2d(shape, frequency=16, lacunarity=2, persistence=0.5, octaves=4, seed=42)
    plt.imshow(noise, cmap='gray')
    plt.show()
    

    此代码将生成一张512×512的灰度图像,其中灰度值表示每个像素的噪声密度。您可以根据需要调整Frequency、Lacunarity、Persistence、Octaves和Seed参数以生成不同的噪声图像。

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