Mesh Opacity in RealityKit

Hi guys

How could I control the opacity of the ModelEntity or mesh in RealityKit?

Answered by Graphics and Games Engineer in 677554022

Were you able to resolve your issue? I ran this code on a new RealityKit macOS project on macOS 12.0. The code should be similar on iOS, you should just need to change from NSColor to UIColor. 

        let entity = ModelEntity.init(mesh: MeshResource.generateBox(size: SIMD3<Float>(1.0, 1.0, 1.0)))

        let anchor = AnchorEntity.init()

        anchor.addChild(entity)

        arView.scene.anchors.append(anchor)

        

        var material = SimpleMaterial()

        material.baseColor = MaterialColorParameter.color(

            NSColor.init(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5)

        )

        entity.model?.materials.append(material)

You can also take a look at our new PhysicallyBasedMaterial and CustomMaterial types in iOS 15 and macOS Monterey.

Not even an option to fade in a model at this time, another thing I'm hoping for soon. I'm sure it's being worked on… quite a standard feature in any scene graph.

At this point 6-21-20, you have to look at using resources outside of RK and RC if you know the opacity level you want for your mesh.Scenekit is a viable path to this end. If your meshModel is .OBJ or other SceneKit ‘Importable’ file types, you can adjust the opacity, color and lighting schemes; the export the meshModel as a .USDZ file. As you know, you cn bring .USDZ objects directly into any RK or RC scene or project from there.

’I agree with Max; ‘object opacity’ sliders in RK-RC is part of the path towards the future!

catfish
If you are able to use SimpleMaterial on your model, you can use the alpha channel of the baseColor property to affect that material's opacity programmatically. If you are loading a model from a file (and thus have arbitrary materials), you can apply fixed opacity values in that file.

If you want to dynamically fade models with arbitrary materials in and out, you may be able to use the animated Show and Hide actions that can be authored in Reality Composer. If you need more flexibility than that, please submit feedback using Feedback Assistant—ideally with a few details about your use case.
Is there a bug in iOS 14 beta 3? I am unable to change opacity using the alpha channel of a material:

Code Block swift
var material = SimpleMaterial()
material.baseColor = MaterialColorParameter.color(
  UIColor(red: 0.97, green: 0.75, blue: 0.09, alpha: 0.10)
)

No matter what the alpha is, it is always solid and I cannot see the world behind.
Bump, I have found multiple people saying "set the alpha" or "set the tintColor to .99" none of this works when using a solid color simple material. It would be great if someone at apple could post the exact code that supposedly works, because the code sample above does not.

I need to make a material that can be see-through like SceneKit is able to do.
Accepted Answer

Were you able to resolve your issue? I ran this code on a new RealityKit macOS project on macOS 12.0. The code should be similar on iOS, you should just need to change from NSColor to UIColor. 

        let entity = ModelEntity.init(mesh: MeshResource.generateBox(size: SIMD3<Float>(1.0, 1.0, 1.0)))

        let anchor = AnchorEntity.init()

        anchor.addChild(entity)

        arView.scene.anchors.append(anchor)

        

        var material = SimpleMaterial()

        material.baseColor = MaterialColorParameter.color(

            NSColor.init(displayP3Red: 1.0, green: 0.0, blue: 0.0, alpha: 0.5)

        )

        entity.model?.materials.append(material)

You can also take a look at our new PhysicallyBasedMaterial and CustomMaterial types in iOS 15 and macOS Monterey.

It worked for me, thanks!

Mesh Opacity in RealityKit
 
 
Q