旋转文本MeshResource,使其不会被镜像

问题描述

添加无角度且具有固定世界位置的文本MeshResource时,从摄影机角度看它看起来不错。 但是,当用户走到文本实体的另一侧并转身时,它看起来像是镜像的。

我不想使用look(at_)API,因为我只想将其绕Y轴旋转180度,并且当用户再次通过它以将角度重置为0时。

解决方法

首先,我们必须将文本置于锚点中,即使旋转文本,锚点也将保持相同的方向。然后添加textIsMirrored变量,该变量将在更改后处理旋转:

class TextAnchor: Entity,HasAnchoring {
        let textEntity = ModelEntity(mesh: .generateText("text"))
        var textIsMirrored = false {
            willSet {
                if newValue != textIsMirrored {
                    if newValue == true {
                        textEntity.setOrientation(.init(angle: .pi,axis: [0,1,0]),relativeTo: self)
                    } else {
                        textEntity.setOrientation(.init(angle: 0,relativeTo: self)
                    }
                }
            }
        }
        
        required init() {
            super.init()
            textEntity.scale = [0.01,0.01,0.01]
            anchoring = AnchoringComponent(.plane(.horizontal,classification: .any,minimumBounds: [0.3,0.3]))
            addChild(textEntity)
        }
}

然后,在您的ViewController中,您可以创建将Camera作为目标的锚点,以便我们跟踪摄像机的位置并创建textAnchor

let cameraAnchor = AnchorEntity(.camera)
let textAnchor = TextAnchor()

要使其正常工作,您必须将其添加为场景的子级(最好是在viewDidLoad中):

arView.scene.addAnchor(cameraAnchor)
arView.scene.addAnchor(textAnchor)

现在,在ARSessionDelegate函数中,您可以检查相机相对于文本的位置,并在Z轴低于0时旋转它:

func session(_ session: ARSession,didUpdate frame: ARFrame) {
    if cameraAnchor.position(relativeTo: textAnchor).z < 0 {
        textAnchor.textIsMirrored = true
    } else {
        textAnchor.textIsMirrored = false
    }
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...