ios - 如何确保 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()
}
解决方案
我已经从 GitHub 下载了 SamMiti-iOS Xcode 项目并对其进行了测试。使用源代码Utah Teapot
SamMiti-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
// ....
}
推荐阅读
- javascript - 条形图 - 条形填充和对齐
- docker - Google Container Engine:容器分析和第三方图像?
- html - 特定像素区域上具有不同 URL 的图像映射
- java - 如何明确告诉 Proguard 删除完整包?
- machine-learning - 为什么需要从随机森林的数据中随机选择样本?
- javascript - 如何在javascript文件中使用css代码
- angular - 打字稿:引用变量中的接口
- c# - Xamarin C# Android 中的异步函数 - 应用程序卡在线程中
- powershell - 尝试删除特定文件并记录删除
- django - 创建超级用户时,它将电子邮件放在用户名中,反之亦然