0

我正在尝试在点击图片时显示 3D 模型(请参见此处)。图片被点击,它继续3DFloorPlanViewController(嵌入在导航控制器中),相机工作但没有显示 3D 对象(见这里)。

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

故事板图片

我的 3D 对象位于名为“3D 对象”的文件夹中(参见此处)。我已经尝试了这两种功能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
    }
}
4

1 回答 1

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
}
于 2019-04-11T13:37:26.977 回答