usdz object is not moving while loaded with SCNReferenceNode

424 views Asked by At

I was following Apple Documentation and example project to load 3d Object using .SCN file with Virtual Object (subclass of SCNReferenceNode) class but suddenly i needed to change the model from .scn to usdz . Now my usdz object is loading successfully but it is not on surface (midway in the air) and i can't interact with it like (tap , pan , rotate) ... Is there any other way to get interaction with usdz object and how can I place it on the surface like I was doing it before with .scn file

For getting model URL (downloaded from server)

    static let loadDownloadedModel : VirtualObject = {
    let downloadedScenePath = getDocumentsDirectory().appendingPathComponent("\(Api.Params.inputModelName).usdz")
    
    return VirtualObject(url: downloadedScenePath)!
}()

Loading it from URL

    func loadVirtualObject(_ object: VirtualObject, loadedHandler: @escaping (VirtualObject) -> Void) {
    isLoading = true
    loadedObjects.append(object)
    
    
    // Load the content asynchronously.
    DispatchQueue.global(qos: .userInitiated).async {
        object.reset()
        object.load()

        self.isLoading = false
        loadedHandler(object)
    }
}

Placing in the scene

    func placeObjectOnFocusSquare() {
    virtualObjectLoader.loadVirtualObject(VirtualObject.loadDownloadedModel) { (loadedObject) in
              DispatchQueue.main.async {
                  self.placeVirtualObject(loadedObject)
                self.setupBottomButtons(isSelected: true)
              }
    }
}


func placeVirtualObject(_ virtualObject: VirtualObject) {
    guard let cameraTransform = session.currentFrame?.camera.transform,
        let focusSquarePosition = focusSquare.lastPosition else {
        statusViewController.showMessage("CANNOT PLACE OBJECT\nTry moving left or right.")
        return
            
    }
    Api.Params.selectedModel = virtualObject
    virtualObject.name = String(Api.Params.inputPreviewId)
    virtualObject.scale = SCNVector3(Api.Params.modelXAxis, Api.Params.modelYAxis, Api.Params.modelZAxis)
    virtualObject.setPosition(focusSquarePosition, relativeTo: cameraTransform, smoothMovement: false)
    updateQueue.async {
        self.sceneView.scene.rootNode.addChildNode(virtualObject)
    }
}

.usdz object in sceneview

enter image description here

1

There are 1 answers

0
Hanzala Raza On BEST ANSWER

After so many tries , finally i found out that dynamic scaling of the model causing problem , Reference to this

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

I scaled the object to 0.01 for all the axis (x,y and z)

        virtualObject.scale = SCNVector3Make(0.01, 0.01, 0.01)