a.palo
a.palo

Reputation: 288

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

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

enter image description here

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

enter image description here

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.

Upvotes: 6

Views: 2498

Answers (3)

dnzzcn
dnzzcn

Reputation: 163

Unlike stated in the accepted answer, the issue is probably not about the tracking quality or a bug in the model. It looks like the model is not correctly placed on top of the ground, probably due to a mispositioned pivot point, and some part of the model stays under ground. So when you move the camera, since the part under ground is not occluded by the floor, it looks like it is shifting. Have a look at this picture:

enter image description here

The pivot points of the models provided by Apple are positioned correctly so that when it is placed on top of a plane on the ground, its parts stay above ground.

If you correctly position the pivot point of the model, it should work correctly, independent of the model type.

Upvotes: 4

gsk_fs
gsk_fs

Reputation: 126

I experienced the same issue.

Whenever I try to change anything in our Model of .usdz type (which is actually an Encrypted and compressed type) we cannot edit or change anything in it. If I edit or change a little position then it behaves same way as highlighted in the question.

To handle this issue, I just moved the old model .usdz to trash and and copied the original file (again) to XCode and then it worked.

Upvotes: 0

a.palo
a.palo

Reputation: 288

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.

Upvotes: 0

Related Questions