swiftuirealitykitvisionosreality-composer-pro

Programmatically added material does not render properly


I am trying to programmatically use one of the Reality Composer Pro's default materials in RealityView. But it's not rendered properly -- all texture is lost.

Expected Actual
enter image description here enter image description here

Here is my code:

RealityView { content in
    if let material = try? await ShaderGraphMaterial(named: "/BrownLeather", from: "BrownLeather.usdz", in: realityKitContentBundle) {
        let sphere = ModelEntity(mesh: .generateSphere(radius: 0.1), materials: [material])
        content.add(sphere)
    }
}

I can add the material to objects in Reality Composer Pro and directly import the objects into RealityView, that works fine and is rendered correctly. Setting UV_Scale as suggested in this answer doesn't work.


Solution

  • In Reality Composer Pro, you need to apply library material to any primitive shape. After that you'll be able to programmatically load this material from current working scene.

    RealityView { content in
        if let matX = try? await ShaderGraphMaterial(named: "/Root/BrownLeather", 
                                                      from: "Scene.usda", 
                                                        in: realityKitContentBundle) {
            let sphere = ModelEntity(mesh: .generateSphere(radius: 0.1), 
                                materials: [matX])
            content.add(sphere)
        }
    }
    

    enter image description here