RealityKit – 如何编辑或添加光照?

RealityKit – How to edit or add a Lighting?

我正在尝试在我的 RealityKit AR 场景中添加光照。我在 Reality Composer 中找不到光照选项。如果有办法添加 Directional Light 或编辑它,请告诉我。我尝试过 Apple 文档,但无法理解如何添加它们。

目前您无法在 Reality Composer 中执行此操作,您需要使用 RealityKit。因此,您需要创建一个继承自 Entity class 并符合 HasPointLight 协议的自定义 class。 运行 macOS 项目中的此代码可了解 PointLight 设置的工作原理:

import AppKit
import RealityKit

class Lighting: Entity, HasPointLight {
    
    required init() {
        super.init()
        
        self.light = PointLightComponent(color: .red,
                                     intensity: 100000,
                             attenuationRadius: 20)
    }
}

class GameViewController: NSViewController {
    
    @IBOutlet var arView: ARView!
    
    override func awakeFromNib() {
        
        arView.environment.background = .color(.black)
        
        let pointLight = Lighting().light
        let boxAnchor = try! Experience.loadBox()
        
        boxAnchor.components.set(pointLight)
        arView.scene.anchors.append(boxAnchor)
        
        boxAnchor.steelBox!.scale = [9,9,9]
        boxAnchor.steelBox!.position.z = -0.5
    }
}

The same way you can add a Directional Light to the scene. But remember: a position of Directional Light does not important, but an orientation does! By default it's oriented to north (-Z).

class Lighting: Entity, HasDirectionalLight {
    
    required init() {
        super.init()
        
        self.light = DirectionalLightComponent(color: .red,
                                           intensity: 100000,
                                    isRealWorldProxy: true)
    }
}

也可以阅读我的 STORY 关于 Medium 上的灯。