How to project shadows on meshed environment in RealityKit?

267 views Asked by At

I'm using RealityKit in combination with a USDZ model and scene understanding (LiDAR iPhone), and simply want my model to project some nice shadows on the real-world environment. I can easily get a custom spotlight on my model, but fail to project any shadows from my it.

I'm stuck with what appears to be the default top-down lighting. What am I doing wrong?

My code:

override func viewDidLoad() {

    super.viewDidLoad()

    arView.session.delegate = self
    arView.environment.sceneUnderstanding.options = []
    arView.environment.sceneUnderstanding.options.insert(.receivesLighting)

    arView.automaticallyConfigureSession = false
    let configuration = ARWorldTrackingConfiguration()
    configuration.planeDetection = [.horizontal, .vertical]
    configuration.sceneReconstruction = .mesh
    configuration.environmentTexturing = .automatic

    arView.session.run(configuration) }

Here's the function I call when adding a model

@IBAction func addMyModel(_ sender: Any) {

    do {
        let mymodel = try ModelEntity.load(named: "mymodel")
        
        // Lights
        let spotLight = CustomSpotLight()
        let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.30, 1.00])
        
        anchor.children.append(mymodel)
        anchor.addChild(spotLight)
        
        self.arView.scene.anchors.append(anchor)
        
    } catch {
        fatalError("Failed to load asset.")
    }
}

and finally here's my custom spotlight code:

import ARKit
import RealityKit

class CustomSpotLight: Entity, HasSpotLight {
    required init() {
        super.init()
        self.light = SpotLightComponent(color: .blue,
                                    intensity: 500000,
                          innerAngleInDegrees: 45,
                          outerAngleInDegrees: 169,
                            attenuationRadius: 9.0)
        
        self.shadow = SpotLightComponent.Shadow()
        self.position.y = 5.0
        self.orientation = simd_quatf(angle: -.pi/1.5,
                                       axis: [1,0,0])
 
    }
}
0

There are 0 answers