【问题标题】:SceneKit- Cannot query using bitmaskSceneKit - 无法使用位掩码进行查询
【发布时间】:2022-01-21 09:18:08
【问题描述】:

我们能够使用位掩码进行自定义光线投射:

let hitTest = sceneView.hitTest(location, options: [categoryBitMask: bitmask])

但 hitTest 现在已被弃用,我不知道如何为光线投射查询设置位掩码:

let query = sceneView.raycastQuery(from: location, allowing: .estimatedPlane, alignment: .horizontal)

【问题讨论】:

    标签: swift scenekit arkit raycasting realitykit


    【解决方案1】:

    场景工具包

    在 SceneKit 中,您可以在 [SCNHitTestResult] 的上下文中使用位掩码。 hitTest(_:options:) 实例方法尚未弃用,它适用于 iOS 15.2。

    let sceneView = ARSCNView(frame: .zero)
    
    enum HitTestType: Int {
        case object_A = 0b00000001
        case object_B = 0b00000010
    }
    
    let point: CGPoint = gesture.location(in: self.sceneView)
    
    let bitMask = HitTestType.object_A.rawValue | HitTestType.object_B.rawValue
    
    let results = sceneView.hitTest(point, options: [.categoryBitMask: bitMask])
    

    附言

    目前仅不推荐使用hitTest(_:types:)


    RealityKit

    在 RealityKit 中,您可以在 CollisionCastHit 的上下文中使用位掩码:

    let arView = ARView(frame: .zero)
    
    let point: CGPoint = gesture.location(in: self.arView)
        
    let (origin, direction) = arView.ray(through: point)!
        
    let raycasts: [CollisionCastHit] = arView.scene.raycast(origin: origin, 
                                                         direction: direction, 
                                                            length: 50, 
                                                             query: .any, 
                                                              mask: .default, 
                                                        relativeTo: nil)
    

    ...或者这样:

    let raycasts: [CollisionCastHit]  = arView.hitTest(point, 
                                                       query: .any, 
                                                        mask: .default)
    

    【讨论】:

    • 感谢@Andy,但我认为您使用的是 RealityKit,SCNScene 不包含“raycast”方法。
    • 你是对的,我弄错了:“func hitTest(_ point: CGPoint, types: ARHitTestResult.ResultType) -> [ARHitTestResult]”
    猜你喜欢
    • 1970-01-01
    • 2014-07-25
    • 2012-02-20
    • 1970-01-01
    • 2021-12-10
    • 2019-03-10
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多