Change Material base color

Hi,


I have a USDZ model loaded and as far as I can tell I am using RealityKit to display it. Is there a way to change the properties of certain material so that the already-displaying model gets a different color in parts where this material is used?


i.e. I have a model of a baby stroller and I want to switch between a couple of different colors for the sun roof.


I couldn't find any documentation or sample to that effect.


Any help would be much appreciated

Oliver

I saw your question on twitter, but I'll respond here with more details…


Once you've loaded your ModelEntity from a USDZ, find the model.materials list and replace the material you want to replace with a SimpleMaterial or UnlitMaterial.


To find which material you need to replace, depending on how your USDZ is structured it could look like this:

https://imgur.com/a/bWyjVlG

In which case you can see that if you want to replace the sail it'll be model.materials[0], model.materials[4] for the handrails.


If you cannot view all the materials like this, for example if you have multiple geometries in your USDZ, then you can try deduce based on the order of the geometries in your usdz file, or loop through some colours, replacing each material in the material array like this:


let myColors: [UIColor] = [.red, .orange, .blue, .yellow, ...]
model.materials = myColors.map { SimpleMaterial(color: $0, isMetallic: false) }


Also make sure sure that your array of colors is the same size as your original model.materials array, not sure what a ModelComponent does if you give it too many materials. (it likely just ignores them)

And then just see what color the mesh shows up on to deduce the index.

Change Material base color
 
 
Q