使用ARSCNView后从场景中删除节点

时间:2018-05-31 21:50:23

标签: swift arkit

我正在制作一个使用ARKit来测量两点之间的应用程序。目标是能够测量长度,存储该值,然后测量宽度和存储。

我遇到的问题是在得到测量结果后处理节点。

到目前为止的步骤: 1)添加了一个带restartFunction的按钮。这有助于重置测量,但没有从场景中移除球体,也使得下一次测量变得笨重。

2)设置限制> 2个节点。这个功能最好。但是球体再一次漂浮在场景中。

以下是我所获得的最佳结果的屏幕截图。

@objc func handleTap(sender: UITapGestureRecognizer) {
    let tapLocation = sender.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(tapLocation, types: .featurePoint)
    if let result = hitTestResults.first {
        let position = SCNVector3.positionFrom(matrix: result.worldTransform)

        let sphere = SphereNode(position: position)


        sceneView.scene.rootNode.addChildNode(sphere)

        let tail = nodes.last

        nodes.append(sphere)

        if tail != nil {
            let distance = tail!.position.distance(to: sphere.position)
            infoLabel.text = String(format: "Size: %.2f inches", distance)

            if nodes.count > 2 {

                nodes.removeAll()

            }
        } else {
            nodes.append(sphere)

        }
    }
}

我是Swift的新手(一般编码),我的大部分代码来自拼凑教程。

2 个答案:

答案 0 :(得分:1)

我认为这里的问题是您实际上并没有删除已添加到层次结构中的SCNNodes

虽然您通过调用nodes.removeAll()从我假设的removeFromParentNode() 数组中删除节点,但您首先需要从场景层次结构中删除它们。

因此,您需要做的是在要删除的任何节点上调用以下函数:

for nodeAdded in nodesArray{
    nodeAdded.removeFromParentNode()
}

nodesArray.removeAll()

简单地说:

  

从父节点的子节点数组中删除节点。

因此你会做类似这样的事情,首先从层次结构中删除节点,然后将它们从数组中删除:

if nodes.count > 2 {

    for nodeAdded in nodes{
        nodeAdded.removeFromParentNode()
    }

    nodes.removeAll()

}

因此,根据提供的代码,您可以执行以下操作:

SCNNodes

为了将来参考,如果您要从您的层次结构中删除所有self.augmentedRealityView.scene.rootNode.enumerateChildNodes { (existingNode, _) in existingNode.removeFromParentNode() } ,您也可以致电:

var augmentedRealityView: ARSCNView!

其中self.augmentedRealityView引用变量:

/// Places A Marker Node At The Desired Tap Point
///
/// - Parameter sender: UITapGestureRecognizer
@objc func handleTap(_ sender: UITapGestureRecognizer) {

    //1. Get The Current Tap Location
    let currentTapLocation = sender.location(in: sceneView)

    //2. Check We Have Hit A Feature Point
    guard let hitTestResult = self.augmentedRealityView.hitTest(currentTapLocation, types: .featurePoint).first else { return }

    //3. Get The World Position From The HitTest Result
    let worldPosition = positionFromMatrix(hitTestResult.worldTransform)

    //4. Create A Marker Node
    createSphereNodeAt(worldPosition)

    //5. If We Have Two Nodes Then Measure The Distance
    if let distance = distanceBetweenNodes(){
        print("Distance == \(distance)")
    }

}

/// Creates A Marker Node
///
/// - Parameter position: SCNVector3
func createSphereNodeAt(_ position: SCNVector3){

    //1. If We Have More Than 2 Nodes Remove Them All From The Array & Hierachy
    if nodes.count >= 2{

        nodes.forEach { (nodeToRemove) in
            nodeToRemove.removeFromParentNode()
        }

        nodes.removeAll()
    }

    //2. Create A Marker Node With An SCNSphereGeometry & Add It To The Scene
    let markerNode = SCNNode()
    let markerGeometry = SCNSphere(radius: 0.01)
    markerGeometry.firstMaterial?.diffuse.contents = UIColor.cyan
    markerNode.geometry = markerGeometry
    markerNode.position = position
    sceneView.scene.rootNode.addChildNode(markerNode)

    //3. Add It To The Nodes Array
    nodes.append(markerNode)
}

/// Converts A matrix_float4x4 To An SCNVector3
///
/// - Parameter matrix: matrix_float4x4
/// - Returns: SCNVector3
func positionFromMatrix(_ matrix: matrix_float4x4) -> SCNVector3{

    return SCNVector3(matrix.columns.3.x, matrix.columns.3.y, matrix.columns.3.z)

}

/// Calculates The Distance Between 2 Nodes
///
/// - Returns: Float?
func distanceBetweenNodes()  -> Float? {

    guard let firstNode = nodes.first, let endNode = nodes.last else { return nil }
    let startPoint = GLKVector3Make(firstNode.position.x, firstNode.position.y, firstNode.position.z)
    let endPoint = GLKVector3Make(endNode.position.x, endNode.position.y, endNode.position.z)
    let distance = GLKVector3Distance(startPoint, endPoint)
    return distance
}

这是一个基于(并修改)您提供的代码的基本工作示例:

{{1}}

有关可能有助于您的开发的measurementApp示例,您可以在此处查看:ARKit Measuring Example

希望它有所帮助...

答案 1 :(得分:0)

这看起来像是一个逻辑问题。你在检查tail是不是nil之前就将nodes.last分配给tail。所以它永远不会!= nil所以你永远不会在else中执行nodes.append(sphere)。

我同意@dfd。设置断点以确保在继续之前执行代码。