Reputation: 2913
For some time now, I have been trying to add a realistic ground shadow to an object in RealityKit. For my use case, I will not be using Reality Composer, nor (per this question) will I be using an anchor entity from a horizontal plane (my user will tap to place an object and that tap could align with either a horizontal plane or an ARMeshAnchor, as we support LiDAR in our app).
When I test my USDZ model via QuickLook on iOS, I see that iOS adds a shadow beneath my model, and while not wholly realistic, it appears a bit more "placed" on a surface, as compared to no shadow.
In trying to add my model, I am taking the following steps;
self.model = Entity.load(named: "model.usdz")
When a user taps on the screen, I perform a raycast and add the model to the built anchor;
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
for anchor in anchors {
if anchor.name == "tapped" {
let anchorEntity = AnchorEntity(anchor: anchor)
anchorEntity.addChild(self.model!)
arView.scene.addAnchor(anchorEntity)
}
}
}
When the model is added to the tapped point, there are no ground shadows. As a test, I had gone down the path of trying to add a Directional Light, believing that its placement may cast a light on the object and, therefore, create shadows. I create the light like so;
class Lighting: Entity, HasDirectionalLight {
required init() {
super.init()
self.light = DirectionalLightComponent(color: .white, intensity: 5000, isRealWorldProxy: true)
}
}
I've added a global var lightEntity = AnchorEntity()
. Then, in my viewDidLoad
method, I am attempting to set up the light like so;
let spotLight = Lighting().light
let shadow = Lighting().shadow
lightAnchor.components.set(shadow!)
lightAnchor.components.set(spotLight)
arView.scene.anchors.append(lightAnchor)
self.model = Entity.load(named: "model.usdz")
While I can see that there is a light shining on the object, it does not seem to cause any shadows to be cast.
Upvotes: 1
Views: 849
Reputation: 556
if your app supports LiDAR , you can use
arView.environment.sceneUnderstanding.options.insert(.receivesLighting)
Upvotes: 1