将定向光定向并添加到ARKit中的场景中

问题描述 投票:1回答:1

我已经找到了几个很棒的示例,这些示例如何向我的代码中添加定向光,但没有如何更改方向以及如何将其添加到场景中。如何使用我的代码执行此操作?这是我的轻课:

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 :)瞄准光的方法”。我该放在哪里?我在哪里可以找到这些简单问题的答案?在过去的几天里,追逐我的尾巴只是为了旋转灯,这令人沮丧。

xcode augmented-reality arkit realitykit
1个回答
0
投票

使用以下代码控制定向光的方向:

import ARKit
import RealityKit

class Lighting: Entity, HasDirectionalLight {

    required init() {
        super.init()

        self.light = DirectionalLightComponent(color: .white,
                                           intensity: 1000000,
                                    isRealWorldProxy: true)
    }
}

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

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

        let directLight = Lighting().light
        let boxAnchor = try! Experience.loadBox()

        let directAnchor = AnchorEntity()
        directAnchor.append(directLight)

        boxAnchor.components.set(directAnchor)
        arView.scene.anchors.append(boxAnchor)

        directAnchor.ttransform.rotation = simd_quatf(angle: .pi/32,
                                                       axis: [-1, 0, 0])
        boxAnchor.steelBox!.scale = [15,15,15]
        boxAnchor.steelBox!.position.z = -3
    }
}

如果您想知道如何在SceneKit中实现定向光的方向,请阅读this post

© www.soinside.com 2019 - 2024. All rights reserved.