Swift 定向平行光并添加到ARKit中的场景

Swift 定向平行光并添加到ARKit中的场景,swift,xcode,augmented-reality,arkit,realitykit,Swift,Xcode,Augmented Reality,Arkit,Realitykit,我已经找到了一些很好的例子来说明如何将平行光添加到代码中,而不是如何更改方向以及将其添加到场景中。如何使用我的代码实现这一点?这是我的灯光课: class Lighting: Entity, HasDirectionalLight { required init() { super.init() self.light = DirectionalLightComponent(color: .white,

我已经找到了一些很好的例子来说明如何将平行光添加到代码中,而不是如何更改方向以及将其添加到场景中。如何使用我的代码实现这一点?这是我的灯光课:

class Lighting: Entity, HasDirectionalLight {
    required init() {
        super.init()
        self.light = DirectionalLightComponent(color: .white,
                                           intensity: 100000,
                                    isRealWorldProxy: true)
    }
}
下面是调用它的函数:

func addTableToPlane(arView: ARView) {
        let tableAnchor = AnchorEntity(plane: .horizontal)
        let table = try! Entity.load(named: "Table_1500")
        tableAnchor.addChild(table)

        let dirLight = Lighting().light
        let shadow = Lighting().shadow
        tableAnchor.components.set(shadow!)
        tableAnchor.components.set(dirLight)
}
我是ARKit的新手,所以我还没有弄清楚如何编辑平行光的方向

我尝试的另一个不成功的方法是创建照明功能,但我一直无法找到如何将其添加到场景中:

func addLights(arView: ARView) {
    // 1
    let directionalLight = SCNLight()
    directionalLight.type = .directional
    directionalLight.intensity = 500
    // 2
    directionalLight.castsShadow = true
    directionalLight.shadowMode = .deferred
    // 3
    directionalLight.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
    // 4
    let directionalLightNode = SCNNode()
    directionalLightNode.light = directionalLight
    directionalLightNode.rotation = SCNVector4Make(1, 0, 0, -Float.pi / 3)
    sceneView.scene.rootNode.addChildNode(directionalLightNode)
}
然后我将addLights(arView:uiView)添加到addTableToPlane函数中。我尝试用以下方式添加灯光:

arView.scene.rootNode.addChildNode(ambientLightNode)
但是这会产生一个错误,我没有一个childNode等等。我想我已经被Python的体面文档宠坏了,这些文档提供了一些示例来帮助解决问题,而不像Xcode的过于简洁的文档,比如,我用“使用灯光的外观(at:from:upVector:relativeTo:)方法瞄准灯光”做了什么。我把这个放在哪里?我在哪里可以找到这些简单问题的答案?
在过去的几天里,仅仅为了旋转灯光而追逐我的尾巴是令人沮丧的。

使用以下代码来控制平行光的方向:

考虑到平行光的位置不重要

import ARKit
import RealityKit

class Lighting: Entity, HasDirectionalLight, HasAnchoring {

    required init() {
        super.init()

        self.light = DirectionalLightComponent(color: .green,
                                           intensity: 1000,
                                    isRealWorldProxy: true)
    }
}

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated) 

        let light = Lighting()
        light.orientation = simd_quatf(angle: .pi/8,
                                        axis: [0, 1, 0])

        let boxAnchor = try! Experience.loadBox()

        let directLightAnchor = AnchorEntity()
        directLightAnchor.addChild(light)

        boxAnchor.addChild(directLightAnchor)
        boxAnchor.steelBox!.scale = [30,30,30]
        boxAnchor.steelBox!.position.z = -3

        arView.scene.anchors.append(boxAnchor)
    }
}
如果您想知道如何在SceneKit中实现平行光的方向,请阅读