TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

RealityKit: Incorrect Shadows with Directional Light

Forums > SwiftUI

I have a problem with shadows in RealityKit and DirectionalLights.

After doing some research from my initial question about this, I [downloaded an Xcode project] that describes the basics of lights in RealityKit. It's simple enough.

One of the examples used in that sample project involved a DirectionalLight, which can be very helpful for lighting like the sun.

I took a look at the DirectionalLight option in the app, and added an extra sphere the scene:

A screenshot of an Xcode iPhone 15 Pro with correct directional lighting.

Here's the lighting for that:

private func addDirectionalLight() {
        let directionalLight = DirectionalLight()
        directionalLight.light.intensity = 100000
        directionalLight.shadow?.maximumDistance = 5
        directionalLight.shadow?.depthBias = 1
        directionalLight.look(at: [0, 0, 0], from: [-50, 20, 0], relativeTo: nil)


I made the intensity of it pretty high so you can easily see the light (white part) vs the shadows (non-white part).

I then went to my own project.

I want to create a directional lighting setup to achieve an effect akin to the sun. I thought that would be easy... I would just have to look at how the sample project did it and make adjustments.

Well... I tried creating my own lighting setup as follows:

let directionalLight = DirectionalLight()
        directionalLight.light.intensity = 100000
        directionalLight.shadow?.maximumDistance = 3
        directionalLight.look(at: [0, 0, 0], from: [0, 100, 1200], relativeTo: nil)
        // directionalLight.orientation = simd_quatf(angle: .pi/2, axis: [0, 1, 0])


where lightAnchor is an AnchorEntity at [0, 0, 0] for the world.

Here's the result when applied to objects:

A screenshot of an Xcode iPad Pro simulator with incorrect directional lighting.

Do you notice anything? I circled it. It's like the lighting is being applied to each individual object without any sort of regard for the fact that another object is there.

In real life, a shadow would be casted from the sphere on top of the cube in addition to the shadow that is currently present. Look at the first image and pretend it is the top of the cube from the second photo. Yes, there's a shadow on the back of the sphere where the light doesn't hit, but ALSO on the plane it's on.

I am extremely confused.

The only thing I thought of is that the first image has a scale where the sphere in the center of the photo is about 3 meters wide... while my second image is at a much, much larger scale. That box is 150 meters wide.

But, that should not impact the lighting, right? I don't believe it should. Is my DirectionalLight in a bad position? I don't understand how it would be, considering that it hit box and sphere.

I simply want shadows like in the first image, but for some reason I cannot achieve that.

I really need some help on this. It would be amazing if someone could!

Also, happy Vision Pro day! :)


I couldn't link the website where I downloaded the project, but it was basically an app where you could change the type of light and see the impact of a scene with a plane and sphere (seen in first photo — I added another tiny sphere).


Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.