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.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
AndroidFreak
  • 866
  • 1
  • 10
  • 31

2 Answers2

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.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 1
    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. – AndroidFreak May 07 '21 at 08:30
  • What do you mean exactly? I haven't seen your code and I don't know what's your task... – Andy Jazz May 07 '21 at 08:49
  • So I followed your first approach from this [link](https://stackoverflow.com/questions/58349044/dynamically-change-text-of-realitykit-entity) 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. – AndroidFreak May 07 '21 at 08:55
  • 1
    I'm sorry, but your question's about colour, isn't it? ))) If you're experiencing problems with other issues just post another question. – Andy Jazz May 07 '21 at 09:03
-1

Look into changing the material of the object.

Banane42
  • 86
  • 2
  • 8