1

下午好,

首先,请原谅我(可能)非常基本的问题,我对 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)

任何建议将不胜感激。

提前致谢!

在此处输入图像描述

4

1 回答 1

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
    .
    .
    .
}
于 2019-07-08T11:49:55.027 回答