MonoGame 中的 SimplexNoise 地形
SimplexNoise terrain in MonoGame
我正在 MonoGame 中制作一个随机生成的方块游戏,我正在尝试使用 Simplex Noise 来生成地形。问题是,我以前从未使用过 Simplex Noise,所以您可能猜到了,我的代码不起作用。它只会创建草方块。这是我试过的代码:
public void Generate() {
Tiles = new List<Tile>();
Seed = GenerateSeed();
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
float value = Noise.Generate((x / Width) * Seed, (y / Height) * Seed) / 10.0f;
if (value <= 0.1f) {
Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else if (value > 0.1f && value <= 0.5f) {
Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else {
Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
}
}
}
public int GenerateSeed() {
Random random = new Random();
int length = 8;
int result = 0;
for (int i = 0; i < length; i++) {
result += random.Next(0, 9);
}
return result;
}
我正在使用 this 实现来生成噪音。
检查您正在使用的 SimplexNoise 中的第 133 行:
// The result is scaled to return values in the interval [-1,1].
除以 10 后,结果在 -0.1 到 +0.1 之间
您需要一个从 0 到 1 的范围,因此您需要:
而不是除以 10
- 加 1(范围从 0 到 2)。
- 除以 2(范围从 0 到 1)。
float value = (Noise.Generate((x / Width) * Seed, (y / Height) * Seed) + 1) / 2.0f;
或者更改 if/else 以使用 -1 到 +1 范围。
if (value <= -0.8f)
{
Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else if (value <= 0)
{
Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else
{
Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
我正在 MonoGame 中制作一个随机生成的方块游戏,我正在尝试使用 Simplex Noise 来生成地形。问题是,我以前从未使用过 Simplex Noise,所以您可能猜到了,我的代码不起作用。它只会创建草方块。这是我试过的代码:
public void Generate() {
Tiles = new List<Tile>();
Seed = GenerateSeed();
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
float value = Noise.Generate((x / Width) * Seed, (y / Height) * Seed) / 10.0f;
if (value <= 0.1f) {
Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else if (value > 0.1f && value <= 0.5f) {
Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else {
Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
}
}
}
public int GenerateSeed() {
Random random = new Random();
int length = 8;
int result = 0;
for (int i = 0; i < length; i++) {
result += random.Next(0, 9);
}
return result;
}
我正在使用 this 实现来生成噪音。
检查您正在使用的 SimplexNoise 中的第 133 行:
// The result is scaled to return values in the interval [-1,1].
除以 10 后,结果在 -0.1 到 +0.1 之间 您需要一个从 0 到 1 的范围,因此您需要:
而不是除以 10- 加 1(范围从 0 到 2)。
- 除以 2(范围从 0 到 1)。
float value = (Noise.Generate((x / Width) * Seed, (y / Height) * Seed) + 1) / 2.0f;
或者更改 if/else 以使用 -1 到 +1 范围。
if (value <= -0.8f)
{
Tiles.Add(new Tile(Main.TileGrass, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else if (value <= 0)
{
Tiles.Add(new Tile(Main.TileSand, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}
else
{
Tiles.Add(new Tile(Main.TileWater, new Vector2((int)x * Tile.Size, (int)y * Tile.Size)));
}