ARKit:处理点击以显示/隐藏节点

时间:2017-12-05 07:49:53

标签: ios swift scenekit arkit

我是ARKit的新手,我正在尝试在自来水位置创建一个SCNBox。我想要做的是在初始触摸时我创建一个盒子,并在第二次点击创建的盒子上它应该从场景中删除。我正在进行热门测试。但它一直在添加盒子。我知道这是一项简单的任务,但我无法做到这一点

    @objc func handleTap(sender: UITapGestureRecognizer) {

    print("hande  tapp")
    guard let _ = sceneView.session.currentFrame
        else { return }

    guard let scnView = sceneView else { return }
    let touchLocation = sender.location(in: scnView)
    let hitTestResult = scnView.hitTest(touchLocation, types: [ .featurePoint])

    guard let pointOfView = sceneView.pointOfView else {return}
    print("point   \(pointOfView.name)")

    if hitTestResult.count > 0 {

        print("Hit")

        if let _ = pointOfView as? ARBox {

            print("Box Available")
        }

        else {

            print("Adding box")

            let transform = hitTestResult.first?.worldTransform.columns.3
            let xPosition = transform?.x
            let yPosition = transform?.y
            let zPosition = transform?.z
            let position = SCNVector3(xPosition!,yPosition!,zPosition!)

            basketCount = basketCount + 1
            let newBasket = ARBox(position: position)
            newBasket.name = "basket\(basketCount)"
            self.sceneView.scene.rootNode.addChildNode(newBasket)
            boxNodes.append(newBasket)

        }
    }
}

1 个答案:

答案 0 :(得分:0)

sceneView的

pointOfView 是场景的根节点,用于渲染整个场景。对于通用情况,它通常是带灯/摄像头的空节点。我不认为你应该把它作为ARBox /或任何类型的SCNNode来表达。

你可能尝试的是下面的逻辑( hitResults是你的hitTest的结果):

if hitResults.count > 0 {
    if let node = hitResults.first?.node as SCNNode? (or ARBox) {
    // node.removeFromParentNode()
    // or make the node opaque if you don't want to remove
else {
    // add node.