swift - ARSCNView 嵌入导航 Controller 时不显示对象

标签 swift uinavigationcontroller arkit arscnview

我试图在点击图片时显示 3D 模型 ( See here )。点击图片后,它会转到 3DFloorPlanViewController(嵌入在导航 Controller 中),相机可以工作,但没有显示 3D 对象 ( See here )。

我在没有导航 Controller 的情况下尝试了完全相同的代码,它工作得很好,所以我很困惑为什么它在嵌入导航 Controller 时不起作用。

Storyboard picture

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

这是 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
    }
}

最佳答案

目前您的场景 View 中没有设置场景

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
}

关于swift - ARSCNView 嵌入导航 Controller 时不显示对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53485811/

相关文章:

ios - 无法在 iPhone 6 中安装我的 ARKit 支持的应用程序,但苹果的演示应用程序在同一部 iPhone 上成功运行

ios - SpriteKit 场景在 iOS 8 上是空白的

ios - 如何从另一个 UIViewController 更改分段控件标题(设置)

ios - PopToRootViewController 在 UITabBarController 中显示不平衡消息

ios - 从 UIView 子类推送导航 Controller 会导致崩溃

swift - ARKit 拖动对象改变比例

来自 ARKit 深度缓冲区的 SceneKit 点云

ios - 如何使用 Metal 自定义计算着色器并获得非常流畅的性能?

ios - 单击更改 BarButtonItem 图标

iphone - iPhone 应用程序中的全局 ADBannerView