Perlin Noise没有更多Octaves获得深度

时间:2017-04-07 02:17:47

标签: java perlin-noise

我是Perlin噪音的新手,我遇到了障碍。我从C ++翻译的perlin噪声函数似乎在一个八度音程中正常工作,但是我发现噪声的低八度音不会添加到原始的Perlin噪声中。这是我的代码:

public class Perlin {
    float[][] generateWhiteNoise(int width, int height) {
        Random random = new Random(0);
        float[][] noise = new float[width][height];

        for (int i = 0; i < noise.length; i++) {
            for (int j = 0; j < noise[i].length; j++){
                noise[i][j] = (float)random.nextDouble();
            }
        }

        return noise;
    }

    float[][] generateSmoothNoise(float[][] baseNoise, int octave){
        int width = baseNoise.length;
        int height = baseNoise[0].length;

        float[][] smoothNoise = baseNoise;

        int samplePeriod = (int) Math.pow(2,octave); // calculates 2 ^ k
        float sampleFrequency = 1.0f / samplePeriod;

        for (int i = 0; i < width; i++) {
          //calculate the horizontal sampling indices
          int sample_i0 = (i / samplePeriod) * samplePeriod;
          int sample_i1 = (sample_i0 + samplePeriod) % width; //wrap around
          float horizontal_blend = (i - sample_i0) * sampleFrequency;
          for (int j = 0; j < height; j++){
             //calculate the vertical sampling indices
             int sample_j0 = (j / samplePeriod) * samplePeriod;
             int sample_j1 = (sample_j0 + samplePeriod) % height; //wrap around
             float vertical_blend = (j - sample_j0) * sampleFrequency;
             //blend the top two corners
             float top = interpolate(baseNoise[sample_i0][sample_j0],
                baseNoise[sample_i1][sample_j0], horizontal_blend);

             //blend the bottom two corners
             float bottom = interpolate(baseNoise[sample_i0][sample_j1],
                baseNoise[sample_i1][sample_j1], horizontal_blend);

             //final blend
             smoothNoise[i][j] = interpolate(top, bottom, vertical_blend);
          }
       }

       return smoothNoise;
    }

    float interpolate(float x0, float x1, float alpha){
        return (float) ((float)(x0) * (float)(1 - alpha) + (float)(alpha * x1));
    }

    float[][] generatePerlinNoise(float[][] baseNoise, int octaveCount) {
       int width = baseNoise.length;
       int height = baseNoise[0].length;

       float[][][] smoothNoise = new float[octaveCount][][]; //an array of 2D arrays containing

       float persistance = .5f;

       //generate smooth noise
       for (int i = 0; i<octaveCount; i++) {
           System.out.println("Generating Smooth Noise: " + i);
           smoothNoise[i] = generateSmoothNoise(baseNoise, i);
       }

        float[][] perlinNoise = new float[width][height];
        float amplitude = 1.0f;
        float totalAmplitude = 0.0f;

        //blend noise together
        for (int octave = octaveCount - 1; octave >= 0; octave--) {
           amplitude *= persistance;
           totalAmplitude += amplitude;
           System.out.println("Adding smooth noise for octave: " + octave + " at amplitude: " + amplitude);
           for (int i = 0; i < width; i++) {
              for (int j = 0; j < height; j++) {
                 perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
              }
           }
        }

       //normalization
       for (int i = 0; i < width; i++) {
          for (int j = 0; j < height; j++) {
             perlinNoise[i][j] /= totalAmplitude;
          }
       }
       return perlinNoise;
    }

    public float[][] printVals(float[][] baseNoise){
        baseNoise = generatePerlinNoise(generateWhiteNoise(800,800),6);
        for(int i = 0; i<baseNoise.length; i++){
            String row = "";
            for(int j = 0; j<baseNoise[i].length;j++){
                row+= (int)(baseNoise[i][j]*255) + " ";
            }
            System.out.println(row);
        }
        return baseNoise;
    }
}

以下是我用来获取值的代码:

baseNoise = generatePerlinNoise(generateWhiteNoise(800,800),6);

以下是八度音程1,2,3和4的输出

Frequency 1 Frequency 2 Frequency 3 Frequency 4

任何帮助将不胜感激!

修改 通过反复试验,我发现最有可能出现问题的区域是generatePerlinNoise()函数。如果我改变八度音阶,我会得到所需的噪音水平,这就是我想要的。这也意味着generateWhiteNoise()generateSmoothNoise()有效。因此,在generatePerlinNoise()混合中的某个地方存在问题,但它似乎应该有效。

1 个答案:

答案 0 :(得分:0)

我找到了答案。我曾经以为generateSmoothNoise()命令会给我多倍的八度perlin噪音。然而,我意识到我需要构建另一个命令,类似于混合平滑噪声以混合我的不同八度perlin噪声所发生的命令。

public float[][] generateMultiOctavePerlinNoise(int octaves, double persistence, double dropoff, int width, int height){
        float[][][]noise = new float[octaves][width][height];
        for(int i = octaves - 1; i > 0;i--){
            noise[i] = generatePerlinNoise(generateWhiteNoise(width,height),octaves - i);
        }

        float[][] multiOctave = new float[width][height];

        for(int a= 0; a<noise.length; a++){
            persistence*= dropoff;
            for(int i = 0; i<multiOctave.length; i++){
                for(int j = 0; j<multiOctave[i].length; j++){
                    multiOctave[i][j] += noise[a][i][j]*persistence;
                }
            }
        }
        return multiOctave;

    }