Let the spheres have radius $r1$ and $r2$ and their centers be separated by distance $d$. There are four cases:
- $r1+r2 < d$ (separate spheres): $V = {4 \pi \over 3} (r1^3 + r2^3)$
- $r1 > r2 \wedge d + r2 < r1$ (sphere 2 within sphere 1): $V = {4 \pi \over 3} r1^3$
- $r2 > r1 \wedge d + r1 < r2$ (sphere 1 within sphere 2): $V = {4 \pi \over 3} r2^3$
- $r1 + r2 <d \wedge (d + r2 > r1 \vee d + r1 > r2)$ (partially intersecting spheres): $V = $$V = {4 \pi \over 3}(r1^3 + r2^3) - V_{cap1} - V_{cap2}$ (see below).
This last result comes from realizing that when two spheres partially intersect, we can define a plane through the circle defined by the spheres' intersecting surfaces. Then there are two "caps" that are "overcounted": the "cap" of sphere 1 within sphere 2, and the "cap" of sphere 2 within sphere 1. The cap of sphere 1 within sphere 2 has volume
$V_{cap} = \int_{h1}^r \pi r^2 dr$$V_{cap1} = \int_{h1}^r \pi r^2(x) dx$ ,
where
$r^2(x) = r1^2 - x^2$.
Thus $V_{cap1} = \frac{1}{3} \pi (\text{h1}-\text{r1})^2 (\text{h1}+2 \text{r1})$.
Likewise, we have
$V_{cap2} = \frac{\pi (d-\text{h1}+2 \text{r2}) \left(d^2-2 d \text{r2}+\text{r1}^2-\text{r2}^2\right)^2}{12 d^2}$.
Here $h1$ is the distance from sphere 1's center to the plane, and likewise for sphere 2. Note that here $h1 + h2 = d$. We solve for $h1$ by $\sqrt{r1^2 - h1^2} = \sqrt{r2^2 - (d - h1)^2}$ then the total volume is the sum of the volumes of the individual spheres (${4 \pi \over 3}ri^3$) minus the two overcounted "caps" given by $h1$ and $h2 = d - h1$.

Of course this computes lightning fast.
