Skip to content

Instantly share code, notes, and snippets.

@DomNomNom
Last active January 18, 2026 05:01
Show Gist options
  • Select an option

  • Save DomNomNom/46bb1ce47f68d255fd5d to your computer and use it in GitHub Desktop.

Select an option

Save DomNomNom/46bb1ce47f68d255fd5d to your computer and use it in GitHub Desktop.
Ray-AABB (Axis Aligned Bounding Box) intersection.
// adapted from intersectCube in https://github.com/evanw/webgl-path-tracing/blob/master/webgl-path-tracing.js
// compute the near and far intersections of the cube (stored in the x and y components) using the slab method
// no intersection means vec.x > vec.y (really tNear > tFar)
vec2 intersectAABB(vec3 rayOrigin, vec3 rayDir, vec3 boxMin, vec3 boxMax) {
vec3 tMin = (boxMin - rayOrigin) / rayDir;
vec3 tMax = (boxMax - rayOrigin) / rayDir;
vec3 t1 = min(tMin, tMax);
vec3 t2 = max(tMin, tMax);
float tNear = max(max(t1.x, t1.y), t1.z);
float tFar = min(min(t2.x, t2.y), t2.z);
return vec2(tNear, tFar);
};
@themightyoarfish
Copy link

What output is expected when rayDir has some zero components causing division by zero?

@DomNomNom
Copy link
Author

@themightyoarfish as long as the min and max functions prefer returning non-NaN's it should work as intended. This code has been written quite a while ago so you might want to check whether it behaves as expected on your target device. if not, you may need to filter out those when computing tNear and tFar.

@TheAIBot
Copy link

TheAIBot commented Jul 25, 2023

Leaving this here if anyone needed this for C# with 3 dimensional vectors.
SIMD optimized implementation that returns true if an intersection was found.

public bool Intersects(Ray ray)
{
    Vector4 tMin = (MinPosition - ray.Origin) / ray.Direction;
    Vector4 tMax = (MaxPosition - ray.Origin) / ray.Direction;
    Vector128<float> t1 = Vector4.Min(tMin, tMax).AsVector128();
    Vector128<float> t2 = Vector4.Max(tMin, tMax).AsVector128();

    Vector128<float> tNear = Sse.Max(Sse.Max(Sse.MoveHighToLow(t1, t1),
                                             Sse.Shuffle(t1, t1, 0b00_00_11_01)),
                                     t1);

    Vector128<float> tFar = Sse.Min(Sse.Min(Sse.MoveHighToLow(t2, t2),
                                            Sse.Shuffle(t2, t2, 0b00_00_11_01)),
                                    t2);

    return tNear.GetElement(0) <= tFar.GetElement(0) && tFar.GetElement(0) >= 0;
}

Obviously if you need an optimal solution for a single ray to check many boxes then get rid of the division operators.

public bool Intersects(RayAxisAlignBoxOptimizedIntersection optimizedRay)
{
    Vector4 tMin = (MinPosition - optimizedRay.Start) * optimizedRay.InverseDirection;
    Vector4 tMax = (MaxPosition - optimizedRay.Start) * optimizedRay.InverseDirection;

@Codezigineer
Copy link

That type name is absurdly long.

@peabnuts123
Copy link

Here's a simplified/optimised version in TypeScript that I'm pretty sure is equivalent to OP. Math.min() and Math.max() operators handle Infinity and -Infinity correctly so it works for axis-aligned rays.

export function rayAABBIntersection(rayOrigin: Vector3, rayDir: Vector3, aabb: AxisAlignedBoundingBox): number | undefined {
  const tMin = new Vector3(
    (aabb.xMin - rayOrigin.x) / rayDir.x,
    (aabb.yMin - rayOrigin.y) / rayDir.y,
    (aabb.zMin - rayOrigin.z) / rayDir.z,
  );
  const tMax = new Vector3(
    (aabb.xMax - rayOrigin.x) / rayDir.x,
    (aabb.yMax - rayOrigin.y) / rayDir.y,
    (aabb.zMax - rayOrigin.z) / rayDir.z,
  );

  const tNear = Math.max(
    Math.min(tMin.x, tMax.x),
    Math.min(tMin.y, tMax.y),
    Math.min(tMin.z, tMax.z),
  );
  const tFar = Math.min(
    Math.max(tMin.x, tMax.x),
    Math.max(tMin.y, tMax.y),
    Math.max(tMin.z, tMax.z),
  );

  if (tNear <= tFar) {
    // Ray intersects AABB
    return tNear;
  } else {
    // Ray does not intersect AABB
    return undefined;
  }
};

You can calculate the intersection point as rayOrigin + tNear * rayDir if you need that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment