Changing the texture of a RealityView

Hello. I have a model of a CD record and box, and I would like to change the artwork of it via an external image URL. My 3D knowledge is limited, but what I can say is that the RealityView contains the USDZ of the record, which in turn contains multiple materials: ArtBack, ArtFront, PlasticBox, CD.

How do I target an artwork material and change it to another image? Here is the code so far.

RealityView { content in
                    do {
                        let entity = try await Entity.init(named: "VinylScene", in: realityKitContentBundle)
                        entity.scale = SIMD3<Float>(repeating: 0.6)
                        content.add(entity)
                    } catch {
                        ProgressView()
                    }
                }

Replies

This is possible. The snippet below changes the white material on the PillBottle model from Realty Composer Pro's content library. Add the pill bottle to your project to follow along.

RealityView { content in
    if let entity = try? await Entity(named: "PillBottle", in: realityKitContentBundle) {
        
        // not related, but position the bottle so it's in view
        entity.position = [0.0, 1.5, -1.0]
        
        /* Important: find the entity with the material binding for the material you want to change.
            Open the model in Reality Composer Pro, use the `navigator` (usually on the left, but 
            you can toggle its visibility using Reality Composer Pro's view menu) to select an entity,
            note its material binding in the `inspector` (usually on the right). Inspect each of 
            the node's child entities until you find the one with the desired material binding.
        */
        
        if let childWithMaterialToChange = entity.findEntity(named: "pill_bottle_base_realistic_lod0"),
            let texture = try? await TextureResource(named: "your-new-image") {

            var material = SimpleMaterial()
            material.color = .init(texture: .init(texture))
            
            childWithMaterialToChange.components[ModelComponent.self]?.materials = [material]
        }
        
        content.add(entity)
    }
}