Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 213
Duck Avatar asked Mar 12 '23 04:03

Duck


1 Answers

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:

like image 132
rickster Avatar answered Mar 14 '23 17:03

rickster