嵌入到导航控制器中时,ARSCNView不显示对象

时间:2018-11-26 17:00:12

标签: swift uinavigationcontroller arkit arscnview

点击图片(See here)时,我试图显示3D模型。点击图片,将其锁定到3DFloorPlanViewController(已嵌入导航控制器中)中,相机可以工作,但没有显示3D对象(See here)。

我已经在没有导航控制器的情况下尝试了相同的代码,并且它可以完美运行,所以我对为什么将其嵌入导航控制器时不起作用感到困惑。

Storyboard picture

我的3D对象位于名为“ 3D对象”(See here)的文件夹中。我已经在named: "3D Objects/paperPlane.scn"函数中尝试过named: "paperPlane.scn"addPaperPlane,但是都没有用。

这是3DFloorPlanViewController的代码:

import UIKit
import ARKit
import SceneKit

class _DFloorPlanViewController: UIViewController, ARSCNViewDelegate {
    @IBOutlet weak var sceneView: ARSCNView!

    override func viewDidLoad() {
        super.viewDidLoad()

        sceneView.delegate = self

        addPaperPlane()
        //addCar()
        configureLighting()
    }

    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()
    }

    func addPaperPlane(x: Float = 0, y: Float = 0, z: Float = -0.5) {
        guard let paperPlaneScene = SCNScene(named: "3D Objects/paperPlane.scn"), let paperPlaneNode = paperPlaneScene.rootNode.childNode(withName: "paperPlane", recursively: true) else { return }
        paperPlaneNode.position = SCNVector3(x, y, z)
        sceneView.scene.rootNode.addChildNode(paperPlaneNode)
    }

    func addCar(x: Float = 0, y: Float = 0, z: Float = -0.5) {
        guard let carScene = SCNScene(named: "car.dae") else { return }
        let carNode = SCNNode()
        let carSceneChildNodes = carScene.rootNode.childNodes

        for childNode in carSceneChildNodes {
            carNode.addChildNode(childNode)
        }

        carNode.position = SCNVector3(x, y, z)
        carNode.scale = SCNVector3(0.5, 0.5, 0.5)
        sceneView.scene.rootNode.addChildNode(carNode)
    }

    func configureLighting() {
        sceneView.autoenablesDefaultLighting = true
        sceneView.automaticallyUpdatesLighting = true
    }
}

1 个答案:

答案 0 :(得分:0)

当前没有场景设置为场景视图

let scene = SCNScene()
self.sceneView.scene = scene

通常尝试以下操作:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Set the view's delegate
    self.sceneView.delegate = self
    self.sceneView.showsStatistics = true
    self.sceneView.autoenablesDefaultLighting = true

    let scene = SCNScene()
    self.sceneView.scene = scene
}