gpt4 book ai didi

java - Perlin 噪声生成无法正常运行

转载 作者:行者123 更新时间:2023-11-30 07:16:26 27 4
gpt4 key购买 nike

我最近一直在处理 perlin 噪声,在我使用的 tile 引擎中实现它时,我注意到 perlin 噪声函数产生了“ block ”,如下图所示。每个像素都是 500 x 500 数组中的另一个不同位置,该数组从 perlin 噪声函数返回。

在此示例中,持久性为 0.5, Octave 音阶计数为 5 enter image description here

当进一步演奏时,我拥有的 Octave 音程越多, block 就越大。

这是我用来调用 perlin 噪声函数的代码:

PerlinNoise p = new PerlinNoise();
//returns a float[][] array of 500 by 500
p.GeneratePerlinNoise(p.genWhiteNoise(500, 500), 5, (float) 0.1);

PerlinNoise 类

导入java.util.Random;

public class PerlinNoise {
Random r;

public PerlinNoise() {
r = new Random();
}

public void setSeed(long seed) {
r.setSeed(seed);
}

public void printOutArray(float[][] arr) {
for(int i = 0; i < arr.length; i++) {
for(int n = 0; n < arr[0].length; n++) {
System.out.print(arr[i][n] + ", ");
}
System.out.print("\n");
}
}

public void printOutTerrain(float[][] arr) {
for(int i = 0; i < arr.length; i++) {
for(int n = 0; n < arr[0].length; n++) {
float a = arr[i][n];
if(a < 0.4) {
System.out.print("W");
} else {
System.out.print("L");
}
}
System.out.print("\n");
}
}

//-------------------------------------------------------------//

float[][] genWhiteNoise(int width, int height) {
float[][] noise = new float[height][width];

for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
noise[y][x] = r.nextFloat();
}
}

return noise;
}

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

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

int samplePeriod = 1 << octave; //calculates 2^k
float sampleFrequency = (float) (1.0/samplePeriod);

for(int i = 0; i < height; i++) {
int sample_i0 = (i / samplePeriod) * samplePeriod;
int sample_i1 = (sample_i0 + samplePeriod) % height; //wrap around
float vertical_blend = (i - sample_i0) * sampleFrequency;

for(int n = 0; n < width; n++) {
int sample_n0 = (n / samplePeriod) * samplePeriod;
int sample_n1 = (sample_n0 + samplePeriod) % width; //wrap around
float horizontal_blend = (n - sample_n0) * sampleFrequency;

//blend the top two corners
float top = Interpolate(baseNoise[sample_i0][sample_n0],
baseNoise[sample_i1][sample_n0], horizontal_blend);

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

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

return smoothNoise;
}

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

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

//generate smooth noise
for (int i = 0; i < octaveCount; i++)
{
smoothNoise[i] = genSmoothNoise(baseNoise, i);
}

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

//blend noise together
for (int octave = octaveCount - 1; octave >= 0; octave--)
{
amplitude *= persistance;
totalAmplitude += amplitude;

for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
perlinNoise[i][j] += smoothNoise[octave][i][j] * amplitude;
}
}
}

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

return perlinNoise;
}

//linear average between two points
float Interpolate(float x0, float x1, float alpha)
{
return Cosine_Interpolate(x0, x1, alpha);
}

//Linear Interpolation
float Linear_Interpolate(float x0, float x1, float alpha)
{
return x0 * (1 - alpha) + alpha * x1;
}

//Cosine interpolation (much smoother)
float Cosine_Interpolate(float x0, float x1, float alpha)
{
float ft = (float) (alpha * 3.141592653589);
float f = (float) ((1 - Math.cos(ft)) * 0.5);

return x0*(1-f) + x1*f;
}


}

所以重申我的问题:为什么我的 perlin 噪声函数的行为方式是这样的,就像只生成 block 中的空间一样?

最佳答案

所以要解决这个问题,我所要做的就是交换 genSmoothNoise() 方法中的 vertical_blend 和 horizo​​ntal_blend 变量。休息后你注意到的是惊人的

关于java - Perlin 噪声生成无法正常运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16973848/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com