RealityKit – Change a color of a model

1.6k views Asked by At

I am currently looking into options on how to transform objects colour from Swift. The object has been added to the scene from Reality Composer.

I found in the documentation that I can change position, rotation, scale, however, I am unable to find a way how to change colour.

2

There are 2 answers

4
Andy Jazz On BEST ANSWER

Xcode 14.2, RealityKit 2.0, Target iOS 16.2

Use the following code to change a color of a box model (found in Xcode RealityKit template):

let boxScene = try! Experience.loadBox()     // Reality Composer scene

let modelEntity = boxScene.steelBox!.children[0] as! ModelEntity

var material = SimpleMaterial()
material.color = .init(tint: .green)
modelEntity.model?.materials[0] = material

arView.scene.anchors.append(boxScene)

print(boxScene)

Downcast to ModelEntity is necessary for accessing model's components:

enter image description here

If you need to change a transparency of your model, use the following approach.

2
Banane42 On

Look into changing the material of the object.