ModelIO: change material of MDLAsset / MDLMesh / MDLSubmesh

939 Views Asked by At

I load models inside my iPad app as a MDLAsset to view them using RealityKit. As I need to change the texture of the models I currently simply apply a SimpleMaterial on the ModelEntity in RealityKit. This works fine, but as there is no elegant way to export the RealityKit object back as e.g. a usd-file. So I need to persist my changes to the MDLAsset, which then can be easily exported.

I managed to persistently change the transform of the MDLAsset, but had no luck changing the material in any way.

Of course there would always be the workaround using SceneKit, which makes it easy to manipulate the model and export it to a MDLAsset. But I hope there is a more straight forward approach.

What I'm doing right now?

let modelAsset = MDLAsset(url: modelUrl)

guard let modelMesh = modelAsset.object(at: 0) as? MDLMesh else {
  return
}

modelMesh.transform = some MDLTransform

// change material here

try! modelAsset.export(to: exportUrl)

I tried many different ways, but most come down to:

let objectMaterial = SCNMaterial()
objectMaterial.diffuse.contents = modelColor
let modelMaterial = MDLMaterial.init(scnMaterial: objectMaterial)

for subMesh in modelMesh.submeshes as! [MDLSubmesh] {
  subMesh.material = modelMaterial
}

I also tried manipulating the MDLMaterialProperties of the existing subMesh material, but it seems the material is somehow write protected, at least I didn't manage to change its baseColor semantic.

So is there a direct way to apply a material to a MDLAsset / MDLMesh / MDLSubmesh?

0

There are 0 best solutions below