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

我在尝试用柏林噪声制作一个被海水包围的岛屿,我的做法是先生成一个柏林噪声,然后将小于0的值设为海洋,大于0的值设为陆地,但这种判断方式会导致陆地的边缘非常的规整,用python怎…

  1. User

    我在尝试用柏林噪声制作一个被海水包围的岛屿,我的做法是先生成一个柏林噪声,然后将小于0的值设为海洋,大于0的值设为陆地,但这种判断方式会导致陆地的边缘非常的规整,用python怎么解决呢,不要使用太高级的算法

  2. Assistant

    以下是一种简单的方法,可以在不使用高级算法的情况下实现更自然的岛屿边缘:

    1. 生成柏林噪声。

    2. 对于每个像素,计算与周围像素的平均值,并将这个平均值与像素本身的值进行比较。如果像素的值小于平均值,则将其设置为海洋,否则设置为陆地。

    3. 对于边缘像素,可以将其与周围所有像素的平均值进行比较,如果小于平均值,则设置为海洋,否则设置为陆地。

    下面是Python代码实现:

    import random
    import numpy as np
    import matplotlib.pyplot as plt
    
    # 生成柏林噪声
    def generate_noise(width, height, scale):
        noise = np.zeros((height, width))
        for y in range(height):
            for x in range(width):
                noise[y][x] = random.uniform(0, 1)
    
        # 插值
        for octave in range(scale):
            octave_noise = np.zeros((height, width))
    
            freq = 2 ** octave
            for y in range(height):
                for x in range(width):
                    sample_x = int(x / freq)
                    sample_y = int(y / freq)
                    interpolation_x = x / freq - sample_x
                    interpolation_y = y / freq - sample_y
    
                    sample_values = [
                        [get_noise_sample(noise, sample_x, sample_y), get_noise_sample(noise, sample_x + 1, sample_y)],
                        [get_noise_sample(noise, sample_x, sample_y + 1), get_noise_sample(noise, sample_x + 1, sample_y + 1)]
                    ]
    
                    octave_noise[y][x] = interpolate_bilinear(sample_values, interpolation_x, interpolation_y)
    
            noise += octave_noise
    
        return noise
    
    # 获取柏林噪声采样值
    def get_noise_sample(noise, x, y):
        width = len(noise[0])
        height = len(noise)
    
        x = x % width
        y = y % height
    
        return noise[y][x]
    
    # 双线性插值
    def interpolate_bilinear(sample_values, x, y):
        top = (1 - x) * sample_values[0][0] + x * sample_values[0][1]
        bottom = (1 - x) * sample_values[1][0] + x * sample_values[1][1]
        return (1 - y) * top + y * bottom
    
    # 生成岛屿
    def generate_island(width, height, scale):
        # 生成柏林噪声
        noise = generate_noise(width, height, scale)
    
        island = np.zeros((height, width))
        for y in range(height):
            for x in range(width):
                # 计算平均值
                average = compute_average(noise, x, y)
    
                if noise[y][x] < average:
                    island[y][x] = 0
                else:
                    island[y][x] = 1
    
        # 处理边缘像素
        for y in range(height):
            for x in range(width):
                if x == 0 or x == width - 1 or y == 0 or y == height - 1:
                    average = compute_average(noise, x, y)
                    if noise[y][x] < average:
                        island[y][x] = 0
                    else:
                        island[y][x] = 1
    
        return island
    
    # 计算平均值
    def compute_average(noise, x, y):
        width = len(noise[0])
        height = len(noise)
    
        neighbors = []
        for i in range(-1, 2):
            for j in range(-1, 2):
                if i == 0 and j == 0:
                    continue
                if x + i < 0 or x + i >= width or y + j < 0 or y + j >= height:
                    continue
                neighbors.append(noise[y+j][x+i])
    
        return sum(neighbors) / len(neighbors)
    
    # 显示图像
    def display_image(image):
        plt.imshow(image, cmap='gray')
        plt.show()
    
    # 生成并显示岛屿
    island = generate_island(512, 512, 6)
    display_image(island)
    

    这样生成的岛屿边缘会更加自然。

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