【问题标题】:How to hit test with a bounding box using MDL如何使用 MDL 对边界框进行命中测试
【发布时间】:2019-08-21 10:41:11
【问题描述】:

我正在尝试计算与 MDL 边界框的交集,我的代码基于 WM 在http://metalbyexample.com/picking-hit-testing/#more-738 的精彩文章

t0 应该是最近点的想法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

但这并没有发生

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)
    }
}

预计最后到达的结果应该是最短的向量。

提前谢谢你

【问题讨论】:

  • 你找到解决方案了吗?如果是请分享
  • 是的,我一个月前就知道了。

标签: swift 3d metal metalkit


【解决方案1】:

解决办法如下:

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)

  }

}

你必须确保你指向正确的方向。

far / (near - far)

你的

eyeRayDir.z = -1

和相机位置

camera.position = [0, 0, 15]

在你的节点类中你应该有

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

希望有帮助

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    相关资源
    最近更新 更多