3
votes

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
Is the buttonRef node geometry a SceneKit object (SCNSphere, SCNBox, etc) or something created in a 3D modelling tool and loaded as a dae file? All the SceneKit objects have an origin at 0,0,0 (by that I mean a sphere's centre is located at 0,0,0), it may be that the model you're using has geometry that is not centred about 0. A quick way to check would be to add a small SCNSphere as a child of your buttonRef node and confirm it appears where you think the origin of your geometry is. FWIW projectPoint returns expected 2D coords in my app.lock
Your comment makes sense. To test that I did this [scnView projectPoint:SCNVector3Zero]; or in other words I am projecting 0,0,0. The projection gives me (0, 375, 0). How can 0,0,0 on scenekit – that in my case is what is visible in the exact center of the camera be (0,375,0) or in fact (0,375) on UIKit? ... that is a point on the right down?Duck
Good test, I added that into my project, even switched to a orthographic projection and still got a 2D point in the middle of my view. Is it possible that your SCNView is much larger than the screen size? What does printing out your SCNView's frame/bounds give (in didLayoutView)? If your view was twice as wide as the screen, with the left half out of view, then that would explain the numbers you're getting.lock
that gives me (0,0,667,375) as expected. But anyway, like I said scenekit coordinate 0,0,0 that should be (333,187) is being reported back as (0,375) that is a total nonsense.Duck
Agreed, somethings not right and I'm out of ideas.lock

2 Answers

4
votes

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:

3
votes

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 :-)