-
Notifications
You must be signed in to change notification settings - Fork 0
2D SDF(8): Logical Operations
One fascinating aspect of SDF is ther support for logical operations. Comparing two one-dimensinal values using min/max functions can result in operations on two-dimensinal shape or three-dimemsional geometries.
There adre fow functions to describe operations betwwen two gemoetric entities: Union, Subtraction, Intersection, and XOR(exclusive or). Here are the geometric explanations for these operations
float opUnion(float d1, float d2)
{
return min(d1, d2);
}
Geometric Interpretation: This operation finds the minimum distance of a point to two shapes and returns that minimum as the result. In geometric terms, the union operation merges two shapes into a single shape. Hence, any point's distance to the union of these two shapes is the smaller of its distances to each of the two shapes. This function achieves this by computing min(d1, d2), effectively merging the two shapes from a distance field perspective.
float opSubtraction(float d1, float d2)
{
return max(-d1, d2);
}
Geometric Interpretation: When subtracting one shape from another, all points inside the subtracted shape (second shape) become outside of the resulting shape. This is accomplished by inverting the sign of d1 (making it -d1). The operation then returns the "farthest outside" distance, using max(-d1, d2), effectively carving out the second shape from the first.
float opIntersection(float d1, float d2)
{
return max(d1, d2);
}
Geometric Interpretation: The intersection operation retrieves the shared space between two shapes, where any point’s distance to the resulting shape is the greater of its distances to each of the two shapes. In simpler terms, only the points that are within both shapes are considered to be inside the intersection. This is achieved with max(d1, d2), ensuring that a point is inside both shapes for it to be considered inside the intersection.
float opXor(float d1, float d2)
{
return max(min(d1, d2), -max(d1, d2));
}
Geometric Interpretation: The XOR (exclusive or) operation produces a shape consisting of the non-overlapping parts of two shapes. It combines the outsides of both shapes while excluding their intersection. Practically, it identifies which of the two shapes is closer (min(d1, d2)) and then uses the negative of the farthest distance (-max(d1, d2)) to exclude the intersection, effectively combining the unique parts of both shapes.
This mainly involves understanding IQ's blog post. https://iquilezles.org/articles/interiordistance/
The gif above depicts a composite shape construced with OpUnion from two rectangle and a circle. and it's noticeable that the distance field inside this shape is incorrect. Theoretically, there are two methods for modeling complex shapes using SDF. 1) combining basic shapes or designing new formulas. However, the use of min() and max() operators does not necessarily yeild a correct SDF, especially in the internal regions, But sometimes, the internal area isn't actually needed, so this error can be ignored. Additionally, as can be seen from the image above, the errors become more egregious towards the centor of the shape. In practice, artifacts introuduced by the min() function are primarily present deep within shapes, or at isolated positions that only touch the suface. Since center difference methods typically sample the sufrace within a small range, the erros are rare and minor when they do appear. This issue is generrally negligialbe. Also, we can consider inverting the problem and thinking about negative space as a potential solution. Taking the above image as an example, we can represent the same boundaries using multiple rectangles and circulare arcs, only now the inside becomes the outside, however, this limits the variability of hte shape and is more suited to fixed settings. such as inside a room