iOS ARKit:大尺寸物体似乎总是随着设备相机位置的变化而移动 [英] iOS ARKit: Large size object always appears to move with the change in the position of the device camera
问题描述
我正在创建一个 iOS ARKit 应用程序,我想在增强现实中放置一个大对象.
当我尝试将对象放置在特定位置时,它似乎总是随着相机位置的变化而移动,而且我无法通过更改相机位置从所有角度查看对象.
但是如果我将它的比例值减小到 0.001(减小对象的大小),我可以从各个角度查看对象,并且放置的对象的位置也不会改变到那个程度.
物体的边界框:-
宽度 = 3.66
高度 = 1.83
深度 = 2.438
模型/对象网址:-
问题的视频网址:-
https://drive.google.com/open?id=1E4euZ0ArEtj2Ffto1pAOfVZocN08E
尝试的方法:-
- 试图将对象放置在原点
modelNode?.position = SCNVector3(0, 0, 0)
- 试图将物体放置在距离设备摄像头一定距离的地方
modelNode?.position = SCNVector3(0, 0, -800)
- 尝试使用 worldTransform/localTransform 列的不同组合
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.3.x, hitTestResult.worldTransform.columns.3.y, hitTestResult.worldTransform.columns.3.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.2.x, hitTestResult.worldTransform.columns.2.y, hitTestResult.worldTransform.columns.2.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.1.x, hitTestResult.worldTransform.columns.1.y, hitTestResult.worldTransform.columns.1.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.1.x, hitTestResult.worldTransform.columns.2.y, hitTestResult.worldTransform.columns.3.z)
modelNode?.position = SCNVector3(hitTestResult.localTransform.columns.3.x, hitTestResult.localTransform.columns.3.y, hitTestResult.localTransform.columns.3.z)
但仍然没有运气.它似乎仍然随着设备摄像头移动,并没有卡在它放置的位置.
E预期结果:-
- 对象应具有实际大小(比例应为 1.0).它们应该不会降低比例值.
- 一旦放置在特定位置,它就不应随着设备摄像头的移动而移动.
- 随着设备摄像头的移动,可以从各个角度看到物体,而物体的位置没有任何变化.
我找到了问题的根本原因.该问题与我用于 AR 的模型有关.什么时候,我用此链接中提供的模型替换了模型:- https://developer.apple.com/augmented-reality/quick-look/.我没有遇到任何问题.因此,如果将来有人遇到此类问题,我会建议使用 Apple 提供的任何模型来检查问题是否仍然存在.
I am creating an iOS ARKit app where I wanted to place a large object in Augmented Reality.
When I am trying to place the object at a particular position it always appears to be moving with the change in camera position and I am not able to view the object from all angles by changing the camera position.
But if I reduce it's scale value to 0.001 (Reducing the size of the object), I am able to view the object from all angles and the position of the placed object also does not change to that extent.
Bounding Box of the Object:-
Width = 3.66
Height = 1.83
Depth = 2.438
Model/Object Url:-
https://drive.google.com/open?id=1uDDlrTIu5iSRJ0cgp70WFo7Dz0hCUz9D
Source Code:-
import UIKit
import ARKit
import SceneKit
class ViewController: UIViewController {
@IBOutlet weak var sceneView: ARSCNView!
private let configuration = ARWorldTrackingConfiguration()
private var node: SCNNode!
//MARK: - Life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.sceneView.showsStatistics = false
self.sceneView.debugOptions = [ARSCNDebugOptions.showFeaturePoints]
self.sceneView.automaticallyUpdatesLighting = false
self.sceneView.delegate = self
self.addTapGesture()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configuration.planeDetection = .horizontal
self.sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.sceneView.session.pause()
}
//MARK: - Methods
func addObject(hitTestResult: ARHitTestResult) {
let scene = SCNScene(named: "art.scnassets/Cube.obj")!
let modelNode = scene.rootNode.childNodes.first
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.3.x,
hitTestResult.worldTransform.columns.3.y,
hitTestResult.worldTransform.columns.3.z)
let scale = 1
modelNode?.scale = SCNVector3(scale, scale, scale)
self.node = modelNode
self.sceneView.scene.rootNode.addChildNode(modelNode!)
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.type = .omni
lightNode.position = SCNVector3(x: 0, y: 10, z: 20)
self.sceneView.scene.rootNode.addChildNode(lightNode)
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light?.type = .ambient
ambientLightNode.light?.color = UIColor.darkGray
self.sceneView.scene.rootNode.addChildNode(ambientLightNode)
}
private func addTapGesture() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didTap(_:)))
self.sceneView.addGestureRecognizer(tapGesture)
}
@objc func didTap(_ gesture: UIPanGestureRecognizer) {
let tapLocation = gesture.location(in: self.sceneView)
let results = self.sceneView.hitTest(tapLocation, types: .featurePoint)
guard let result = results.first else {
return
}
let translation = result.worldTransform.translation
guard let node = self.node else {
self.addObject(hitTestResult: result)
return
}
node.position = SCNVector3Make(translation.x, translation.y, translation.z)
self.sceneView.scene.rootNode.addChildNode(self.node)
}
}
extension float4x4 {
var translation: SIMD3<Float> {
let translation = self.columns.3
return SIMD3<Float>(translation.x, translation.y, translation.z)
}
}
GIF of the Problem:-
Video URL of the Problem:-
https://drive.google.com/open?id=1E4euZ0ArEtj2Ffto1pAOfVZocV08EYKN
Approaches Tried:-
- Tried to place the object at the origin
modelNode?.position = SCNVector3(0, 0, 0)
- Tried to place the object at some distance away from the device camera
modelNode?.position = SCNVector3(0, 0, -800)
- Tried with the different combinations of worldTransform/localTransform columns
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.3.x, hitTestResult.worldTransform.columns.3.y, hitTestResult.worldTransform.columns.3.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.2.x, hitTestResult.worldTransform.columns.2.y, hitTestResult.worldTransform.columns.2.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.1.x, hitTestResult.worldTransform.columns.1.y, hitTestResult.worldTransform.columns.1.z)
modelNode?.position = SCNVector3(hitTestResult.worldTransform.columns.1.x, hitTestResult.worldTransform.columns.2.y, hitTestResult.worldTransform.columns.3.z)
modelNode?.position = SCNVector3(hitTestResult.localTransform.columns.3.x, hitTestResult.localTransform.columns.3.y, hitTestResult.localTransform.columns.3.z)
But still of no luck. It still appears to be moving with the device camera and not stuck to a position where it has been placed.
Expected Result:-
- Object should be of actual size (Scale should be of 1.0). Their should be no reduction in the scale value.
- Once placed at a particular position it should not move with the movement of the device camera.
- Object can be seen from all angles with the movement of the device camera without any change in object position.
I found out the root cause of the issue. The issue was related to the Model which I was using for AR. When, I replaced the model with the one provided in this link:- https://developer.apple.com/augmented-reality/quick-look/. I was not facing any issues. So, if anyone face such type of issues in future I would recommend to use any of the model provided by Apple to check if the issue persists with it or not.
这篇关于iOS ARKit:大尺寸物体似乎总是随着设备相机位置的变化而移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!