6

I am working on an iOS App with ARKit 3 and RealityKit. I want to create virtual 2d planes within the room containing images. Therefore I used

Adding a material to a ModelEntity programmatically

But images which aren't nearly white will get displayed very dark, nearly black. I also tried out UnlitMaterial. It indeed created a little better result but the images are still way to dark. This problem persists over JPEG and PNG files.

On earlier versions where I used SceneKit I solved a similiar problem through

sceneView.automaticallyUpdatesLighting = true
sceneView.autoenablesDefaultLighting = true

I guess the image did enlight itself through this code.

How can I solve the problem in RealityKit?

Possibly related: https://forums.developer.apple.com/thread/119265

2 Answers2

2

When scene lighting doesn’t affect a material

Out-of-the-box ARView has auto-lighting feature embedded in light estimation stage. However, sometimes, it doesn't work properly due to poor environment lighting. So, the only way for materials to ignore scene lighting, is to use UnlitMaterial or VideoMaterial.

Also, make sure that your unlit material has white color and your jpg or png images have no special color profile. Here's my code:

import UIKit
import RealityKit

class ViewController : UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        arView.renderOptions = [.disableGroundingShadows]

        var material = UnlitMaterial(color: .white)
        material.color.texture = .init(try! .load(named: "img", in: nil))

        let mesh = MeshResource.generateSphere(radius: 0.25)
        let model = ModelEntity(mesh: mesh, materials: [material])
        
        let anchor = AnchorEntity()
        anchor.addChild(model)
        arView.scene.anchors.append(anchor)
    }
}
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
2
 let img = UIImage(named: ouputSrc, in: Bundle(path: resourcePath), compatibleWith: nil)
                
  var myMaterial = UnlitMaterial()
  myMaterial.baseColor = try! .texture(.load(named: ouputSrc, in: Bundle(path: resourcePath)))
  myMaterial.tintColor = UIColor.white.withAlphaComponent(1)
SnehaK
  • 141
  • 1
  • 5