Search code examples
swiftaugmented-realityarkitusdzaranchor

ARKit: Place USDZ model to plane by add anchor but it always above my head


I am newbie in ARKit and do small project add object to plane.

I am follow step in Apple demo project: Handling 3D Interaction and UI Controls in Augmented Reality.

If I load scn model (or dae, obj) and place in plane use add anchor it work perfect.

But when I change use USDZ model(download from Apple) to load and place it alway above my head. I change use add child node and set position but not working, same result as add anchor.

    // Test add usdz object
    guard let url = Bundle.main.url(forResource: "wheelbarrow", withExtension: "usdz"),
        let object = VirtualObject(url: url) else {
            print("Error usdz file")
            return
    }

    // Add Child Node not working
    //        let object = obj.clone()
    //        object.load()
    //        object.position = focusSquare.position
    //        sceneView.scene.rootNode.addChildNode(object)

    self.virtualObjectLoader.loadVirtualObject(object, loadedHandler: { [weak self] loadedObject in
        do {
            print("Load Virtual Object:\(object.referenceURL.absoluteString)")
            let scene = try SCNScene(url: object.referenceURL, options: nil)
            self?.sceneView.prepare([scene], completionHandler: { _ in
                DispatchQueue.main.async {
                    self?.placeVirtualObject(loadedObject)
                    loadedObject.isHidden = false
                }
            })
        } catch {
            print("Error: \(error)")
            fatalError("Failed to load SCNScene from object.referenceURL")
        }
    })

Solution

  • By default the scale of WheelBarrow.usdz file is huge.

    enter image description here

    You need to scale down a model to a value of 0.025 (for all three axis):

    wheelBarrowNode.scale = SCNVector3(x: 0.025, y: 0.025, z: 0.025)
    

    enter image description here

    Hope this helps.