【问题标题】:How to pin a label / UIView to a node in ARKit如何将标签/ UIView 固定到 ARKit 中的节点
【发布时间】:2018-12-02 12:03:08
【问题描述】:

我希望在 ARKit 中的节点顶部保留一个 UILabel,类似于 iOS 12 的 Measure 应用程序中的尺寸标签。 我尝试将标签添加为平面节点并使用广告牌约束,但是当您离开时文本会变小,这并不理想。 在 WWDC,他们将其称为屏幕空间,但没有说明如何实现它。

提前致谢!

【问题讨论】:

    标签: ios iphone swift uiview arkit


    【解决方案1】:

    我想出了以下解决方案来让它发挥作用。

    首先,我创建 UILabel 并将其添加为子视图。接下来,我将要跟随的节点的位置转换为renderer(_: updateAtTime:) 中的屏幕坐标。现在标签正确地跟随节点并保持固定的比例。但是,标签与屏幕保持水平,这看起来很奇怪。为了使其与世界保持水平,我根据 ARCamera 的偏航(z 旋转)旋转标签。

    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
    
        // Convert the node's position to screen coordinates
        let screenCoordinate = self.sceneView.projectPoint(node.position)
    
        DispatchQueue.main.async {
        // Move the label 
        label.center = CGPoint(x: CGFloat(screenCoordinate.x), y: CGFloat(screenCoordinate.y))
    
        // Hide the label if the node is "behind the screen"
        label.isHidden = (screenCoordinate.z > 1)
    
        // Rotate the label
        if let rotation = sceneView.session.currentFrame?.camera.eulerAngles.z {
            label.transform = CGAffineTransform(rotationAngle: CGFloat(rotation + Float.pi/2))
        }
        }
    
    }
    

    【讨论】:

    • 嘿 Leonard,你从哪里得到 nodecenterVector
    • @Cesare 该节点也是您要附加叠加层的节点。 CenterVector 应该是 screenCoordinate,我更新了我的答案。
    【解决方案2】:

    尝试使用 SCNText 几何,几个月前我使用过以下代码:

        var txtNote    = SCNText()
        txtNote.string = "Hello AR word!"
        txtNote.font   = UIFont.systemFont(ofSize: 2)
        txtNote.extrusionDepth = 1.0
        txtNote.containerFrame = CGRect(x: 2.5, y: 0, width: 16, height: 10)
        txtNote.isWrapped      = true
        txtNote.alignmentMode  = kCAAlignmentLeft
        txtNote.materials.first?.diffuse.contents = UIColor.black.cgColor
    
        var txtNode = SCNNode(geometry: txtNote)
    

    【讨论】:

    • 谢谢,但这只会创建文本节点。我的问题是关于如何使文本或任何 UIView 粘在节点上。当您使用 Apple 的 Measure 应用程序来回移动/在场景中四处走动时,文本不会改变大小并且始终面向相机。
    • SCNText 是具有字符串属性的几何图形,您可以在运行时更改(代码中的“Hello AR word!”)并附加到 SCNNode。为了使节点跟随相机,有 SCNBillboardConstraint
    • 我已经描述了为什么在我原来的问题中使用广告牌约束不是正确的解决方案
    猜你喜欢
    • 2018-01-30
    • 2018-07-19
    • 2018-12-09
    • 1970-01-01
    • 2019-04-02
    • 2021-03-16
    • 2013-01-29
    • 2011-06-17
    • 2019-06-17
    相关资源
    最近更新 更多