I have a SCNView with SCNNode of SCNPlane geometry in front of my camera root node.
Over SCNView, in UIView, I am adding UIImages (Markers) - orange circles.
In Motion listener I am trying to position the Markers in a way so they will stick to the center of each edge of the Plane.
A proper Markers alignment - while device is in straight position:
I'm doing this using projection from SceneKit objects to UIView:
//world coordinates
let v1w = sm.node.convertPosition(sm.node.boundingBox.min,
to: self.sceneView.scene?.rootNode)
let v2w = sm.node.convertPosition(sm.node.boundingBox.max,
to: self.sceneView.scene?.rootNode)
//projected coordinates
let v1p = self.sceneView.projectPoint(v1w)
let v2p = self.sceneView.projectPoint(v2w)
//frame rectangle
let rect = CGRect.init(x: CGFloat(v1p.x), y: CGFloat(v2p.y),
width: CGFloat(v2p.x - v1p.x), height: CGFloat(v1p.y - v2p.y))
var frameOld = sm.marker.frame
switch sm.position
{
case .Top:
frameOld.origin.y = rect.minY - frameOld.size.height/2
frameOld.origin.x = rect.midX - frameOld.size.width/2
case .Bottom:
frameOld.origin.y = rect.maxY - frameOld.size.height/2
frameOld.origin.x = rect.midX - frameOld.size.width/2
case .Left:
frameOld.origin.y = rect.midY - frameOld.size.height/2
frameOld.origin.x = rect.minX - frameOld.size.width/2
case .Right:
frameOld.origin.y = rect.midY - frameOld.size.height/2
frameOld.origin.x = rect.maxX - frameOld.size.width/2
}
sm.marker.frame = frameOld
self.view.layoutSubviews()
Similar approach you can find here: Scene Kit: projectPoint calculated is displaced
So I would like those Markers to stick to the edges of the Plane during device motion. But there is the issue: when rotating device - Markers are drifting from the Plane edges
See video of an issue: https://youtu.be/XBgNDDX5ZI8
I have created a basic project on github to reproduce an issue: https://github.com/mgontar/SceneKitProjectionIssue