Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
        }
    })
like image 403
Quyet Nguyen Avatar asked Jan 24 '26 12:01

Quyet Nguyen


1 Answers

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.

like image 176
Andy Jazz Avatar answered Jan 26 '26 02:01

Andy Jazz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!