How to Convert a SCNVector3 position to a CGPoint SCNView coordinate?

6.6k Views Asked by At

I have this app that just works in landscape.

I have an object on scenekit. That object is at a certain position, specified by:

 SCNNode * buttonRef = [scene.rootNode childNodeWithName:@"buttonRef" recursively:YES];
 SCNVector3 buttonRefPosition = [buttonRef position];

Now I need to convert that SCNVector3 to mainView 2D coordinates, where

 mainView = (SCNView *)self.view;

The scene is using orthogonal camera, so no perspective. The camera is the default camera. No rotation, no translation. Camera's orthographicScale is 5.4.

How do I convert that?

I have found other answers on SO about the reverse question, that is converting from CGPoint to SCNVector3 but doesn't sound obvious to me how to do the reverse, from SCNVector3 to CGPoint.

If the SCNView is a subclass of UIView, than (0,0) is the upper left point and in the case of the iPhone 6 landscape, (667, 375) is the lower right point.

This button of mine is at almost full right and middle of height. In terms of UIKit I would say that this button is at (600, 187) but when I do this:

SCNNode * buttonRef = [scene.rootNode childNodeWithName:@"buttonRef" recursively:YES];
SCNVector3 buttonRefPosition = [buttonRef position];

SCNVector3 projectedPoint = [mainView projectPoint: buttonRefPosition];

I get projectPoint equal to (7.8, 375, 0) (?)

and when I add a 10x10 view to the coordinate (7.8, 375) using UIKit, I get a view at the lower left!!

Like I said the app is operating in landscape only but these coordinate systems are all messed.

2

There are 2 best solutions below

3
rickster On BEST ANSWER

Converting model-space points to screen-space points is called projecting (because it's applying the camera's projection transform to map from a 3D space into a 2D one). The renderer (via the GPU) does it many, many times per frame to get the points in your scene onto the screen.

There's also a convenience method for when you need to do it yourself: projectPoint:

4
LaborEtArs On

Quite old question, but unfortunately @ricksters answer was no real help for me, as projectPoint: keeps delivering messed up view coordinates in my case too (the same problem as @SpaceDog described)!

However, I found, that in my case, the problem was, that I had applied a transform to my root node (turned the world upside down ;-)). This obviously lead to the wrong results from projectPoint:...

As I couldn't remove the transform, I had to adjust my input to projectPoint: accordingly.

SCNVector3  viewStartPosition = ...; // Some place in the scene
SCNVector3  viewStartPositionRel = SCNVector3FromSCNVector4(SCNMatrix4MultV(self.scene.rootNode.worldTransform, SCNVector4FromSCNVector3(viewStartPosition)));
// Vector from UISceneView 0/0 top/left to start point
SCNVector3  viewStartPositionInViewCoords = [self.sceneView projectPoint:viewStartPositionRel];

Unfortunately, Apple delivers no matrix mathematics for SCNMatrix4 matrices, so the used (really easy) matrix handling methods are also 'home made':

/*
 SCNVector4FromSCNVector3

 */
SCNVector4 SCNVector4FromSCNVector3(SCNVector3 pV) {
    return SCNVector4Make(pV.x, pV.y, pV.z, 0.0);
}

/*
 SCNVector3FromSCNVector4

 */
SCNVector3 SCNVector3FromSCNVector4(SCNVector4 pV) {
    return SCNVector3Make(pV.x, pV.y, pV.z);
}

/*
 SCNMatrix4MultV

 */
SCNVector4 SCNMatrix4MultV(SCNMatrix4 pM, SCNVector4 pV) {
    SCNVector4  r = {
        pM.m11 * pV.x + pM.m12 * pV.y + pM.m13 * pV.z + pM.m14 * pV.w,
        pM.m21 * pV.x + pM.m22 * pV.y + pM.m23 * pV.z + pM.m24 * pV.w,
        pM.m31 * pV.x + pM.m32 * pV.y + pM.m33 * pV.z + pM.m34 * pV.w,
        pM.m41 * pV.x + pM.m42 * pV.y + pM.m43 * pV.z + pM.m44 * pV.w
    };
    return r;
}

I've got no idea, if this is a bug in SceneKit, or if maybe it is forbidden to apply transforms to the rootNode... Anyway: Problem solved :-)