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:
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)
addLight(directionalLight)
}
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])
lightAnchor.addChild(directionalLight)
where lightAnchor
is an AnchorEntity
at [0, 0, 0] for the world.
Here's the result when applied to objects:
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! :)