Search code examples
swiftaugmented-realityarkitrealitykitreality-composer

RealityKit – Change a color of a model


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.


Solution

  • 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.