# Generating a marching cubes terrain with perlin noise

This topic is 1914 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hellp there! I was making a marching cubes terrain, but i encounter a very annoying trouble, when generator was rising the "y" level is riging by 2 Screenshot, i spend almost 2 days, but no progress at all...here is the code of the the generator, perlin noise is from the Lib Noise .NET, maybe someone here can point me to the solution=(
[source lang="csharp"] private void Generate()
{
//Initializing the noise
Random rnd = new Random();
Perlin perlin = new Perlin();
perlin.Frequency = 0.05;
perlin.Lacunarity = 0.25;
perlin.OctaveCount = 1;
perlin.Persistence = 0.05;
perlin.Seed = 5;
perlin.NoiseQuality = NoiseQuality.High;

//Generating the noise values for every vertex
bool[, ,] vertexDensities = new bool[Size + 1, Height + 1, Size + 1];
for (float y = -0.5f; y < Height + 0.5f; y ++)
for (float z = -0.5f; z < Size + 0.5f; z ++)
for (float x = -0.5f; x < Size + 0.5f; x++)
{
float density = ((-y + 0.5f) + 0) + (float)(perlin.GetValue(x, y, z));
vertexDensities[(int)(x + 0.5f), (int)(y + 0.5f), (int)(z + 0.5f)] = density > 0;
}

//Writing values for each voxel
for (int y = 0; y < Height; y++)
for (int z = 0; z < Size; z++)
for (int x = 0; x < Size; x++)
{
//Converting density values into binary form
string density = "";
density += Convert.ToByte(vertexDensities[x + 1, y, z]);
density += Convert.ToByte(vertexDensities[x + 1, y + 1, z]);
density += Convert.ToByte(vertexDensities[x, y + 1, z]);
density += Convert.ToByte(vertexDensities[x, y, z]);
density += Convert.ToByte(vertexDensities[x + 1, y, z + 1]);
density += Convert.ToByte(vertexDensities[x + 1, y + 1, z + 1]);
density += Convert.ToByte(vertexDensities[x, y + 1, z + 1]);
density += Convert.ToByte(vertexDensities[x, y, z + 1]);
depthData[x, y, z] = Convert.ToByte(density, 2);
}
}[/source] Edited by CohoJET