Dynamic loading various USDZ files

I have various USDZ files in my visionOS app. Loading the USDZ files works quite well. I only have problems with the positioning of the 3D model. For example, I have a USDZ file that is displayed directly above me. I can't move the model or perform any other actions on it. If I sit on a chair or stand up again, the 3D model automatically moves with me. This is my source code for loading the USDZ files:

struct ImmersiveView: View {
    @State var modelName: String
    @State private var loadedModel = Entity()
    
    var body: some View {
        RealityView { content in
            if let usdModel = try? await Entity(named: modelName) {
                print("====> \(modelName) : \(usdModel) <====")
                
                let bounds = usdModel.visualBounds(relativeTo: nil).extents
                usdModel.scale = SIMD3<Float>(1.0, 1.0, 1.0)
                usdModel.position = SIMD3<Float>(0.0, 0.0, 0.0)
                
                
                
                usdModel.components.set(CollisionComponent(shapes: [.generateBox(size: bounds)]))
                usdModel.components.set(HoverEffectComponent())
                usdModel.components.set(InputTargetComponent())
                
                loadedModel = usdModel
                content.add(usdModel)
            }
        }
    }
}

I only want the 3D models from the USDZ files to be displayed later, and later on, to be able to move them via gestures. Moving the models is step 2. First, I need to make sure the models are displayed correctly. What have I forgotten or done wrong?

Answered by DTS Engineer in 807499022

Acknowledging the reply above re: anchoring your loaded model.

To put that into context and also consider interaction with gestures you'll want to check out the Transforming RealityKit entities using gestures sample code project.

It sounds like your usdModel entity may not have a good anchor.

Try either loading it in through a Reality Composer Pro bundle, where you can position it within your scene, or set an anchor to be 1-2 meters in front of you, and 1-2 meters off the ground:

usdModel.components.set(
    AnchoringComponent(.world(
        transform: Transform(translation: [0, 1.5, -1.5]).matrix
    ))
)

Acknowledging the reply above re: anchoring your loaded model.

To put that into context and also consider interaction with gestures you'll want to check out the Transforming RealityKit entities using gestures sample code project.

Dynamic loading various USDZ files
 
 
Q