5

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

https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-box-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

mjeragh
  • 85
  • 8

1 Answers1

2

Here is the solution:

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)

  }

}

you have to be sure that you are point in the right direction.

far / (near - far)

your

eyeRayDir.z = -1

and the camera position

camera.position = [0, 0, 15]

and in your node class you should have

var boundingBox = MDLAxisAlignedBoundingBox()
var size: float3 {
    return boundingBox.maxBounds - boundingBox.minBounds
}

Hope that helps

mjeragh
  • 85
  • 8
  • Doesn't work, as camera gets farther in the z direction the ray doesn't collide with cube hm. im I missing something else? – masaldana2 Jun 23 '22 at 02:51