improve AABB hit test by adding a range check

main
jbb01 6 months ago
parent 1b02f8a96d
commit a90a0db6d5

@ -55,7 +55,7 @@ public record AABB(@NotNull Vec3 min, @NotNull Vec3 max) {
return new AABB(Vec3.min(this.min, box.min), Vec3.max(this.max, box.max));
}
public boolean hit(@NotNull Ray ray) {
public boolean hit(@NotNull Ray ray, @NotNull Range range) {
var origin = ray.origin();
var direction = ray.direction();
var invDirection = direction.inv();
@ -82,7 +82,7 @@ public record AABB(@NotNull Vec3 min, @NotNull Vec3 max) {
}
}
return tlmax < tumin;
return tlmax < tumin && tumin >= range.min() && tlmax <= range.max();
}
public static double @NotNull[] intersect(@NotNull Vec3 corner, @NotNull Vec3 origin, @NotNull Vec3 invDirection) {

@ -61,9 +61,12 @@ public record Box(@NotNull AABB box, @NotNull Material material) implements Hitt
}
}
if (tlmax >= tumin) return Optional.empty();
if (tlmax < tumin && tumin >= range.min() && tlmax <= range.max()) {
assert entry != null && exit != null;
return hit0(tlmax, tumin, entry, exit, ray, range);
} else {
return Optional.empty();
}
}
private @NotNull Optional<HitResult> hit0(double tmin, double tmax, @NotNull Side entry, @NotNull Side exit, @NotNull Ray ray, @NotNull Range range) {

@ -47,7 +47,7 @@ public final class HittableBinaryTree extends HittableCollection {
@Override
public void hit(@NotNull Ray ray, @NotNull State state) {
if (!bbox.hit(ray)) return;
if (!bbox.hit(ray, state.getRange())) return;
if (left instanceof HittableCollection coll) {
coll.hit(ray, state);
} else if (left != null) {

@ -41,6 +41,10 @@ public abstract class HittableCollection implements Hittable {
this.range = Objects.requireNonNull(range);
}
public @NotNull Range getRange() {
return range;
}
private @NotNull Optional<HitResult> getResult() {
return Optional.ofNullable(result);
}

Loading…
Cancel
Save