首页 文章

ARKit 需要 30 秒才能开始跟踪?

提问于
浏览
-1

好的,在 iOS 11 中使用 Swift 探索 ARKit,我创建了一个非常简单的应用程序,只需在用户点击时添加节点:

override func viewDidLoad() {
        super.viewDidLoad()

        // Set the view's delegate
        sceneView.delegate = self

        // Show statistics such as fps and timing information
        sceneView.showsStatistics = true

        // Create a new scene
        let actualScene = SCNScene(named: "art.scnassets/ship.scn")!

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last else { return }
        let hitTransform = SCNMatrix4.init(hitFeature.worldTransform) // <- if higher than beta 1, use just this -> hitFeature.worldTransform

        let hitPosition = SCNVector3Make(hitTransform.m41,
                                         hitTransform.m42,
                                         hitTransform.m43)

        createBall(hitPosition: hitPosition)
    }
func createBall(hitPosition : SCNVector3) {
        let newBall = SCNSphere(radius: 0.01)
        let newBallNode = SCNNode(geometry: newBall)
        newBallNode.position = hitPosition
        self.sceneView.scene.rootNode.addChildNode(newBallNode)
    }

这很有效。我的问题是,当第一次运行应用程序时,需要 30-60 秒才能将相机平移到无法点击的地方。

似乎 ARKit 正在“加载”,因此当我在第一分钟点击时,没有节点出现在轻敲位置。第一分钟没有任何事情发生。

为什么是这样?有没有办法加快这个加载过程?这里发生了什么?

1 回答

  • 0

    在覆盖函数 viewWillAppear 上调用此函数

    func setUpSceneView() {
            let configuration = ARWorldTrackingConfiguration()
            configuration.planeDetection = .horizontal
    
            sceneView.session.run(configuration)
    
            sceneView.delegate = self
        }
    

    并删除 viewdidload 中的所有内容,并在视图中添加您需要的内容

相关问题