# Help with Marching Cubes Voxel Terrain

Having fun implementing Marching Cubes, I’m having an issue which I can’t figure out.

I’m using a 2d HeightMap to generate density, then I add a Perlin Noise to get floating values interpolation. But I end up having all the terrain look very ‘blocky’.

I can’t figure out what I am doing wrong.

Here’s the code and output.

``````terrainData = Terrain.activeTerrain.terrainData;
heightMap = terrainData.GetHeights(0, 0, terrainData.heightmapWidth, terrainData.heightmapHeight);

for (int x = 0; x < size; x++)
{

for (int z = 0; z < size; z++)
{

for (int y = 0; y < size; y++)
{
float val = heightMap[x * 10, z * 10] * 30f;
if (y < val + Mathf.PerlinNoise(x, z))
{
data[x, y, z] = 0;

}
else
{
data[x, y, z] = -val;
}

}
}
}
``````

You are not adding perlinNoise. I mean, yes you are adding it as part of a comparison, but not using it other than to say : 0 or -val.
I admit I’m not sure if that’s relevant, but it’s just something that came to mind.

Well yeah, out of despair I’ve put it there

But basically, I iterate thru all the height map and get voxels from ground to sky; if it’s less than the value, then it’s 0 otherwise it’s terrain; no ?

Sounds fair. It was kind of a shot in the dark as an answer.
My train of thought was that you wanted the PerlinNoise to smooth the terrain in some way, and… ya… my train of thought wasn’t great or much beyond that.

Hope you figure it out / get some better help lol

1 Like