RealityKit – Change a color of a model
Asked Answered
R

2

2

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.

Rightly answered 5/5, 2021 at 20:47 Comment(0)
G
4

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.

Ghat answered 6/5, 2021 at 14:3 Comment(4)
Thanks for your answer, but wouldn't this append a new instance of the object and not change the existing one, if that is the case is it possible to connect it with already existing behaviours that have been created in reality composer? This seems like a good workaround.Rightly
What do you mean exactly? I haven't seen your code and I don't know what's your task...Ghat
So I followed your first approach from this link and I was able to add the "Obj-C" text entity to the scene, however, it did not have the behaviour (animation) that other children have. Also, once I changed the colour of the text it added a new entity to the scene, I am guessing I just have to delete it from the scene.Rightly
I'm sorry, but your question's about colour, isn't it? ))) If you're experiencing problems with other issues just post another question.Ghat
G
-1

Look into changing the material of the object.

Glenn answered 5/5, 2021 at 21:32 Comment(2)
Hi @Banane42, this question is about Reality Composer and RealityKit. It's not about SceneKit.Ghat
I see. I did not read close enough to the tags or the linked documentation.Glenn

© 2022 - 2024 — McMap. All rights reserved.