【问题标题】:RealityKit ARkit : find an anchor (or entity) from raycast - always nilRealityKit ARkit:从 raycast 中找到锚点(或实体) - 始终为零
【发布时间】:2021-04-26 21:22:28
【问题描述】:

当我触摸屏幕时,我使用光线投射将对象(实体和锚点)放置到它的 worldTransform

然后我尝试触摸这个对象以获取它的锚点(或它自己的实体)

我正在尝试使用 raycast (或 hitTest )找到以前放置的锚点,但每件事都会返回 nil

像这样:

这是我的onTap 代码:

@IBAction func onTap(_ sender: UITapGestureRecognizer){
   let tapLocation = sender.location(in: arView)
        
   guard let result = arView.raycast(from: tapLocation, allowing: .estimatedPlane, alignment: .any).first else { return }
  print("we have anchor??")
  print(result.anchor) // always nil
        
  // never hit
  if let existResult = arView.hitTest(tapLocation).first {
     print("hit test trigger")
     if let entity = existResult.entity as Entity? {
        NSLog("we have an entity \(entity.name)")
...
      } 
   }  
}

这就是我创建一些对象和锚点的方式:

let anchor = AnchorEntity(world: position)
anchor.addChild(myObj)
arView.scene.anchors.append(anchor)
// my obj is now visible

你知道为什么我不能抓住我触摸的锚吗?

编辑:

ARview 配置:


        arView.session.delegate = self
        arView.automaticallyConfigureSession = true

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal, .vertical]
        
        NSLog("FINISHED INIT")
        
        if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
            config.sceneReconstruction = .mesh // .meshWithClassification
            arView.environment.sceneUnderstanding.options.insert([.occlusion])

            arView.debugOptions.insert(.showSceneUnderstanding)
            NSLog("FINISHED with scene reco")
        } else {
            NSLog("no not support scene Reconstruction")
        }
        

        
        let tapGesture = UITapGestureRecognizer(target: self, action:#selector(onTap))
        
        arView.addGestureRecognizer(tapGesture)
        arView.session.run(config)



【问题讨论】:

    标签: swift arkit realitykit


    【解决方案1】:

    我终于找到了解决办法:

    我的ModelEntity(锚定)必须有一个碰撞形状!

    所以在简单地添加entity.generateCollisionShapes(recursive: true)之后。

    这就是我如何生成一个简单的盒子:

    
        let box: MeshResource = .generateBox(width: width, height: height, depth: length)
        var material = SimpleMaterial()
        material.tintColor = color
        let entity = ModelEntity(mesh: box, materials: [material])
        entity.generateCollisionShapes(recursive: true) // Very important to active collisstion and hittesting !
        return entity
    
    

    然后我们必须告诉arView 来听手势:

    arView.installGestures(.all, for: entity)

    最后:

    
        @IBAction func onTap(_ sender: UITapGestureRecognizer){
            let tapLocation = sender.location(in: arView)
    
            if let hitEntity = arView.entity(
                at: tapLocation
            ) {
                print("touched")
                print(hitEntity.name)
                // touched !
    
                return ;
            }
    
        }
    
    

    【讨论】:

      【解决方案2】:

      只有在 ARKit 检测到平面后才能进行光线投射。它只能光线投射到平面或特征点。因此,请确保您使用平面检测(垂直或水平,具体取决于您的情况)运行 AR 配置

      let configuration = ARWorldTrackingConfiguration()
      configuration.planeDetection = .horizontal
      

      可以在ARSCNViewDelegate的didAdd delegate中查看是否在此处添加了平面锚

      func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
           
      if let planeAnchor = anchor as? ARPlaneAnchor {
         //plane detected
        }
      }
      

      【讨论】:

      • 嗨,是的,当我何时添加新锚点时,光线投射工作正常,但是在这里我想检测何时触摸已经存在的对象
      猜你喜欢
      • 1970-01-01
      • 2019-09-21
      • 2020-01-07
      • 2021-05-03
      • 2020-11-07
      • 1970-01-01
      • 2018-11-28
      • 2021-04-24
      • 2019-12-26
      相关资源
      最近更新 更多