I have a bunch of markers placed in my scene as childNodes at fixed node positions in 3D world. When I move the phone around, I need to determine which marker node is the closest to the 2D screen center, so I can get the text description corresponding to that node and display it.
Right now, in a renderloop, I just determined the distance of each node from the screen center in a forEach loop, and decide if that distance is <150, if so get title and copy of that node. However, this doesn't solve my problem because there could be multiple nodes that satisfy that condition. I need to compare the distances from the center across all the nodes and get that one node that's is closest
func renderer(_ renderer: SCNSceneRenderer, willRenderScene scene: SCNScene, atTime time: TimeInterval){
scene.rootNode.childNodes.filter{ $0.name != nil }.forEach{ node in
guard let pointOfView = sceneView.pointOfView else { return }
let isVisible = sceneView.isNode(node, insideFrustumOf: pointOfView)
if isVisible {
let nodePos = sceneView.projectPoint(node.position)
let nodeScreenPos = CGPoint(x: CGFloat(nodePos.x), y: CGFloat(nodePos.y))
let distance = CGPointDistance(from: nodeScreenPos, to: view.center)
if distance < 150.0 {
print("display description of: \(node.name!)")
guard let title = ar360Experience?.threeSixtyHotspot?[Int(node.name!)!].title else { return }
guard let copy = ar360Experience?.threeSixtyHotspot?[Int(node.name!)!].copy else { return }
titleLabel.text = title
copyLabel.text = copy
cardView.isHidden = false
}else {
cardView.isHidden = true
}
}
}
}