[Ray vs AABB] exit point knowing entry point

Given:

  • An Axis Aligned Bounding Box (min, max coordinates)
  • A Ray entry point in this Box (Pin)
  • The normalized ray direction vector (Rd)
    Is there a way to determine the coordinates of the exit point Pout without having to test each plane of the box?
    The coordinates are given in 3D.
    Illustration in 2D:
 
        __Pout_____ max
       |  /        |
       | /         |
       |/          |
      Pin          |
       |___________|
      min

Sign of each Rd component should help reduce the number of planes to test from 5 to 2

I’ve decided to implement the whole AABB vs Ray test, which can be simplified in my scenario. Here’s the code in GLSL, if it can help others :slight_smile:

struct Ray
{
	vec3 o; // ray origin
	vec3 d; // ray direction unit vector
};

struct Box
{
	vec3 m[2]; // min and max
};

// compute exit point of ray from a AABB
vec3 ray_box_exit(in Ray r, in Box b)
{
	vec3 tmax;
	vec3 div = 1.0 / r.d;
	ivec3 indexes = ivec3(step(0.0,div));
	tmax.x = (b.m[indexes[0]].x - r.o.x) * div.x;
	tmax.y = (b.m[indexes[1]].y - r.o.y) * div.y;
	tmax.z = (b.m[indexes[2]].z - r.o.z) * div.z;

	return r.o + min(min(tmax.x, tmax.y), tmax.z) * r.d;
}