图像未在场景中显示

时间:2018-01-29 07:10:31

标签: ios swift xcode swift4 arkit

我正在根据Augmented Reality制作应用。我已经以.obj格式从互联网上下载了一个3D对象,并在.scn中以Xcode格式进行了转换。当我运行应用程序相机开始但图像没有添加到场景中。我使用的是ARKit SceneKitView

import ARKit
import UIKit
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    addTapGestureToSceneView()
    configureLighting()
}

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


@IBOutlet weak var sceneView: ARSCNView!


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARWorldTrackingConfiguration()
    sceneView.session.run(configuration)
}


func addBox(x: Float = 0, y: Float = 0, z: Float = 0)
{
    guard let paperPlaneScene = SCNScene(named: "chair_f.scn"),
        let paperPlaneNode = paperPlaneScene.rootNode.childNode(withName: "chair_f",recursively: true)
        else { return }
    paperPlaneNode.position = SCNVector3(x, y, z)
    sceneView.scene.rootNode.addChildNode(paperPlaneNode)
}

func addTapGestureToSceneView() {
    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.didTap(withGestureRecognizer:)))
    sceneView.addGestureRecognizer(tapGestureRecognizer)
}

@objc func didTap(withGestureRecognizer recognizer: UIGestureRecognizer) {
    let tapLocation = recognizer.location(in: sceneView)
    let hitTestResults = sceneView.hitTest(tapLocation)
    guard let node = hitTestResults.first?.node else {
        let hitTestResultsWithFeaturePoints = sceneView.hitTest(tapLocation, types: .featurePoint)
        if let hitTestResultWithFeaturePoints = hitTestResultsWithFeaturePoints.first {
            let translation = hitTestResultWithFeaturePoints.worldTransform.translation
            addBox(x: translation.x, y: translation.y, z: translation.z)
        }
        return
    }
    node.removeFromParentNode()
}




override func viewWillDisappear(_ animated: Bool) {
    super.viewWillAppear(animated)
    sceneView.session.pause()
}
}
extension float4x4 {
var translation: float3 {
    let translation = self.columns.3
    return float3(translation.x, translation.y, translation.z)
}
}

3 个答案:

答案 0 :(得分:0)

将您的Initialization文件放在File -> New -> Project from Version Control -> Git中,然后按以下方式访问

The directory <project-name> already exists. Please choose another directory.

感谢。

答案 1 :(得分:0)

如果您有来自外部源的动态.obj文件,或者您希望将项目中已有的.obj导入到场景视图中,我建议您将.obj转换为.scn,如下所示:

    import SceneKit.ModelIO

    var downloadedNode = SCNNode()


    func convertToScn(sourceUrl: URL){
    let asset = MDLAsset(url: sourceUrl)

    guard let object = asset.object(at: 0) as? MDLMesh else { return }
    downloadedNode = SCNNode.init(mdlObject: object
    //Do what you require in the application using the downloadedNode. Maybe add to your root node.
    }

像这样使用:

self.convertToScn(destinationUrl: destinationObjectUrl)

这里的destinationObjectUrl是.scn / .obj文件位置的URL。

答案 2 :(得分:0)

同样的事情发生在我身上,我使用.DAE模型并导出到SCN文件为我工作。