首页 > 解决方案 > 如何确保 OBJ 模型以正确的方向显示?

问题描述

我一直忙于制作一个应用程序,以在 swift4 中使用 ARKit 动态显示 OBJ 模型。当前的问题是每当我在应用程序中加载模型时,我都会得到错误的方向。一个模型是颠倒的,另一个是等等……下面是问题的一个例子。如您所见,模型应该围绕 X 轴旋转 90 度,但正如我之前提到的,每个模型都有自己的问题。

有人可以帮我解决这个问题吗?

在此处输入图像描述

更新:我正在使用这个框架https://github.com/prolificinteractive/SamMitiAR-iOS 我像这样加载模型:

let virtualObject = SamMitiVirtualObject(refferenceNode: SCNReferenceNode.init(url:modelUrl as URL)! , allowedAlignments: [.horizontal])

接着:

virtualObjectLoader.loadVirtualObject(virtualObject) { loadedObject in
        loadedObject.scaleRange = (0.00001)...0.0002

        self.sceneView.currentVirtualObject = loadedObject
        self.sceneView.currentVirtualObject?.contentNode?.opacity = 0

        SceneKitAnimator.animateWithDuration(duration: 0.35, animations: {
            self.sceneView.currentVirtualObject?.contentNode?.opacity = 1
        })

这就是 viewDidLoad

override func viewDidLoad() {

    super.viewDidLoad()
    sceneView.placingMode = .focusNode

    sceneView.samMitiARDelegate = self
    sceneView.isAutoFocusEnabled = false
    sceneView.isLightingIntensityAutomaticallyUpdated = true

    if #available(iOS 12.0, *) {
        sceneView.environmentTexturing = .automatic
        sceneView.lightingEnvironmentContent = nil
        sceneView.baseLightingEnvironmentIntensity = 6

    } else {
        sceneView.environmentTexturing = .none
        sceneView.lightingEnvironmentContent = "art.scnassets/hdr-room.jpg"
        sceneView.baseLightingEnvironmentIntensity = 1.5
    }

    sceneView.initialPreviewObjectOpacity = 0.667
    sceneView.initialPreviewObjectMaxSizeRatio = CGSize(width: 0.667, height: 0.667)
    sceneView.allowedGestureTypes  = [.tap, .pan, .rotation, .pinch]



override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    initializeSamMiti()


}

override func viewDidAppear(_ animated: Bool) {

}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Pause the view's AR session.
    sceneView.session.pause()
}

@IBAction func crossButtonDidTouch(_ sender: Any) {
    print("close button got touched")
    dismiss(animated: true, completion: nil)
}


extension ARViewController: SamMitiARDelegate {

/// Example of using delegate for haptic feedback when object was placed
func samMitiViewDidPlace(_ virtualObject: SamMitiVirtualObject) {
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.impactOccurred()
}

/// Example of using delegate for haptic feedback when object scaling is snapped
@objc(samMitiVirtualObject:didSnapToScalingFactor:) func samMitiVirtualObject(_ virtualObject: SamMitiVirtualObject, didSnapToScalingFactor didSnappedToScalingFactor: Float) {
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.impactOccurred()
}

/// Example of using delegate for haptic feedback when scaling to bound
func samMitiVirtualObject(_ virtualObject: SamMitiVirtualObject, didScaleToBound: Float) {
    let generator = UIImpactFeedbackGenerator(style: .light)
    generator.impactOccurred()
}

标签: iosswiftswift4augmented-realityarkit

解决方案


我已经从 GitHub 下载了 SamMiti-iOS Xcode 项目并对其进行了测试。使用源代码Utah TeapotSamMiti-iOS 应用程序可以正常工作。我认为问题在于您的 3D 模型。

所以你需要在 Xcode 中打开你的模型Scene graph并适当地旋转/缩放它。

在此处输入图像描述

在(左面板)中选择一个模型Project Navigator,然后转到Show the Node inspector(右面板)并围绕 X 轴旋转它。Scene Graph View在视口的最底部显示/隐藏一个使用小矩形按钮。

您也可以以编程方式执行此操作:

private func initializeModel() {

    let virtualObject = SamMitiVirtualObject(refferenceNode: SCNReferenceNode(named: "art.scnassets/nameOfModel.scn")!,
                                          allowedAlignments: [.horizontal])

    // retrieving a model
    let yourModel = scene.rootNode.childNode(withName: "nameOfModelInSceneGraph",
                                          recursively: true)!
    yourModel.rotation.x = -90

    // ....
}

推荐阅读