Can't load texture of USDZ file via script using ModelIO library on Xcode with swift

808 Views Asked by At

I added an USDZ file to my Xcode project. The USDZ file is showing as normal in the project in Xcode open on my mac, however, when I try to load it via code using modelIO library it loads the mesh but it doesn't load the texture.

In the snapshots below there is a bee model that I try to load. In the second there is a bee and aircraft, bee textures are not rendered whereas aircraft textures are loaded without problem. I also convert the USDZ file to the scn file and it worked properly. I think the problem is not about the file but the code I am using can't load the USDZ file properly. I am trying to load a USDZ file via code and which library I am using not much important. If you have other suggestions I am open to listen them.

I use the following code to load my model:

    let fileName = “bee”
       let ext = “usdz”
       let asset = MDLAsset(url: Bundle.main.url(forResource:fileName, withExtension: ext)!)
       let loadedScene = SCNScene(mdlAsset: asset)
           for child in loadedScene.rootNode.childNodes {
              child.geometry?.firstMaterial?.lightingModel = .physicallyBased
}

Here are some snapshots to demonstrate results I get: - snapshot from Xcode viewer on my mac, successfully showing the bee with texture:

enter image description here

  • snapshot from the app on mobile phone, bee and aircraft, bee textures are not visible:

enter image description here

  • on the Xcode console I get following error logs:

enter image description here

What am I missing here to load the model file entirely, including textures, to my scene?

1

There are 1 best solutions below

1
On

I found the solution. There is a method in MDLAsset as MDLAsset.loadTextures() to load the textures. When you call it then it loads the textures.