iOS ARKit: Large size object always appears to move with the change in the position of the device camera

雨燕双飞 提交于 2021-02-18 16:44:23

问题


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:-

  1. Tried to place the object at the origin
 modelNode?.position = SCNVector3(0, 0, 0)
  1. Tried to place the object at some distance away from the device camera
 modelNode?.position = SCNVector3(0, 0, -800)
  1. 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:-

  1. Object should be of actual size (Scale should be of 1.0). Their should be no reduction in the scale value.
  2. Once placed at a particular position it should not move with the movement of the device camera.
  3. Object can be seen from all angles with the movement of the device camera without any change in object position.

回答1:


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.



来源:https://stackoverflow.com/questions/60999044/ios-arkit-large-size-object-always-appears-to-move-with-the-change-in-the-posit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!