简体   繁体   中英

C# perlin Noise

I have read some tutorials on perlin noise (for example this one) for my terrain generation, and I wanted to make sure that I understood it and can correctly implement it.

I start with 1 Dimension:

amplitude = persistence^i // persistence can have any value but mostly it is 1 or lower. It changes the amplitude of the graphs with higher frequency since:

frequency = 2^i //the 2 effects, that each graph reflects one octave, wich is not 100% necessary but the example happens do do it like this

'i' is the octave we are looking at.

Here is my attempt:

private float[] generateGraph()
{
    float[] graph = new float[output.Width];
    for (int i = 0; i < output.Width; i += 1/frequency)
    {
        graph[i] = random.Next((int)(1000000000000*persistence))/1000000000000f;
    }
    return graph;
}

I imagined the array as a graph, where the index is X and the value is Y. I search for a value for every multiple of texture.Width/frequency until the end of the array.

I have some random values I am using for now, which I have to connect with either Linear Interpolation/Cosine Interpolation or Cubic Interpolation. Which one should I use? Which is the most performant when I want to use the noise for terrain generation in 2D?

I would like to put the graphs in a 2D-array after this and then check for each value, if its higher than 0.5, it should get some material or texture.

Is this situation, how should I do it? Am I totally on the wrong track?

edit1: Before I put the graph in a 2D array, I would like to generate perhaps 5 other graphs with a higher 'i' and blend them (which shouldn't be too hard).

edit2: this implementation is nice and 'easy'.

Define "too much performance" any kind of interpolation should be fine for 2D data. If you are really worried about performance, you might try implementing Simplex Noise , but that is much harder to understand and implement and it really becomes better at 3D and higher. In 2D they are somehow comparable.

Also, perlin noise is usually implemented as function of x parameters, where x is number of dimensions and the function has internal array of random values, that is accessed based on integer values of the parameters. You should try studying the original source code .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM