Chribit
Chribit

Reputation: 73

Is there a more efficient Ray intersection algorithm for rounded Boxes?

I'm currently trying to figure out a rendering approach for implicit geometry which involves a ray intersection algorithm for oriented rounded box primitives. It's just a small toy project and I'm not the best at deriving the below formulas myself mathematically, which is why I'm asking this question.

The intersection of a ray originating from ro in the direction rd with a box of half-dimensions bd is easy to compute:

void box_intersection (vec3 ro, vec3 rd, vec3 bd) 
{
    vec3 m = 1.0 / rd;

    vec3 plane_hit = -1.0 * m * ro;
    vec3 plane_offset = abs(m) * bd;

    vec3 planes_near = plane_hit - plane_offset;
    vec3 planes_far = plane_hit + plane_offset;

    float near_distance = max(max( planes_near.x, planes_near.y ), planes_near.z );
    float far_distance = min(min( planes_far.x, planes_far.y ), planes_far.z );

    if (near_distance <= far_distance)
    {
        // handle intersection
    }
}

sources:

But once you want the intersection with a uniformly rounded box, the necessary algorithm explodes in complexity. I don't know how to hide snippets of code behind a collapsible section or something like that, but the code for intersecting a rounded box can also be found on iq's site (iquilezles.org) linked above, right below the snippet for standard box intersections.

As far as I know, this intersection test considers the radius to be interior i.e. contained within the bounding box defined by the box dimensions. This is unlike another approach to rendering a rounded box using signed distance fields (SDFs), where the radius is "added" to the box, visually expanding it by that radius in all directions. However, I'm not using a ray-marching approach which is needed to find the intersection to such an SDF along a ray.

I understand why the algorithm for gaining the closest distance to a rounded box is so much simpler compared to gaining accurate intersections without ray marching. The former "only" needs to consider a point as input while the latter considers an entire line. However I did not expect the difference in computational effort to be this drastic.

Given that the article is fairly old in computer graphics terms (the shadertoy examples are from 2019) and I don't know if Inigo Quilez updates that article anymore - I was wondering if there are any more efficient approaches out there by now? I'm not having any luck using search engines, as it usually refers me to bounding box intersection approaches...

I "only" need the closest point along a ray and I'm fine with the radius being "added outside" of the box dimensions as seen in the SDF version - if that makes any calculations simpler.

Does anyone know of a more efficient approach to getting the closest intersection with a rounded box? Thanks in advance!

Upvotes: 0

Views: 89

Answers (0)

Related Questions