简体   繁体   English

如何在ARKit中设置文本方向?

[英]How can I set text orientation in ARKit?

I am creating a simple app with ARKit in which I add some text to the scene to the tapped position: 我正在用ARKit创建一个简单的应用程序,在其中我将一些文本添加到场景中的tapped位置:

@objc func tapped(sender: UITapGestureRecognizer){
    let sceneView = sender.view as! ARSCNView
    let tapLocation = sender.location(in: sceneView)
    let hitTest = sceneView.hitTest(tapLocation, types: .featurePoint)
    if !hitTest.isEmpty{
        self.addTag(tag: "A", hitTestResult: hitTest.first!)
    }
    else{
        print("no match")
    }
}

func addTag(tag: String, hitTestResult: ARHitTestResult){
    let tag = SCNText(string:tag, extrusionDepth: 0.1)
    tag.font = UIFont(name: "Optima", size: 1)
    tag.firstMaterial?.diffuse.contents = UIColor.red

    let tagNode = SCNNode(geometry: tag)

    let transform = hitTestResult.worldTransform
    let thirdColumn = transform.columns.3
    tagNode.position = SCNVector3(thirdColumn.x,thirdColumn.y - tagNode.boundingBox.max.y / 2,thirdColumn.z)
    print("\(thirdColumn.x) \(thirdColumn.y) \(thirdColumn.z)")
    self.sceneView.scene.rootNode.addChildNode(tagNode)
}

It works, but I have problem with the orientation of the text. 它有效,但我对文本的方向有问题。 When I add it with the camera's original position, the text orientation is ok, I can see the text frontwise (Sample 1). 当我用相机的原始位置添加它时,文本方向正常,我可以正面看到文本(样本1)。 But when I turn camera to the left / right, and add the text by tapping, I can see the added text from the side (Sample 2). 但是当我向左/向右转动相机并通过点击添加文本时,我可以从侧面看到添加的文本(示例2)。

Sample 1: 样本1:

前面的三维字母

Sample 2: 样本2:

侧面有3-D字母

I know there should be some simple trick to solve it, but as a beginner in this topic I could not find it so far. 我知道应该有一些简单的技巧来解决它,但作为这个主题的初学者我到目前为止找不到它。

You want the text to always face the camera? 您希望文本始终面向相机吗? SCNBillboardConstraint is your friend: SCNBillboardConstraint是你的朋友:

tagNode.constraints = [SCNBillboardConstraint()]

Am I correct in saying that you want the text to face the camera when you tap (wherever you happen to be facing), but then remain stationary? 我是否正确地说当你点击(无论你碰巧面对的地方)时你想要文本面对镜头,但是然后保持静止?

There are a number of ways of adjusting the orientation of any node. 有许多方法可以调整任何节点的方向。 For this case I would suggest simply setting the eulerAngles of the text node to be equal to those of the camera, at the point in which you instantiate the text. 对于这种情况,我建议在实例化文本时简单地将文本节点的eulerAngles设置为等于摄像机的eulerAngles

In your addTag() function you add: addTag()函数中添加:

let eulerAngles = self.sceneView.session.currentFrame?.camera.eulerAngles
tagNode.eulerAngles = SCNVector3(eulerAngles.x, eulerAngles.y, eulerAngles.z + .pi / 2)

The additional .pi / 2 is there to ensure the text is in the correct orientation, as the default with ARKit is for a landscape orientation and therefore the text comes out funny. 附加的.pi / 2用于确保文本的方向正确,因为ARKit的默认设置是横向方向,因此文本很有趣。 This applies a rotation around the local z axis. 这适用于围绕局部z轴的旋转。

It's also plausible (and some may argue it's better) to use .localRotate() of the node, or to access its transform property, however I like the approach of manipulating both the position and eulerAngles directly. 使用节点的.localRotate()或访问其transform属性也是合理的(有些人可能认为它更好),但我喜欢直接操纵位置和eulerAngles的方法。

Hope this helps. 希望这可以帮助。

EDIT: replaced Float(1.57) with .pi / 2 . 编辑:用.pi / 2替换Float(1.57)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM