How to load a USDZ inside of a Reality Composer Pro package as ModelEntity in RealityKit ?

I need a MeshResource from ModelEntity to generate a box collider, but ModelEntity fails to load USDZ files from the Reality Composer Pro (RCP) bundle.

This code works for loading an Entity:

// Successfully loads as generic Entity
previewEntity = try await Entity(named: fileName, in: realityKitContentBundle)

But this fails when trying to load as ModelEntity:


// Fails to load as ModelEntity
modelEntity = try await ModelEntity(named: fileName, in: realityKitContentBundle)

I found this thread mentioning:

"You'll likely go from USDZ to Entity which contains a MeshResource when you load/init the USDZ file."

But it doesn't explain ​how to actually extract the MeshResource. Could anyone advise:

  1. How to properly load USDZ files as ModelEntity from RCP bundles?
  2. How to extract MeshResource from a successfully loaded Entity?
  3. Alternative approaches to generate box colliders if direct extraction isn't possible?

Sample code for extraction or workarounds would be greatly appreciated!

Hi @Hernando9563

You can access the MeshResource of an entity via its ModelComponent:

if let meshResource = entity.components[ModelComponent.self]?.mesh {
    // Do something with the mesh resource...
}

This would be my suggested approach in this case.

To answer your other two questions, you can load a USDZ file as a ModelEntity from an RCP bundle by casting it directly to a ModelEntity after loading it as an Entity, for example:

let modelEntity = try? await Entity(named: fileName, in: realityKitContentBundle).findEntity(named: entityName) as? ModelEntity

One alternative way to generate a box collider directly from an entity without accessing its MeshResource is to utilize its visualBounds:

let boundsSize = entity.visualBounds(relativeTo: entity.parent).extents
let collisionComponent = CollisionComponent(shapes: [.generateBox(size: boundsSize)])
entity.components.set(collisionComponent)

You can also add a CollisionComponent directly to an entity in RCP and set its shape to Box.

Let me know if you have any further questions!

How to load a USDZ inside of a Reality Composer Pro package as ModelEntity in RealityKit ?
 
 
Q