Search code examples
swiftaugmented-realityarkitrealitykitreality-composer

How can I reduce the opacity of the shadows in RealityKit?


I composed a scene in Reality Composer and added 3 objects in it. The problem is that the shadows are too intense (dark).

I tried using the Directional Light in RealityKit from this answer rather than a default light from Reality Composer (since you don't have an option to adjust light in it).

enter image description here

Update I implemented the spotlight Lighting as explained by @AndyFedo in the answer. The shadow is still so dark.

enter image description here


Solution

  • The shadows appear darker when I use "Hide" action sequence on "Scene Start" and post a notification to call "Show" action sequence on tap gesture. The shadows were fixed when I scaled the Object to 0% and post Notification to call "Move,Rotate,Scale to" action sequence on tap gesture.

    Scaled Image

    enter image description here

    Unhide Image

    enter image description here

    Object Difference with hidden and scaled actions

    enter image description here

    import UIKit
    
    import RealityKit
    
    import ARKit
    
    
    class Lighting: Entity, HasDirectionalLight {
        required init() {
            super.init()
            self.light = DirectionalLightComponent(color: .red, intensity: 1000, isRealWorldProxy: true)
        }
    }
    
    class SpotLight: Entity, HasSpotLight {
    
        required init() {
            super.init()
            self.light = SpotLightComponent(color: .yellow,
                                            intensity: 50000,
                                            innerAngleInDegrees: 90,
                                            outerAngleInDegrees: 179, // greater angle – softer shadows
                attenuationRadius: 10) // can't be Zero
    
        }
    }
    
    class ViewController: UIViewController {
    
        @IBOutlet var arView: ARView!
    
        enum TapObjects {
            case None
            case HiddenChair
            case ScaledChair
        }    
        var furnitureAnchor : Furniture._Furniture!
        var tapObjects : TapObjects = .None
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            furnitureAnchor = try! Furniture.load_Furniture()
            arView.scene.anchors.append(furnitureAnchor)
    
            addTapGesture()
    
        }
    
        func addTapGesture() {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
            arView.addGestureRecognizer(tapGesture)
        }
    
        @objc func onTap(_ sender: UITapGestureRecognizer) {
    
            switch tapObjects {
            case .None:
                furnitureAnchor.notifications.unhideChair.post()
                tapObjects = .HiddenChair
            case .HiddenChair:
                furnitureAnchor.notifications.scaleChair.post()
                tapObjects = .ScaledChair
            default:
                break
            }
    
        }
    }