问题描述
当我观看第805节创建出色的AR体验"的视频时,他们提到在AR中显示文本时使用屏幕空间文本.他们以Measure应用程序为例,该应用程序运行良好.
When I went through the video for session 805, Creating Great AR Experiences, and they mention using screen space text when displaying text in AR. They use the example of their Measure app that works quite well.
我想实现类似的功能.但是,我无法弄清楚.
I want to achieve similar functionality. But, I am not able to figure it out.
推荐答案
屏幕空间是显示器的2D像素坐标系.在这里,您通常可以执行一些操作,例如在屏幕空间中布置UIKit小部件等.
Screen Space is the 2D pixel coordinate system of your display. It's where you normally do things like laying out UIKit widgets in screen space, etc.
屏幕空间"的替代方法是3D坐标空间,该空间与跟踪ARKit的3D世界或用于在AR中显示虚拟3D内容的3D渲染引擎有关. (在那种情况下,要考虑多个3D空间,例如绝对"世界空间与相对于相机的位置.)
The alternative to Screen Space is a 3D coordinate space that relates to the 3D world tracking ARKit does or the 3D rendering engine you use to display virtual 3D content in AR. (In that case there are multiple 3D spaces to consider, like "absolute" world space vs camera-relative positioning.)
如WWDC18谈话所述创造出色的AR体验,屏幕空间这是一种以固有的方式显示固有2D内容(如文本)的好方法,无论与该内容关联的现实功能的距离或方向如何. (如果您要将该文本标签放置在世界空间中,那么当您远离文本标签时,它会变得很小并且不可读.)
As mentioned in the WWDC18 talk Creating Great AR Experiences, screen space is a great way to display content that's inherently 2D (like text) in a way that remains readable regardless of the distance to or orientation of a real-world feature that content is associated with. (If you were to place that text label in world space, it'd get small and unreadable as you get far away.)
ARKit不会给您任何自动的方式来使2D屏幕空间内容遵循3D世界空间的位置,但是要自己实现这一点并不是特别困难.这是叠加跟随3D位置的UIView
的一般方法:
ARKit doesn't give you any automatic way to have 2D screen-space content follow a 3D world-space position, but making that happen yourself isn't especially difficult. Here's a general recipe for overlaying UIView
s that follow 3D positions:
- 在要显示屏幕空间内容的真实位置添加
ARAnchor
. - 使用每帧回调(
renderer(_:didUpdate:for:)
或renderer(_:updateAtTime:)
(如果您将SceneKit与,如果您要构建自己的渲染器,请session(_:didUpdate:)
)以获取该锚点的当前3D位置. - 投影3D位置(
ARSCNView
projectPoint
,ARCamera
projectPoint
)以获取2D点3D位置在屏幕上的显示位置. - 设置
UIView
的位置以匹配该点.
- Add an
ARAnchor
at the real-world position where you want screen-space content to appear. - Use a per-frame callback (
renderer(_:didUpdate:for:)
orrenderer(_:updateAtTime:)
if you're using SceneKit withARSCNView
,session(_:didUpdate:)
if you're building your own renderer) to get the current 3D position of that anchor. - Project the 3D position (
ARSCNView
projectPoint
,ARCamera
projectPoint
) to get the 2D point where that 3D position appears on screen. - Set the position of your
UIView
to match that point.
对于更高级的情况,例如Measure应用如何使其屏幕空间标签平行于两个3D点之间的线,您需要投影两个端点并使用类似atan2
的函数来获取旋转角度标签.
For more advanced cases like how the Measure app makes its screen-space label parallel to the line between two 3D points, you'd need to project the two endpoints and use a function like atan2
to get an angle for rotating the label.
这篇关于ARKit中的屏幕空间是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!