Shadow in RealityKit
Shadow Question
I want know how to set the shadow to make it reality in real world
I have try to load a model in my app, I find that the shadow is always perpendicular to the model like this:
the sunshine is in the real world, and the shadow should be long and face to me.
My Try
I have already add directionalLight, but It seems to only effect model color, no effect in model shadow. I try to set this config arView.environment.sceneUnderstanding.options.insert(.receivesLighting)
the shadow will disappear, this is not my expect.
I want know how to set the shadow to make it reality in real world
code
there is my code:
import SwiftUI
import RealityKit
import ARKit
struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let config = ARWorldTrackingConfiguration()
if ARWorldTrackingConfiguration.isSupported {
config.planeDetection = [.horizontal]
if(ARWorldTrackingConfiguration.supportsSceneReconstruction(.meshWithClassification)) {
config.sceneReconstruction = .meshWithClassification
}
let sematics : ARConfiguration.FrameSemantics = [.personSegmentationWithDepth]
if type(of: config).supportsFrameSemantics(sematics) {
config.frameSemantics = sematics
}
}
// arView.environment.background = .color(.black)
// arView.environment.sceneUnderstanding.options.insert(.receivesLighting)
// config.isLightEstimationEnabled = true
// arView.environment.sceneUnderstanding.options.insert(.occlusion)
arView.session.run(config, options: [])
arView.session.delegate = arView
let buildAnchor = LoadModelEntity().loadBuildingModel()
arView.scene.addAnchor(buildAnchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
//var times : Int = 0
extension ARView : ARSessionDelegate {
public func session(_ session: ARSession, didUpdate frame: ARFrame) {
guard let estimatLight = frame.lightEstimate else {return}
print("light intensity : \(estimatLight.ambientIntensity), light temperature : \(estimatLight.ambientColorTemperature)")
// times += 1
// ARLightEstimate.self
}
}
import ARKit
import RealityKit
class LoadModelEntity {
func loadBuildingModel() -> AnchorEntity {
var buildAnchor = AnchorEntity()
do {
let buildModelUrl = "toy.usdz"
let buildModel = try ModelEntity.load(named: buildModelUrl)
if ARPlaneAnchor.isClassificationSupported {
buildAnchor = AnchorEntity(plane: .any, classification: .floor)
}
buildModel.scale.x = buildModel.scale.x * 20
buildModel.scale.y = buildModel.scale.y * 20
buildModel.scale.z = buildModel.scale.z * 20
buildAnchor.addChild(buildModel)
let directionalLight = DirectionalLight()
directionalLight.light.color = .red
directionalLight.light.intensity = 10000
directionalLight.shadow?.maximumDistance = 5
directionalLight.shadow?.depthBias = 1
directionalLight.look(at: [1,0,1], from: [0,1,0], relativeTo: nil)
buildAnchor.addChild(directionalLight)
buildModel.playAnimation(buildModel.availableAnimations[0].repeat())
} catch {
print("file not found!")
}
return buildAnchor
}
}