AndroidFreak
AndroidFreak

Reputation: 866

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.

Upvotes: 2

Views: 2416

Answers (2)

Andy Jazz
Andy Jazz

Reputation: 58563

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.

Upvotes: 4

Banane42
Banane42

Reputation: 86

Look into changing the material of the object.

Upvotes: -1

Related Questions