在SCNText上享受SCNText选项的乐趣。谷歌搜索了一下,看起来alignmentMode和containerFrame都有问题。我发现的替代方案建议使用get边界框函数来查找文本大小,然后手动进行相应调整。太好了,除了我不能让这个功能开始工作。当我试图得到这两个向量时,我得到了一个错误:
“SCNVector3”不能转换为“UnsafeMutablePointer”
我在几何和节点上都得到了这个结论。代码示例如下所示
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)
}任何建议都非常感谢。
发布于 2014-12-21 22:13:14
好的,我的最后一个代码解决方案看起来如下:
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)
}我离开了我的两个全局变量,但应该是有意义的。
谢谢大家的帮助。
发布于 2014-12-21 16:14:06
编辑:带指针外参数的函数在Swift中很糟糕,所以在Swift 3中,Apple用属性,其类型为元组。替换了这个方法(以及相应的setter方法)。
var boundingBox: (min: SCNVector3, max: SCNVector3) { get set }所以你只需写这样的东西:
let (min, max) = textNode.boundingBox更一般的..。
在Swift中接受UnsafeMutablePointer类型的输出参数的函数可以通过传递inout引用作为参数来调用。因此,对于Swift 2版本的这种方法,或者其他类似的方法:
_textNode.getBoundingBoxMin(&v1, max: &v2)发布于 2019-07-24 08:41:56
您可以使用此代码添加文本,它是用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()
}
}https://stackoverflow.com/questions/27590621
复制相似问题