向SCNSphere添加文本

时间:2019-07-06 14:27:21

标签: swift xcode arkit scnnode scntext

下午好,

首先,请原谅我(可能)一个非常基本的问题,我对AR世界还很陌生...

当用户点击屏幕时,我正在显示3D对象。当用户点击时,我已经将一个类似气泡的对象成功投影到了一个固定位置:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let bubble = SCNSphere(radius: 0.1)
    bubble.firstMaterial?.transparency = 0.5
    bubble.firstMaterial?.writesToDepthBuffer = false
    bubble.firstMaterial?.blendMode = .screen
    bubble.firstMaterial?.reflective.contents = #imageLiteral(resourceName: "bubble")

    let node = SCNNode(geometry: bubble)
    node.position = SCNVector3Make(0, 0.1, 0)

    let parentNode = SCNNode()
    parentNode.addChildNode(node)

    if let camera = sceneView.pointOfView {
        parentNode.position = camera.position

        // Animation like bubble
        let wait = SCNAction.wait(duration: 0.2)

        let speedsArray: [TimeInterval] = [0.5, 1.0, 1.5]
        let speed: TimeInterval = speedsArray[Int(arc4random_uniform(UInt32(speedsArray.count)))]

        let toPositionCamera = SCNVector3Make(0, 0, -2)
        let toPosition = camera.convertPosition(toPositionCamera, to: nil)

        let move = SCNAction.move(to: toPosition, duration: speed)
        move.timingMode = .easeOut
        let group = SCNAction.sequence([wait, move])
        parentNode.runAction(group) {

        }
    }
    sceneView.scene.rootNode.addChildNode(parentNode)
}

我现在想做的是在SCNSphere上显示一个数字。理想情况下,看起来像是打印在球体的表面上。

我尝试了以下代码,但发现,尽管呈现了文本,但在球体后几乎看不到它,并且比球体小很多(我附上一张照片以尝试证明这一点)。我尝试过尝试不同的价值观,但是我觉得我在定位方面缺少一些重要的东西。

let string = "25"
    let text = SCNText(string: string, extrusionDepth: 0.1)
    text.font = UIFont.systemFont(ofSize: 5)
    text.firstMaterial?.diffuse.contents = UIColor.black
    text.flatness = 0.005
    let textNode = SCNNode(geometry: text)
    let fontScale: Float = 0.01
    textNode.scale = SCNVector3(fontScale, fontScale, fontScale)

    parentNode.addChildNode(textNode)

任何建议将不胜感激。

谢谢!

enter image description here

1 个答案:

答案 0 :(得分:1)

不确定是否会给您您真正想要的东西。这只是我将数字作为球体上的纹理的一种方式。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let bubble = SCNSphere(radius: 0.1)

    let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
    view.backgroundColor = .black
    let label = UILabel(frame: CGRect(x: 0, y: 35, width: 200, height: 30))
    label.font = UIFont.systemFont(ofSize: 30)
    view.addSubview(label)
    label.textAlignment = .center
    label.text = "25"
    label.textColor = .white

    bubble.firstMaterial?.diffuse.contents = view
    .
    .
    .
}