Scenekit - Calculate radius of projected sphere in screen pixels to positioning label always below node

220 Views Asked by At

How can I calculate the radius of a projected sphere in screen pixels to position the label below my SCNNode? Currently I place the label in the center of the node. My SCNCamera has usesOrthographicProjection = false, because the user can move the camera in space and further objects should appear smaller.

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    if let pointOfView = self.generalSceneView.pointOfView,
       self.generalSceneView.isNode( celestialBody.presentation, insideFrustumOf: pointOfView) {
                         
        let centerCooordinates =  self.generalSceneView.projectPoint(celestialBody.worldPosition)
    
        DispatchQueue.main.async {
            celestialBody.label.center = CGPoint(x: CGFloat(centerCooordinates.x), y: CGFloat(centerCooordinates.y))
            celestialBody.label.isHidden = false
        }
    }
    else {
        DispatchQueue.main.async {
            celestialBody.label.isHidden = true
        }
    }
}

enter image description here

enter image description here

I tried to projectPoint the boundingBox corners to the view, but if I rotate the camera and look for example from above, the text is moving up to the center. After that I tried to project another point with the radius distance and calculated the difference in pixels. But again, if I change the camera position, the distance is of course changing too.

I'm looking for help to calculate the radius of the sphere in screen pixels. I think it's somehow possible with the cameras' projectionTransform property. Something like in this post: Radius of projected sphere in screen space But unfortunately I don't understand how to do this with the SCNCamera properties. Any help appreciated!

0

There are 0 best solutions below