将嵌套的SCNNode位置转换为屏幕空间中的CGPoint

4
我将尝试将嵌套的SCNNode的3D位置转换为x,y(CGPoint)坐标(用于在对象上叠加视图)。
我的SCNNode结构如下:
      +-----------+
      | Root node |
      +-----+-----+
            |
            |
       +----+---+
    +--+ Node A +--+
    |  +--------+  |
    |              |
    |              |
    |              |
+---+----+    +----+---+
| Node B |    | Node C |
+--------+    +--------+

我想获取NodeB在屏幕上的2D位置。我知道convertPosition:projectPoint:方法,但我认为我没有正确使用它们。
有任何指针吗?
编辑:
目前尝试类似这样的代码:
SCNVector3 rootPosition = [[[[self view3D] scene] rootNode] convertPosition:[nodeB position] fromNode:nodeB];
SCNVector3 projectedPoint = [[self view3D] projectPoint:rootPosition];
NSLog(@"x %f y %f z %f", projectedPoint.x, projectedPoint.y, projectedPoint.z);

生成的 x、y、z 值偏差很大(有时甚至为负数,在屏幕空间中这是没有意义的,因为对象显然在视野内)。

你能发布一下你目前的进展吗? - wogsland
@wogsland 用代码编辑过。 - jxd
2个回答

2

NodeB的位置是以NodeA的坐标系为参考的。因此,我相信您想要

rootPosition = [rootNode convertPosition:nodeB.position fromNode:nodeA]

2

On swift4:

nodeAchildPosition = scnScene.rootNode.convertPosition(nodeA.position, from: nodeAchild)

这个答案是获取nodeB的位置还是nodeA的位置?(看起来与Hal Mueller的答案相反!)而且,你不能只使用nodeB.parent作为“from”参数吗?有没有什么原因呢? - livingtech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接