在Arkit应用程序中设置3D文本的方向

时间:2018-06-04 10:39:01

标签: ios swift arkit

我基本上有两个3D积分。我们假设两点的坐标是:

(x: 1, y: 1, z: 1)(x: 2, y: 2, z: 2)

基本上,我有三行:

第1行:直接连接第1点和第2点。因此,此行的结束点为:

(x: 1, y: 1, z: 1)(x: 2, y: 2, z: 2)

Line2:这基本上是水平组件。所以,这一行的终点是:

(x: 1, y: 1, z: 1)(x: 2, y: 2, z: 1)

Line3:这基本上是垂直组件。所以,这一行的终点是:

(x: 1, y: 1, z: 1)(x: 1, y: 1, z: 2)

现在,我正在尝试添加3D文本。我想在我的行上面显示我的文字。因为,我有三行,我将显示三个文本。所以,我想知道文本的坐标(对于所有三条不同的线,它们不重叠)以及为文本设置的方向(这样它们对于用户始终是正确的可见,即使他旋转手机等)。

创建文本如下:

func AddText(pos1: SCNVector3?, pos2: SCNVector3?) {

    var txtNode = SCNNode()
    let d = self.distanceBetweenPoints(A: pos1!, B: pos2!)

    let distanceString = String(format: "%.2f", d)
    let txtGeometry = SCNText(string: distanceString, extrusionDepth: 1.0)

    txtGeometry.firstMaterial?.diffuse.contents = UIColor.red

    txtGeometry.alignmentMode  = kCAAlignmentCenter
    txtGeometry.truncationMode = kCATruncationMiddle

    txtNode.removeFromParentNode()
    txtNode = SCNNode(geometry: txtGeometry)

    txtNode.position = SCNVector3(((pos1?.x)! + (pos2?.x)!)/2, ((pos1?.y)! + (pos2?.y)!)/2, ((pos1?.z)! + (pos2?.z)!)/2)

    txtNode.scale = SCNVector3(0.005, 0.005, 0.005)
    sceneView.scene.rootNode.addChildNode(txtNode)
}

如果有人可以解释我在上面的功能中所做的更改,我会很高兴的。这样所有三个文本都清晰可见,并且不会相互重叠。

1 个答案:

答案 0 :(得分:0)

看看你的问题我假设你想要在你的两点之间和它们之间的线之上集中显示你的SCNText,并响应摄像机的变化位置,例如:始终面对当前的----------------------------- -- option #1: -- place yourself in the database use dbname go -- now run sp_helptext exec sp_helptext procedure_name go ----------------------------- -- option #2 -- preface the sp_helptext invocation with the name of the -- database in which you wish to run sp_helptext execute dbname..sp_helptext procedure_name go -----------------------------

因此我创建了一个pointOfView,可以相对轻松地处理这个问题:

SCNNode Subclass

如何使用它的示例如下(注意我没有在节点之间放置一条线)但是这应该指向正确的方向:

class TextNode: SCNNode{

    var textGeometry: SCNText!

    /// Creates An SCNText Geometry
    ///
    /// - Parameters:
    ///   - text: String (The Text To Be Displayed)
    ///   - depth: Optional CGFloat (Defaults To 1)
    ///   - font: UIFont
    ///   - textSize: Optional CGFloat (Defaults To 3)
    ///   - colour: UIColor
    init(text: String, depth: CGFloat = 1, font: String = "Helvatica", textSize: CGFloat = 0.1, colour: UIColor) {

        super.init()

        //1. Create A Billboard Constraint So Our Text Always Faces The Camera
        let constraints = SCNBillboardConstraint()

        //2. Create An SCNNode To Hold Out Text
        let node = SCNNode()
        let max, min: SCNVector3
        let tx, ty, tz: Float

        //3. Set Our Free Axes
        constraints.freeAxes = .Y

        //4. Create Our Text Geometry
        textGeometry = SCNText(string: text, extrusionDepth: depth)

        //5. Set The Flatness To Zero (This Makes The Text Look Smoother)
        textGeometry.flatness = 0

        //6. Set The Alignment Mode Of The Text
        textGeometry.alignmentMode = kCAAlignmentCenter

        //7. Set Our Text Colour & Apply The Font
        textGeometry.firstMaterial?.diffuse.contents = colour
        textGeometry.firstMaterial?.isDoubleSided = true
        textGeometry.font = UIFont(name: font, size: textSize)

        //8. Position & Scale Our Node
        max = textGeometry.boundingBox.max
        min = textGeometry.boundingBox.min

        tx = (max.x - min.x) / 2.0
        ty = min.y
        tz = Float(depth) / 2.0

        node.geometry = textGeometry
        node.scale = SCNVector3(0.001, 0.001, 0.001)
        node.pivot = SCNMatrix4MakeTranslation(tx, ty, tz)

        self.addChildNode(node)

        self.constraints = [constraints]

    }

    /// Places The TextNode Between Two SCNNodes
    ///
    /// - Parameters:
    ///   - nodeA: SCNode
    ///   - nodeB: SCNode
    func placeBetweenNodes(_ nodeA: SCNNode, and nodeB: SCNNode){

        let minPosition = nodeA.position
        let maxPosition = nodeB.position
        let x = ((maxPosition.x + minPosition.x)/2.0)
        let y = (maxPosition.y + minPosition.y)/2.0 + 0.01
        let z = (maxPosition.z + minPosition.z)/2.0
        self.position =  SCNVector3(x, y, z)
    }

    required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

}

如果您感兴趣,可以从这里获得更深入,更全面评论的示例:Measuring In ARKit

希望它有所帮助...