1
votes

I am trying to center a UIView to the center of my SCNView in Order to detect the other added SCNTorus nodes in my scene. enter image description here

I added a view to the center of my sceneView like below

var focusPoint: CGPoint {
    return CGPoint(
        x: sceneView.bounds.size.width / 2,
        y: sceneView.bounds.size.height - (sceneView.bounds.size.height / 1.618))
}

Then I tried two ways :

1 -

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

        DispatchQueue.main.async { [weak self] in

            guard let strongSelf = self else { return }

            if !strongSelf.inEditMode { return }

            for node in strongSelf.selectionRingsNodes {

                let projectedPoint = renderer.projectPoint(node.position)
                let projectedCGPoint = CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y))

                let distance = projectedCGPoint.distance(to: strongSelf.focusPoint)


                if distance < 20 {
                    print(node.name)
                }

            }
        }
    }

2 -

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

        DispatchQueue.main.async { [weak self] in

            guard let strongSelf = self else { return }

            if !strongSelf.inEditMode { return }

            for node in strongSelf.selectionRingsNodes {

                let (min, max) = node.boundingBox
                let projectedMinPoint = renderer.projectPoint(min)
                let projectedMinCGPoint = CGPoint(x: CGFloat(projectedMinPoint.x), y: CGFloat(projectedMinPoint.y))

                let projectedMaxPoint = renderer.projectPoint(max)
                let projectedMaxCGPoint = CGPoint(x: CGFloat(projectedMaxPoint.x), y: CGFloat(projectedMaxPoint.y))

                let minX = CGFloat(projectedMinCGPoint.x)
                let maxX = CGFloat(projectedMaxCGPoint.x)
                let minY = CGFloat(projectedMinCGPoint.y)
                let maxY = CGFloat(projectedMaxCGPoint.y)

                let nodeRect = CGRect(x: minX, y: minY, width: maxX - minX, height: maxY - minY)

                if nodeRect.contains(strongSelf.focusPoint) {
                    print(node.name)
                }
            }
        }
    }

These two methods return the wrong results, a very big distance, and very big x and y.

1

1 Answers

0
votes

Finally I Got the solution!

It turned out that I should convert the position to the scene’s world coordinate space, using this method convertPosition(SCNVector3,to:)

Here the complete code :

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

        DispatchQueue.main.async { [weak self] in

            guard let strongSelf = self else { return }

            if !strongSelf.inEditMode { return }

            for node in strongSelf.selectionRingsNodes {

                let position = node.convertPosition(SCNVector3Zero, to: nil)
                let projectedPoint = renderer.projectPoint(position)
                let projectedCGPoint = CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y))
                let distance = projectedCGPoint.distance(to: strongSelf.focusPoint)
                if distance < 50 {
                    strongSelf.showToast(message: node.getTopMostParentNode().name!, font: .systemFont(ofSize: 30))
                }
            }
        }
    }