# How to calculate SDF intersection for union and primitive?

First I union A1 and A2 (with min). A2 distance is closest. Then I do intersect (max) between the union A (min of a1 and a2) and B rectangle. Distance to A2 is result. But that doesn't help me to to render only the intersection.

The result you are getting should help you if you are going to do ray marching.

The result you get d = max(min(a1, a2), b) is a minimum safe distance you can advance the ray, in any direction, and be sure you won't collide with anything. This does not mean you will collide at that distance, it means you need to sample again at that distance.

The issue is that these operations done with min and max are not an exact SDF. They are only lower bound (it is equal or smaller than the exact SDF) with the correct contour at zero. This is good enough to render with ray marching, even though it means extra iterations. Be aware the ray marching may require a lot of iterations anyway, in particular when a ray is passing nearby the geometry.

Sadly, there is no general solution to compute the exact SDF of an union, subtraction or intersection of SDFs. If you need an exact SDF of the result you will need a function capable of generating the SDF directly from the description of the shape, instead of manipulating SDFs with union, subtraction or intersection.

As mental exercise, consider that the intersection of a2 and b is void. What SDF would you expect to get? With max you get an "SDF" that is zero nowhere - which is correct - but it still has local minima that correlates to the contour of the shapes.

I'll refer you to Inigo Quilez article on Interior SDFs for further reading.

• Pretty sure the union SDF is exact, following from the definition of an SDF: the distance to the nearest point of a set is the minimum of the distances to all points in the set. Edit: I guess technically, the exterior SDF is exact, but not the interior. The reverse is true for intersection. Aug 30, 2023 at 5:44