Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save unitycoder/2cc493b2b207ce3520fcf68f30ebfe7f to your computer and use it in GitHub Desktop.

Select an option

Save unitycoder/2cc493b2b207ce3520fcf68f30ebfe7f to your computer and use it in GitHub Desktop.
fast-branchless-raybounding-box-intersections
// https://tavianator.com/fast-branchless-raybounding-box-intersections/
bool intersection(box b, ray r) {
double tx1 = (b.min.x - r.x0.x)*r.n_inv.x;
double tx2 = (b.max.x - r.x0.x)*r.n_inv.x;
double tmin = min(tx1, tx2);
double tmax = max(tx1, tx2);
double ty1 = (b.min.y - r.x0.y)*r.n_inv.y;
double ty2 = (b.max.y - r.x0.y)*r.n_inv.y;
tmin = max(tmin, min(ty1, ty2));
tmax = min(tmax, max(ty1, ty2));
return tmax >= tmin;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment