1

I am displaying a 3d model consisting of several SNNodes in SceneKit.

I am displaying a 2D SpriteKit node on the sceneview-overlay view. The 2D-node position should always be in front of a particular 3D SCNNode.

How can I map the 3D position to the SpriteKit 2D position?

I tried let screenPoint = sceneView.projectPoint(my3dNode), but this doesn't work: even when the my3dNode is in the center of the screen, the function returns very weird values (e.g. negative values or over 1000).

Do you have any ideas why?

Pascal
  • 2,590
  • 3
  • 21
  • 46

1 Answers1

7

It could be that it's projecting from the untranslated, unmodified node position, or that the node is a child of another node, thus changing its frame of reference. Try using myNode.presentation.worldPosition as the input for projectPoint.

[EDIT]

Reading a bit closer, would you not be better off creating a flat texture of your sprite, attach it to the node in question, give it a 'billboard' constraint and set its Z order so that it always appears in front?

Ash
  • 9,064
  • 3
  • 48
  • 59
  • @Ash is it same for converting 3d world coordinated point to 2d pixel coordinate system. In this case also i am getting same kind of results – Roshan Bade Aug 16 '19 at 04:11
  • That seems to be exactly what the OP was attempting to do, so if I'm understanding you correctly, then yes. The input for the `projectPoint` function must be in the world's frame of reference. In my example I am using a node's position, but any 3D vector representing a point in space should do. Essentially, it's just putting that value through the renderer's transformation matrix. If you need to go from 2D to 3D coordinates, use `unprojectPoint` instead. – Ash Aug 16 '19 at 08:10
  • Hi @Ash! Would you be able to help me with a similar ARkit/Scenekit related question:https://stackoverflow.com/questions/62783206/projecting-the-arkit-face-tracking-3d-mesh-to-2d-image-coordinates – swiftlearneer Jul 29 '20 at 19:33