将场景(x,y)转换为屏幕(x,y) [英] Convert Scene's (x, y) to screen's (x, y)

查看:69
本文介绍了将场景(x,y)转换为屏幕(x,y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SceneKit构建的应用程序,当前正在显示多个节点.我可以弄清楚按下了哪个节点,并希望使用它来使标签显示在被触摸的节点下方.现在,当我通过以下方式设置标签的中心时...

I have an application built with SceneKit that is currently displaying several nodes. I can figure out which node is pressed and want to use that to make a label appear below the Node that was touched. Now, when I set the label's center the following way...

nameLabel.center = CGPointMake(CGFloat(result.node.position.x), CGFloat(result.node.position.y+20)

...由于节点位于(1,0)上,因此它出现在左上角.我发现, sceneView的 (0,0)在屏幕的中心中,而实际的物理显示(0,0)位于左上角.

…it appears in the upper left corner since the node is on (1, 0). What I figured is that the sceneView's (0, 0) is in the center of the screen while the actual physical display's (0, 0) is in the top left corner.

有没有办法将两个不同的数字相互转换?(我可以硬编码,因为我知道节点的位置,或者为每个节点创建一个单独的标签,但这并不是一个完美的解决方案.)

Is there a way to convert the two different numbers into each other? (I could hardcode since I know where the Node's are or create a separate label for each Node but that is not really a perfect solution.)

先谢谢您了:)

推荐答案

您可以使用 projectPoint:方法:

var projected = view.projectPoint(result.node.position))   
//projected is an SCNVector3
//projected.x and y are the node's position in screen coordinates
//projected.z is its depth relative to the near and far clipping planes
nameLabel.center = CGPointMake(CGFloat(projected.x), CGFloat(projected.y+20)

这篇关于将场景(x,y)转换为屏幕(x,y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆