Hi,
I'm trying to replicate ground shadow in this video. However, I couldn't get it to work in the simulator.
My scene looks like the following which is rendered as an immersive space:
The rocket object has the grounding shadow component with "cast shadow" set to true:
but I couldn't see any shadow on the plane beneath it.
Things I tried:
- using code to add the grounding shadow component, didn't work
- re-used the IBL from the helloworld project to get some lighting for the objects. Although the IBL worked, I still couldn't see the shadow
- tried adding a DirectionalLight but got an error saying that directional lights are not supported in VisionOS (despite the docs saying the opposite)
A related question on lighting: I can see that the simulator definitely applies some scene lighting to objects. But it doesn't seem to do it perfectly. For example in the above screenshot I placed the objects under a transparent ceiling which is supposed to get a lot of lights. But everything is still quite dark.
@tracyhenry I think I figured it out.
I think I figured out the problem through experimentation, and GroundingShadowComponent
actually does work.
I think when you read a model from a USDZ file, you can't simply assign the GroundingShadowComponent
to the root entity of the entity hierarchy that's in the USDZ file. GroundingShadowComponent
must be assigned to each ModelEntity
that's in the USDZ file hierarchy.
This would explain why GroundingShadowComponent
worked when directly assigned to sphereEntity
in your code, because it's a ModelEntity
.
Assuming the existence of Entity/enumerateHierarchy
, defined below, you could write something like this after you load the entity from the USDZ file:
usdzFileEntity.enumerateHierarchy { entity, stop in
if entity is ModelEntity {
entity.components.set(GroundingShadowComponent(castsShadow: true))
}
}
This worked for me when I tried it.
import RealityKit
extension Entity {
/// Executes a closure for each of the entity's child and descendant
/// entities, as well as for the entity itself.
///
/// Set `stop` to true in the closure to abort further processing of the child entity subtree.
func enumerateHierarchy(_ body: (Entity, UnsafeMutablePointer<Bool>) -> Void) {
var stop = false
func enumerate(_ body: (Entity, UnsafeMutablePointer<Bool>) -> Void) {
guard !stop else {
return
}
body(self, &stop)
for child in children {
guard !stop else {
break
}
child.enumerateHierarchy(body)
}
}
enumerate(body)
}
}