奇怪的Perlin噪音产生结果

时间:2019-01-26 17:14:29

标签: java opengl perlin-noise

我的Perlin噪音产生器一直存在一些问题;在该项目中,当我彼此相邻生成两个地形时,我一直在努力工作,我的照明得到了一个奇怪的结果:enter image description here

Perlin噪声脚本:

package generation;

import java.util.Random;

public class PerlinNoise {

    // settings for the perlin noise \\
    public static float AMPLITUDE = 100;
    public static int OCTAVES = 6;
    public static float ROUGHNESS = 0.3f;

    private Random random = new Random();

    private int seed;
    private int xOffset = 0;
    private int zOffset = 0;

    // constructor \\
    public PerlinNoise(int seed, int gridX, int gridZ, int vertexCount) {

        this.seed = seed;

        xOffset = gridX * (vertexCount-1);
        zOffset = gridZ * (vertexCount-1);

    }

    // function to generate height (returns a float) \\
    public float generateHeight(int x, int z) {

        x = x < 0 ? -x : x;
        z = z < 0 ? -z : z;

        float total = 0;
        float d = (float) Math.pow(2, OCTAVES-1);
        for(int i=0;i<OCTAVES;i++){
            float freq = (float) (Math.pow(2, i) / d);
            float amp = (float) Math.pow(ROUGHNESS, i) * AMPLITUDE;
            total += getInterpolatedNoise((x+xOffset)*freq, (z + zOffset)*freq) * amp;
        }

        return (float)total;

    }

    // gets interpolated noise from the noise \\
    private float getInterpolatedNoise(float x, float z){
        int intX = (int) x;
        int intZ = (int) z;
        float fracX = x - intX;
        float fracZ = z - intZ;

        float v1 = getSmoothNoise(intX, intZ);
        float v2 = getSmoothNoise(intX + 1, intZ);
        float v3 = getSmoothNoise(intX, intZ + 1);
        float v4 = getSmoothNoise(intX + 1, intZ + 1);
        float i1 = interpolate(v1, v2, fracX);
        float i2 = interpolate(v3, v4, fracX);
        return interpolate(i1, i2, fracZ);
    }

    // interpolates the noise \\
    private float interpolate(float a, float b, float blend){
        double theta = blend * Math.PI;
        float f = (float)(1f - Math.cos(theta)) * 0.5f;
        return a * (1f - f) + b * f;
    }

    // smooths the noise \\
    private float getSmoothNoise(int x, int z) {
        float corners = (getNoise(x - 1, z - 1) + getNoise(x + 1, z - 1) + getNoise(x - 1, z + 1)
                + getNoise(x + 1, z + 1)) / 16f;
        float sides = (getNoise(x - 1, z) + getNoise(x + 1, z) + getNoise(x, z - 1)
                + getNoise(x, z + 1)) / 8f;
        float center = getNoise(x, z) / 4f;
        return corners + sides + center;
    }

    // generates noise using the given int x,Z offset and seed\\
    private float getNoise(int x, int z) {

        random.setSeed(x * 03748 + z * 72849 + (seed/3-278)*2);

        return random.nextFloat() * 2f - 1f;
    }
}

如果您知道如何摆脱两个地形之间的界线,请告诉我。

预先感谢

0 个答案:

没有答案