0

So I have been working on some procedural generating and i managed to create a circular monochrome gradient map which i use to generate other maps.

def create_circular_gradient(self, world):
    center_x, center_y = self.mapSize[1] // 2, self.mapSize[0] // 2
    circle_grad = np.zeros_like(world)

    for y in range(world.shape[0]):
        for x in range(world.shape[1]):
            distx = abs(x - center_x)
            disty = abs(y - center_y)
            dist = math.sqrt(distx * distx + disty * disty)
            circle_grad[y][x] = dist

    # get it between -1 and 1
    max_grad = np.max(circle_grad)
    circle_grad = circle_grad / max_grad
    circle_grad -= 0.5
    circle_grad *= 2.0
    circle_grad = -circle_grad

    # shrink gradient
    for y in range(world.shape[0]):
        for x in range(world.shape[1]):
            if circle_grad[y][x] > 0:
                circle_grad[y][x] *= 20

    # get it between 0 and 1
    max_grad = np.max(circle_grad)
    circle_grad = circle_grad / max_grad
    grad_world = self.apply_gradient_noise(world, circle_grad)
    return grad_world

Now my question is, how exactly would i need to modify this to create a swirly gradient in a random way? and even maybe a box or a diverse linear gradients?

Note that world is a randomly generated 2d array (and can be any shape like 100x100 or even 100x50 but of course i use it a whole lot bigger) and to the circle gradient, I add a perlin noise to randomize the generation. The code that randomizes it will work for any map but i'm not sure where to start creating different shapes of gradient.

Edit Ignore the magic numbers, that was the only way i found how to create a circular monochrome gradient

Nick
  • 545
  • 12
  • 31
  • Your question is way too vague, what exactly is a "swirly gradient" anyway? Besides that, "random pattern" sounds like an oxymoron. There's also quite a few magic numbers in your code. I think the first thing you should do is define the input and desired outputs in fairly precise terms. Hmmm, maybe that's what you're asking how to do... – martineau May 03 '18 at 15:23
  • similar to this but no color is needed https://i.stack.imgur.com/pYHku.png – Nick May 03 '18 at 15:34
  • 1
    your `shrink gradient` block can be rewritten `circle_grad[circle_grad > 0] *= 20`. It's faster (no loop) and more readable for people used to numpy – P. Camilleri May 03 '18 at 16:27
  • I think your question boils down to how to create a gradient map that has an arbitrary shape or shapes in it. One way to do that would be to "render" the shape(s) onto the gradient map. There are a number of well-know ways of doing that including "scanline-conversion" of 2D polygons. The way to do it depends on how these "shapes" are defined. For example, another way to create them would be to apply a user-supplied function to every `[y][x]` position in it (which would handle what your current code is doing). – martineau May 03 '18 at 16:42

0 Answers0