使用SceneKit时如何将文本放置在特定位置? [英] How to place text in the certain position when using SceneKit?

查看:164
本文介绍了使用SceneKit时如何将文本放置在特定位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遵循与圆柱体,box…相同的简单逻辑,因此仅通过定义textNode的位置即可,但是它不起作用.

I have tried to follow the same simple logic as for cylinder, box … , so just by defining the position for textNode, but it is not working.

func makeText(text3D: String, position: SCNVector3, depthOfText: CGFloat, color: NSColor, transparency: CGFloat) -> SCNNode
{
    let textTodraw = SCNText(string: text3D, extrusionDepth: depthOfText)
    textTodraw.firstMaterial?.transparency = transparency
    textTodraw.firstMaterial?.diffuse.contents = color
    let textNode = SCNNode(geometry: textTodraw)
    textNode.position = position        
    return textNode
}

推荐答案

SCNText的默认字体为 36点Helvetica ,字体大小的点"与场景空间的单位相同. (嗯,包含SCNText几何的节点的局部空间.但是,除非您在节点上设置了比例因子,否则局部空间单位与场景空间单位相同.)这意味着即使一个短标签也可以是数十个单位高和数百个单位的宽度.

The default font for SCNText is 36 point Helvetica, and a "point" in font size is the same as a unit of scene space. (Well, of local space for the node containing the SCNText geometry. But unless you've set a scale factor on your node, local space units are the same as scene space units.) That means even a short label can be tens of units tall and hundreds of units wide.

构建具有较小范围的SceneKit场景是很典型的-例如,简单的测试场景,例如您可能使用默认尺寸SCNBoxSCNSphere放在Swift操场上一起放在一起,等等,宽度可能只有3-4个单位. (如果您将SceneKit与ARKit一起使用,则场景单位为米,因此36点字体的文本等于市区几个办公区的大小.)

It's typical to build SceneKit scenes with smaller scope — for example, simple test scenes like you might throw together in a Swift playground using the default sizes for SCNBox, SCNSphere, etc might be only 3-4 units wide. (And if you're using SceneKit with ARKit, scene units are meters, so some text in 36 "point" font is the size of a few office blocks downtown.)

此外,相对于其包含节点的文本几何图形的锚点位于文本的左下角.将所有这些放在一起,很有可能在您的其余场景中隐约可见巨大的字母,只是隐藏在相机视野之外.

Also, the anchor point for a text geometry relative to its containing node is at the lower left corner of the text. Put all this together and it's entirely possible that there are giant letters looming over the rest of your scene, hiding just out of camera view.

请注意,如果尝试通过在SCNText上设置小得多的字体来解决此问题,则文本可能会变得参差不齐.这是因为 flatness 属性是相对于磅值进行测量的文字的大小(更准确地说,是在一个坐标系中测量的,其中一个单位==文字大小的一个点).因此,如果您选择的字体大小小于屏幕/打印标准,则需要相应地按比例缩小flatness,以使字母中的曲线保持平滑.

Note that if you try to fix this by setting a much smaller font on your SCNText, the text might get jagged and chunky. That's because the flatness property is measured relative to the point size of the text (more precisely, it's measured in a coordinate system where one unit == one point of text size). So if you choose a font size that'd be tiny by screen/print standards, you'll need to scale down the flatness accordingly to still get smooth curves in your letters.

或者,您可以不考虑字体大小和平坦度,而是设置包含文本几何的节点上的scale 因子,或设置该节点的

Alternatively, you can leave font sizes and flatness alone — instead, set a scale factor on the node containing the text geometry, or set that node's pivot to a transform matrix that scales down its content. For example, if you set a scale factor of 1/72, one unit of scene space is the same as one "inch" (72 points) of text height — depending on the other sizes in your scene, that might make it a bit easier to think of font sizes the way you do in 2D.

这篇关于使用SceneKit时如何将文本放置在特定位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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