I am trying to compute the intersection with MDL bounding Box, my code is based on the great article by WM at http://metalbyexample.com/picking-hit-testing/#more-738
the idea that t0 should be the nearest point https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
but that is not happening
extension MDLAxisAlignedBoundingBox {
func intersect(_ ray: Ray) -> float4? {
var tmin = minBounds
var tmax = maxBounds
let inverseDirection = 1 / ray.direction
var sign : [Int] = [(inverseDirection.x < 0) ? 1 : 0,(inverseDirection.y < 0) ? 1 : 0,(inverseDirection.z < 0) ? 1 : 0]
var bounds : [float3] = [minBounds,maxBounds]
var t0 = Float(minBounds.z)
if ((tmin.x > tmax.y) || (tmin.y > tmax.x)){
return nil
}
if (tmin.y > tmin.x){
tmin.x = tmin.y;
}
if (tmax.y < tmax.x){
tmax.x = tmax.y;
}
tmin.z = (bounds[sign[2]].z - ray.origin.z) * inverseDirection.z
tmax.z = (bounds[1-sign[2]].z - ray.origin.z) * inverseDirection.z
if ((tmin.x > tmax.z) || (tmin.z > tmax.x)){
return nil
}
if (tmin.z > tmin.x){
tmin.x = tmin.z
t0 = tmin.x
}
if (tmax.z < tmax.x){
tmax.x = tmax.z
t0 = tmax.z
}
return float4(ray.origin + ray.direction * t0, 1)
}
}
it is expected that the result if reached at the end should be the shortest vector.
Thank you in advance