You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建随机图案渐变图?已实现圆形单色渐变图,求技术方案

Creating a Random Pattern Gradient for Procedural Generation

Hey there! Nice work getting that circular gradient up and running—let's walk through how to create a random pattern gradient that has that smooth, natural-looking transition you're probably after.

For procedural generation, Perlin or Simplex Noise is the go-to for creating coherent random gradients (way better than pure random pixels, which look messy). Below are two approaches: one using a dedicated noise library for polished results, and a simpler numpy-based method if you want to avoid extra dependencies.

Perlin noise generates continuous, organic-looking random patterns that are perfect for gradients. We'll use the perlin-noise library for this.

First, install the dependency if you haven't already:

pip install perlin-noise

Then, add this function to your code (aligned with your existing circular gradient structure):

import math
import numpy as np
from perlin_noise import PerlinNoise

def create_random_pattern_gradient(self, world, octaves=3, seed=None):
    # Initialize the Perlin noise generator
    noise = PerlinNoise(octaves=octaves, seed=seed)
    
    height, width = world.shape[0], world.shape[1]
    random_grad = np.zeros_like(world, dtype=np.float64)
    
    for y in range(height):
        for x in range(width):
            # Normalize coordinates to 0-1 to keep noise scaling consistent across map sizes
            norm_x = x / width
            norm_y = y / height
            
            # Get noise value (ranges roughly from -1 to 1)
            noise_val = noise([norm_x, norm_y])
            
            # Map the noise value to match the scale of your circular gradient
            # This ensures you can use it interchangeably with your existing gradient
            max_dist = math.sqrt((width // 2)**2 + (height // 2)**2)
            random_grad[y][x] = (noise_val + 1) * (max_dist / 2)  # Converts -1→1 to 0→max_dist
    
    # If your world array uses integer types, cast the gradient to match
    # random_grad = random_grad.astype(world.dtype)
    
    return random_grad

Key Parameters Explained:

  • octaves: Controls the level of detail in the gradient. Higher values mean more small-scale variations; lower values create smoother, broader patterns.
  • seed: Optional. Set this to a fixed number if you want to reproduce the exact same random gradient later (great for debugging or consistent world generation).
  • Normalization: By scaling x/y to 0-1, the noise pattern stays proportional no matter how big or small your map is.

Method 2: Simple Blurred Random Noise (No Extra Dependencies)

If you don't want to install a new library, you can generate random noise and smooth it with a Gaussian blur to create a gradient effect.

import math
import numpy as np
from scipy.ndimage import gaussian_filter

def create_simple_random_gradient(self, world, blur_kernel_size=15):
    height, width = world.shape[0], world.shape[1]
    
    # Generate raw random noise (values 0-1)
    random_noise = np.random.rand(height, width)
    
    # Apply Gaussian blur to smooth the noise into a gradient
    blurred_noise = gaussian_filter(random_noise, sigma=blur_kernel_size)
    
    # Map to the same scale as your circular gradient
    max_dist = math.sqrt((width // 2)**2 + (height // 2)**2)
    random_grad = blurred_noise * max_dist
    
    # Cast to match world's data type if needed
    # random_grad = random_grad.astype(world.dtype)
    
    return random_grad

Notes:

  • blur_kernel_size: Adjust this to control smoothness. Larger values create softer gradients; smaller values keep more of the original random texture.
  • You'll need scipy for the Gaussian filter—if you don't have it, install it with pip install scipy.

Bonus: Combine Gradients

For even more interesting results, mix your circular gradient with the random one:

circular_grad = self.create_circular_gradient(world)
random_grad = self.create_random_pattern_gradient(world)
combined_grad = circular_grad * 0.7 + random_grad * 0.3  # 70% circular, 30% random

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:46:57