Hello,

I’m making asteroids for my game. I’ve decided to do so procedurally using 3D Perlin noise and a Marching Cubes algorithm.

I’ve run into a problem whilst trying to confine the marching cubes to a sphere. Normally, marching cubes gives you a cube-shaped grid. However, I’ve modified it so that it cuts off anything that’s outside of a radius of a sphere like so:

I do this by adding this code to my marching cubes algorithm:

```
void FillData(float xOrigin, float yOrigin, float zOrigin) {
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
for (int z = 0; z < size; z++) {
// If the current point is outside the radius, set it to -1
if (Vector3.Distance(new Vector3(x, y, z), new Vector3(xOrigin + size/2, yOrigin + size/2, zOrigin + size/2)) > (size / 2)) {
data[x, y, z] = -1;
continue;
}
// more code here...
}
}
}
```

xOrigin, yOrigin, and zOrigin are the Terrain GameObject’s transform.position. If the distance between the current point and the outer edge of the sphere is larger than the sphere’s radius, cut those points off (set them equal to a negative density).

~

This works, up to a certain point. When I move the Terrain GameObject past the length of the sphere’s radius (AKA size / 2), the asteroid starts to disappear, like this:

How do I prevent this from happening?