如果您需要在场景中使用柔和和半透明的阴影,请使用SpotLight 照明设备,当您使用SpotLight 类或实现HasSpotLight 协议时可用。默认情况下,SpotLight 是北向的。目前,RealityKit 中没有用于阴影的 opacity 实例属性。
outerAngleInDegrees 实例属性不得超过 179 degrees。
import RealityKit
class Lighting: Entity, HasSpotLight {
required init() {
super.init()
self.light = SpotLightComponent(color: .yellow,
intensity: 50000,
innerAngleInDegrees: 90,
outerAngleInDegrees: 179, // greater angle – softer shadows
attenuationRadius: 10) // can't be Zero
}
}
然后创建shadow实例:
class ViewController: NSViewController {
@IBOutlet var arView: ARView!
override func awakeFromNib() {
arView.environment.background = .color(.black)
let spotLight = Lighting().light
let shadow = Lighting().shadow
let boxAndCurlAnchor = try! Experience.loadBoxAndCurl()
boxAndCurlAnchor.components.set(shadow!)
boxAndCurlAnchor.components.set(spotLight)
arView.scene.anchors.append(boxAndCurlAnchor)
}
}
这是一张没有这条线的图片:boxAnchor.components.set(shadow!)。
这是使用以下值生成的图像outerAngleInDegrees = 140:
这是使用以下值生成的图像outerAngleInDegrees = 179:
在房间中,将 SpotLight 灯具保持在距离模型 2...4 米的高度。
对于更大的对象,您必须为intensity 和attenuationRadius 使用更高的值:
self.light = SpotLightComponent(color: .white,
intensity: 625000,
innerAngleInDegrees: 10,
outerAngleInDegrees: 120,
attenuationRadius: 10000)
您也可以阅读我的 STORY 关于 Medium 上的 RealityKit 灯。