享受SCNText上的alignmentMode选项。谷歌搜索周围看起来像alignmentMode和containerFrame有问题。我发现的替代方案建议使用获取边界框功能来查找文本大小,然后相应地手动调整。很棒,除了我不能让功能工作。当我试图得到两个向量时,我得到一个错误:
'SCNVector3'无法转换为'UnsafeMutablePointer< SCNVector3>'
我在几何和节点上得到了它。代码示例如下
func setCounterValue(counterValue:Int) {
var v1 = SCNVector3(x: 0,y: 0,z: 0)
var v2 = SCNVector3(x: 0,y: 0,z: 0)
_counterValue = counterValue
let newText = SCNText(string: String(format: "%06d", counterValue), extrusionDepth:sDepth)
newText.font = UIFont (name: "Arial", size: 3)
newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
newText.firstMaterial!.specular.contents = UIColor.whiteColor()
newText.getBoundingBoxMin(v1, max: v2)
_textNode = SCNNode(geometry: newText)
_textNode.getBoundingBoxMin(v1, max: v2)
}
任何建议都非常感谢。
答案 0 :(得分:12)
好的,所以我的最终代码解决方案如下:
func setCounterValue(counterValue:Int) {
var v1 = SCNVector3(x: 0,y: 0,z: 0)
var v2 = SCNVector3(x: 0,y: 0,z: 0)
_textNode.removeFromParentNode()
_counterValue = counterValue
let newText = SCNText(string: String(format: "%08d", counterValue), extrusionDepth:sDepth)
newText.font = UIFont (name: "Arial", size: 3)
newText.firstMaterial!.diffuse.contents = UIColor.whiteColor()
newText.firstMaterial!.specular.contents = UIColor.whiteColor()
_textNode = SCNNode(geometry: newText)
_textNode.getBoundingBoxMin(&v1, max: &v2)
let dx:Float = Float(v1.x - v2.x)/2.0
let dy:Float = Float(v1.y - v2.y)
_textNode.position = SCNVector3Make(dx, dy, Float(sDepth/2))
node.addChildNode(_textNode)
}
我已经离开了我的几个全局变量,但应该有意义。
感谢所有人的帮助。
答案 1 :(得分:10)
var boundingBox: (min: SCNVector3, max: SCNVector3) { get set }
所以你可以这样写:
let (min, max) = textNode.boundingBox
更一般地......
可以通过传递UnsafeMutablePointer
引用作为参数来调用在Swift中采用inout
类型的out参数的函数。因此,对于此方法的Swift 2版本,或其他地方的类似方法:
_textNode.getBoundingBoxMin(&v1, max: &v2)
答案 2 :(得分:0)
您可以使用以下代码添加文本,该文本以 Swift 5
编写import ARKit
class ARSceneViewController: UIViewController {
@IBOutlet var sceneView: ARSCNView!
let config = ARWorldTrackingConfiguration()
private func addTextToTheWorld() {
let text = SCNText(string: "HELLO WORLD", extrusionDepth: 0.02)
let font = UIFont(name: "Futura", size: 0.22)
text.font = font
text.alignmentMode = CATextLayerAlignmentMode.center.rawValue
text.firstMaterial?.diffuse.contents = UIColor.red
text.firstMaterial?.specular.contents = UIColor.white
text.firstMaterial?.isDoubleSided = true
text.chamferRadius = 0.01
let (minBound, maxBound) = text.boundingBox
let textNode = SCNNode(geometry: text)
textNode.pivot = SCNMatrix4MakeTranslation( (maxBound.x - minBound.x)/2, minBound.y, 0.02/2)
textNode.scale = SCNVector3Make(0.1, 0.1, 0.1)
textNode.position = SCNVector3(0.1, 0.1, -0.1)
sceneView.scene.rootNode.addChildNode(textNode)
}
// MARK: - View lifeCycle methods
override func viewDidLoad() {
super.viewDidLoad()
//ARKit Debugging:
// Show Axis and feature points
sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints, ARSCNDebugOptions.showWorldOrigin]
sceneView.session.run(config)
self.addTextToTheWorld()
}
}