首页 文章

如何使用 ARKit 在两个 ARSCNViews 中显示相同的场景?

提问于
浏览
2

我的视图控制器出现问题。我正在尝试创建一个使用 ARKit 在两个 AR 场景上显示的应用程序。我试图使用一个插座集合,但我收到类型为“[1]”的值的错误?没有会员。我开始使用 Swift,所以我不知道一些事情。

这是我的代码:

import UIKit
import SceneKit
import ARKit

class ViewController: UIViewController {
    @IBOutlet var bothEyes: [ARSCNView]!

    override func viewDidLoad() {
        super.viewDidLoad()

        let configuration = ARWorldTrackingConfiguration()
        configuration.planeDetection = .horizontal

        let cubeNode = SCNNode(geometry: SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0.0))
        cubeNode.position = SCNVector3(0, 0, -0.2)// in meters

        bothEyes.session.run(configuration)
        bothEyes.scene.rootNode.addChildNode(cubeNode)
    }
}

1 回答

  • 1

    您需要做的就是在两个ARSCNViews之间共享一个ARSession(并且,正如我之前所说,您需要一个委托):

    import UIKit
    import SceneKit
    import ARKit
    
    class ViewController: UIViewController, ARSCNViewDelegate {
    
        @IBOutlet weak var sceneView: ARSCNView!
        @IBOutlet weak var sceneView2: ARSCNView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            sceneView.delegate = self
            sceneView.showsStatistics = true
            let scene = SCNScene(named: "art.scnassets/ship.scn")!
            sceneView.scene = scene
            sceneView.isPlaying = true
    
            // SceneView2 Setup
            sceneView2.scene = scene
            sceneView2.showsStatistics = sceneView.showsStatistics
    
            // Now sceneView2 starts receiving updates
            sceneView2.isPlaying = true     
        }
    
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            let configuration = ARWorldTrackingConfiguration()
            sceneView.session.run(configuration)
        }
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            sceneView.session.pause()
        }
    }
    

    但要记住!帧速率60 fps现在在两个ARSCNViews(30 fps 30 fps)之间共享。

    我使用Horizontal Stack View来线性排列ARSCNViews

    在此输入图像描述

相关问题