Swift Scenekit - 居中SCNText - getBoundingBoxMin:Max问题

Mat*_*ker 6 ios scenekit swift scntext

享受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)

}
Run Code Online (Sandbox Code Playgroud)

任何建议都非常感谢.

Mat*_*ker 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)

}
Run Code Online (Sandbox Code Playgroud)

我留下了几个全局变量,但应该有意义.

谢谢大家的帮助.


ric*_*ter 10

编辑:带有指针参数的函数在Swift中很糟糕,所以在Swift 3中,Apple用一个类型为元组属性替换了这个方法(以及相应的setter方法):

var boundingBox: (min: SCNVector3, max: SCNVector3) { get set }
Run Code Online (Sandbox Code Playgroud)

所以你可以写下这样的东西:

let (min, max) = textNode.boundingBox
Run Code Online (Sandbox Code Playgroud)

更普遍...

UnsafeMutablePointer可以通过传递inout引用作为参数来调用在Swift中取出out-parameter 类型的函数.因此对于此方法的Swift 2版本,或其他地方的类似方法:

_textNode.getBoundingBoxMin(&v1, max: &v2)
Run Code Online (Sandbox Code Playgroud)


Kam*_*pai 6

您可以使用此代码添加文本,它是用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()
    }
}
Run Code Online (Sandbox Code Playgroud)