2

I'm trying to figure out how I can - or at least where I can read more about it - create a circle / square gradient and store the data in a 2d array.

My goal is to use the gradient with simplex-noise to create a procedural island by subtracting the gradient from the generated noise.

Here are picture references of the gradients that I'm talking about and trying to achieve: Square Gradient Circle Gradient

The use of a gradient with noise is explained e.g. in a blog post I found here: https://medium.com/@travall/procedural-2d-island-generation-noise-functions-13976bddeaf9

But it only mentions creating the gradient without any details on how it's supposed to be done. I have tried looking around but I have been unsuccessful in finding what I'm looking for.

I would appreciate any tips or pointers in the right direction.

olawrdhalpme
  • 180
  • 8
  • The linked tutorial seems more like a photoshop tutorial than a javascript tutorial. It doesn't really explain anything at all. Are you sure the tutorial actually has anything to do with javascript (or programming)? – Emil Karlsson Jan 31 '22 at 13:33

1 Answers1

0

I've been trying to do pretty much the same thing. For the square one, you can find the answer here: 2D Array Gradient Generation in Unity

And for the circle one: You can generate it based on the distance of each "pixel" from the centre of the array.

this code is in C#, because that's what I'm using, but I think it's basic enough the be easily converted.

    int arrayWidth, arrayHeight;
    float[,] gradientArray = new var[arrayWidth, arrayHeight]

    int centerX = arrayWidth / 2 -1; //find center of the array
    int centerY = arrayHeight / 2 -1; //find center of the array

    for (int x = 0; x < arrayWidth; x++)
        {
            for (int y = 0; y < arrayHeight; y++)
            {
                float distanceX = (centerX - x) * (centerX - x);
                float distanceY = (centerY - y) * (centerY - y);

                //find distance from center
                float distanceToCenter = Mathf.Sqrt(distanceX + distanceY);
 
                //divide distance by one of the sides (width or height). 
                //by messing with this line you can easily alter the gradient
                distanceToCenter = distanceToCenter / arrayHeight; 

                gradientArray[x, y] = distanceToCenter; //set value
            }
        }
Kumma
  • 13
  • 4