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
Model/Object Url:-
https://drive.google.com/open?id=1uDDlrTIu5iSRJ0cgp70WFo7Dz0hCUz9D
源代码:-
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:-
问题视频URL:-
https://drive.google.com/open?id=1E4euZ0ArEtj2Ffto1pAOfVZocV08EYKN
尝试过的方法:-
- 试图将对象放置在原点
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 的模型有关。什么时候,我用这个 link:- https://developer.apple.com/augmented-reality/quick-look/ 中提供的模型替换了模型。我没有遇到任何问题。因此,如果将来有人遇到此类问题,我建议使用 Apple 提供的任何模型来检查问题是否仍然存在。
我遇到了同样的问题。
每当我尝试更改 .usdz 类型(实际上是加密和压缩类型)模型中的任何内容时,我们无法编辑或更改其中的任何内容。如果我编辑或更改一点位置,那么它的行为方式与问题中突出显示的方式相同。
为了解决这个问题,我只是将旧模型 .usdz
移到了垃圾箱,然后(再次)将原始文件复制到 XCode,然后就可以了。
与接受的答案中所述不同,问题可能与跟踪质量或模型中的错误无关。看起来模型没有正确放置在地面上,可能是由于轴心点位置错误,模型的某些部分留在地下。所以当你移动相机时,由于地下部分没有被地板遮挡,所以看起来它在移动。看看这张照片:
Apple 提供的模型的枢轴点位置正确,因此当它放在地面上的平面顶部时,它的部分保持在地面以上。
如果正确定位模型的枢轴点,它应该可以正常工作,与模型类型无关。
我正在创建一个 iOS ARKit 应用程序,我想在增强现实中放置一个大对象。
当我尝试将物体放置在特定位置时,它似乎总是随着相机位置的变化而移动,我无法通过改变相机位置从所有角度查看物体。
但是如果我将它的缩放值减小到 0.001(减小对象的大小),我就可以从各个角度查看对象,并且放置的对象的位置也不会改变到那个程度。
对象的边界框:-
宽度 = 3.66
身高=1.83
深度 = 2.438
Model/Object Url:-
https://drive.google.com/open?id=1uDDlrTIu5iSRJ0cgp70WFo7Dz0hCUz9D
源代码:-
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:-
问题视频URL:-
https://drive.google.com/open?id=1E4euZ0ArEtj2Ffto1pAOfVZocV08EYKN
尝试过的方法:-
- 试图将对象放置在原点
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 的模型有关。什么时候,我用这个 link:- https://developer.apple.com/augmented-reality/quick-look/ 中提供的模型替换了模型。我没有遇到任何问题。因此,如果将来有人遇到此类问题,我建议使用 Apple 提供的任何模型来检查问题是否仍然存在。
我遇到了同样的问题。
每当我尝试更改 .usdz 类型(实际上是加密和压缩类型)模型中的任何内容时,我们无法编辑或更改其中的任何内容。如果我编辑或更改一点位置,那么它的行为方式与问题中突出显示的方式相同。
为了解决这个问题,我只是将旧模型 .usdz
移到了垃圾箱,然后(再次)将原始文件复制到 XCode,然后就可以了。
与接受的答案中所述不同,问题可能与跟踪质量或模型中的错误无关。看起来模型没有正确放置在地面上,可能是由于轴心点位置错误,模型的某些部分留在地下。所以当你移动相机时,由于地下部分没有被地板遮挡,所以看起来它在移动。看看这张照片:
Apple 提供的模型的枢轴点位置正确,因此当它放在地面上的平面顶部时,它的部分保持在地面以上。
如果正确定位模型的枢轴点,它应该可以正常工作,与模型类型无关。