开发一个ARKit应用,该应用将文本留给他人查看

时间:2018-11-01 05:15:49

标签: ios swift firebase arkit

我正在创建一个iOS AR应用程序,该应用程序将文本设置在特定的位置,并将其留在此处供其他人查看。有比我正在做的更好的方法吗?

目前,我已对其进行了设置,以便将文本保存到Firebase并通过设置相对于摄像机位置的节点来加载文本。我想知道是否有一种方法可以以与我正在执行的方式相似的方式保存ARAnchors,但这有可能吗?

我当前用于通过用户点击屏幕将文本保存到该位置的功能:

    /*
     * Variables for saving the user touch
     */
    var touchX : Float = 0.0
    var touchY : Float = 0.0
    var touchZ : Float = 0.0

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    // will be used for getting the text
        let textNode = SCNNode()
        var writing = SCNText()

    // gets the user’s touch upon tapping the screen
        guard let touch = touches.first else {return}
        let result = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitResult = result.last else {return}
        let hitTransform = SCNMatrix4.init(hitResult.worldTransform)
        let hitVector = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)

    // saves X, Y, and Z coordinates of touch relative to the camera
        touchX = hitTransform.m41
        touchY = hitTransform.m42
        touchZ = hitTransform.m43

    // Was thinking of adding the ability to change colors. Probably can skip next seven lines
        var colorArray = [UIColor]()
        colorArray.append(UIColor.red)

        writing = SCNText(string: input.text, extrusionDepth: 1)

        material.diffuse.contents = colorArray[0]
        writing.materials = [material]

    // modifies the node’s position and size
        textNode.scale = SCNVector3(0.01, 0.01, 0.01)
        textNode.geometry = writing
        textNode.position = hitVector
        sceneView.scene.rootNode.addChildNode(textNode)

    // last few lines save the info to Firebase
        let values = ["X" : touchX, "Y" : touchY, "Z" : touchZ, "Text" : input.text!] as [String : Any]
        let childKey = reference.child("Test").childByAutoId().key

        if input.text != nil && input.text != "" {
            let child = reference.child("Test").child(childKey!)
            child.updateChildValues(values)
        } else {
            let child = reference.child("Test").child(childKey!)
            child.updateChildValues(values)
        } // if
    } // override func

    /*
     * Similar to the previous function but used in next function
     */
    func placeNode(x: Float, y: Float, z: Float, text: String) -> Void {
        let textNode = SCNNode()
        var writing = SCNText()

        let hitVector = SCNVector3Make(x, y, z)

        touchX = x
        touchY = y
        touchZ = z

        var colorArray = [UIColor]()
        colorArray.append(UIColor.red)

        writing = SCNText(string: text, extrusionDepth: 1)

        material.diffuse.contents = colorArray[0]
        writing.materials = [material]

        textNode.scale = SCNVector3(0.01, 0.01, 0.01)
        textNode.geometry = writing
        textNode.position = hitVector
        sceneView.scene.rootNode.addChildNode(textNode)
    } // func

 /*
  *  This next function is used in my viewDidLoad to load the data
  */
    func handleData() {
        reference.child("Test").observeSingleEvent(of: .value, with: { (snapshot) in
            if let result = snapshot.children.allObjects as? [DataSnapshot] {
                for child in result {
                    let xCoord = Float(truncating: child.childSnapshot(forPath: "X").value as! NSNumber)
                    let yCoord = Float(truncating: child.childSnapshot(forPath: "Y").value as! NSNumber)
                    let zCoord = Float(truncating: child.childSnapshot(forPath: "Z").value as! NSNumber)

                    let inscription = child.childSnapshot(forPath: "Text").value

                    self.placeNode(x: xCoord , y: yCoord , z: zCoord , text: inscription as! String)
                } // for
            } // if
        }) // reference
    } // func

我研究了一些东西,例如ARCore,但看起来它使用了Objective-C。我已经在Swift中制作了这个应用程序,但不确定是否可以将ARCore与实现当前应用程序的方式结合起来。

我是否只需要克服它并学习Objective-C?我还能继续使用我现有的东西吗?

1 个答案:

答案 0 :(得分:1)

我认为ARCore锚仅可使用24小时,因此可能是一个问题。

您可能需要使用ARKit2.0的ARWorldMap并将其另存为Firebase上的数据,以供其他人在同一位置查看文本,否则您将在代码中假设将来的用户将在完全相同的位置启动AR会话和方向作为离开文本的人。您可能需要首先使用核心位置来查看用户在世界上的位置。