Confining Marching Cubes to the Radius of a Sphere

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?

Your xOrigin, yOrigin and zOrigin should not have any influence on the radius distance since it’s a local coordinate vector. Your Vector3(x,y,z) is always a vector from the “world origin” up to (size,size,size).

What you actually want is a local space vector that doesn’t go from (0,0,0) up to (size,size,size) but a vector that should have the range (-size/2f, -size/2f, -size/2f) up to (size/2f, size/2f, size/2f). So you just subtract half the size of each component. The resulting vector will be a relative vector from the center of your cube volume. The length of this vector should be compared against your desired radius. Since we just want to compare length we can use the squared length since it’s easier to calculate:

float radius = size * 0.5f;
float sqrRadius = radius * radius;

for (int x = 0; x < size; x++) {
    for (int y = 0; y < size; y++) {
        for (int z = 0; z < size; z++) {
            var v = new Vector3(x-radius, y-radius, z-radius);
            if (v.sqrMagnitude > sqrRadius)
            {
                data[x, y, z] = -1;
                continue;
            }
            // [ .. ]