[英]Version of Improved Noise keeps returning 0
我正在嘗試在我的 XNA 游戲中實現改進的噪聲,但我的改進噪聲 function 一直返回 0.0f。 它與 Ken Perlin 的代碼 ( http://mrl.nyu.edu/~perlin/noise/ ) 完全相同,只是移植到 C#。
我試過重寫 class 甚至直接從站點復制和粘貼(然后移植到 C#,當然),但它不會 Z78E6221F6393D1356681DB308F14CE6DZ 任何值。
這是我正在使用的代碼:
public class PerlinNoise
{
private int[] permutations = new int[512];
private Random random;
public PerlinNoise()
: this(Environment.TickCount)
{ }
public PerlinNoise(int seed)
{
random = new Random(seed);
for (int i = 0; i < 256; i++)
{
permutations[i] = i;
}
for (int i = 0; i < 256; i++)
{
int k = random.Next(256 - i) + i;
int l = permutations[i];
permutations[i] = permutations[k];
permutations[k] = l;
permutations[i + 256] = permutations[i];
}
}
private int fastfloor(float x)
{
return x > 0 ? (int)x : (int)x - 1;
}
private float fade(float t)
{
return t * t * t * (t * (t * 6 - 15) + 10);
}
private float lerp(float t, float a, float b)
{
return a + t * (b - a);
}
public float grad(int hash, float x, float y, float z)
{
int h = hash & 15;
float u = h < 8 ? x : y,
v = h < 4 ? y : h == 12 || h == 14 ? x : z;
return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
}
public float noise3d(float x, float y, float z)
{
int X = fastfloor(x) & 0xff,
Y = fastfloor(y) & 0xff,
Z = fastfloor(z) & 0xff;
x -= fastfloor(x);
y -= fastfloor(y);
z -= fastfloor(z);
float u = fade(x);
float v = fade(y);
float w = fade(z);
int A = permutations[X] + Y, AA = permutations[A] + Z, AB = permutations[A + 1] + Z,
B = permutations[X + 1] + Y, BA = permutations[B] + Z, BB = permutations[B + 1] + Z;
return lerp(w, lerp(v, lerp(u, grad(permutations[AA], x, y, z),
grad(permutations[BA], x - 1, y, z)),
lerp(u, grad(permutations[AB], x, y - 1, z),
grad(permutations[BB], x - 1, y - 1, z))),
lerp(v, lerp(u, grad(permutations[AA + 1], x, y, z - 1),
grad(permutations[BA + 1], x - 1, y, z - 1)),
lerp(u, grad(permutations[AB + 1], x, y - 1, z - 1),
grad(permutations[BB + 1], x - 1, y - 1, z - 1))));
}
public float noise2d(float x, float y)
{
return noise3d(x, y, 0f);
}
} `
為了測試它,我只是做了:
string[] args = Console.ReadLine().Split(' ');
PerlinNoise noise = new PerlinNoise();
int x = args[0];
int y = args[1];
int z = args[2];
Console.WriteLine(noise.noise3d(x, y, z));
正如我上面所說,它總是 output 0。
如果所有 arguments 都是整數,則似乎 output 0.0f
。 將您的測試代碼更改為
var input = Console.ReadLine()
.Split(' ')
.Select(s => float.Parse(s,
System.Globalization.CultureInfo.InvariantCulture))
.ToArray();
並嘗試輸入,例如4234.2123 3123.12312 423.2434
。
我不太確定這是否是所需的行為,但是
x -= Math.Floor(x); // FIND RELATIVE X,Y,Z
y -= Math.Floor(y); // OF POINT IN CUBE.
z -= Math.Floor(z);
如果x
、 y
和z
= 0,則它們是整數; fade(0.0f)
也總是為零。
在您的輸入案例中將您的輸入乘以(1 / MAX_VALUE)
,只需乘以 1 / 256 左右,並且永遠不要給它更大的值。 在您的游戲中使用時,將輸入乘以(1 / MAXIMUM_CHOORD_VALUE)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.