我将 RealityKit 与 USDZ 模型和场景理解 (LiDAR iPhone) 结合使用,并且只是希望我的模型在真实世界环境中投射一些漂亮的阴影。我可以轻松地在我的模型上获得自定义聚光灯,但无法投射任何阴影。
我被默认的自上而下照明所困扰。我究竟做错了什么?
我的代码:
override func viewDidLoad() {
super.viewDidLoad()
arView.session.delegate = self
arView.environment.sceneUnderstanding.options = []
arView.environment.sceneUnderstanding.options.insert(.receivesLighting)
arView.automaticallyConfigureSession = false
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
configuration.sceneReconstruction = .mesh
configuration.environmentTexturing = .automatic
arView.session.run(configuration) }
这是我添加模型时调用的函数
@IBAction func addMyModel(_ sender: Any) {
do {
let mymodel = try ModelEntity.load(named: "mymodel")
// Lights
let spotLight = CustomSpotLight()
let anchor = AnchorEntity(plane: .horizontal, minimumBounds: [0.30, 1.00])
anchor.children.append(mymodel)
anchor.addChild(spotLight)
self.arView.scene.anchors.append(anchor)
} catch {
fatalError("Failed to load asset.")
}
}
最后这是我的自定义聚光灯代码:
import ARKit
import RealityKit
class CustomSpotLight: Entity, HasSpotLight {
required init() {
super.init()
self.light = SpotLightComponent(color: .blue,
intensity: 500000,
innerAngleInDegrees: 45,
outerAngleInDegrees: 169,
attenuationRadius: 9.0)
self.shadow = SpotLightComponent.Shadow()
self.position.y = 5.0
self.orientation = simd_quatf(angle: -.pi/1.5,
axis: [1,0,0])
}
}