首页 > 解决方案 > RealityKit – ARView 中的正确命中测试

问题描述

我有这样的代码:

@objc func handleTap(_ sender: UITapGestureRecognizer) {
    
    let tapLocation = sender.location(in: arView)
    
    let hitResult0 = scnView?.hitTest(tapLocation)

    if let hitResult = arView?.entity(at: tapLocation) {
         // ...
    }
}

hitResult的精度很差。WheelbarrowHandles即使我点击了另一个较小 的实体,它也会返回相同的实体 ( )。hitResult0提供靠近点击位置的所有对象(太多)。

模型加载:

self.theModel = try! Entity.load(named: "wheelborrow")
self.theModel?.generateCollisionShapes(recursive: true)

是否生成了不良的碰撞形状?


更新

我试图做下一代而不是一代:

modelEntity.collision = CollisionComponent(shapes: [ShapeResource.generateConvex(from: modelEntity.model!.mesh)])

它有效。

标签: swiftaugmented-realityhittestrealitykitusdz

解决方案


生成的碰撞形状:

generateCollisionShapes(recursive: true)

在此处输入图像描述

并使用下一个代码:

modelEntity.collision = CollisionComponent(shapes: [ShapeResource.generateConvex(from: modelEntity.model!.mesh)])

在此处输入图像描述


推荐阅读