下午好,
首先,请原谅我(可能)非常基本的问题,我对 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)
任何建议将不胜感激。
提前致谢!