RealityKit ShaderGraphMaterial parameters in Reality Composer Pro

I have a custom material using Shader Graph in Reality Composer Pro, and I am trying to rig up sliders to values to control the shader. I am able to read the values from the Shader Graph without a problem, and I can even update them when setting them from the LLDB command line and then getting the values back. But the changes are not reflected in the graphics. Is there some sort of update() method or something that is required to read the changed parameter values?

On a related note, I am trying to understand what the MaterialParameters.Handle property is and why one would access a MaterialParameter via the handle vs just the name.

Answered by arthurfromberlin in 798876022

Hi, you likely forgot to reapply the material to the mesh after updating its parameters. RealityKit Materials are value types.

I always use a helper function like this:

extension Entity {
    func modifyMaterials(_ closure: (Material) throws -> Material) rethrows {
        try children.forEach { try $0.modifyMaterials(closure) }
        
        guard var comp = components[ModelComponent.self] as? ModelComponent else { return }
        comp.materials = try comp.materials.map { try closure($0) }
        components[ModelComponent.self] = comp
    }
}
Accepted Answer

Hi, you likely forgot to reapply the material to the mesh after updating its parameters. RealityKit Materials are value types.

I always use a helper function like this:

extension Entity {
    func modifyMaterials(_ closure: (Material) throws -> Material) rethrows {
        try children.forEach { try $0.modifyMaterials(closure) }
        
        guard var comp = components[ModelComponent.self] as? ModelComponent else { return }
        comp.materials = try comp.materials.map { try closure($0) }
        components[ModelComponent.self] = comp
    }
}
RealityKit ShaderGraphMaterial parameters in Reality Composer Pro
 
 
Q