【问题标题】:Orienting a directional light and adding to scene in ARKit在 ARKit 中定向定向光并添加到场景中
【发布时间】:2020-09-05 19:24:15
【问题描述】:

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

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

【问题讨论】:

    标签: swift xcode augmented-reality arkit realitykit


    【解决方案1】:

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

    考虑到方向光的位置并不重要!

    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 中实现定向光的方向,请阅读this post

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 2019-03-02
      • 2018-07-19
      • 2011-12-03
      • 2018-03-22
      • 2017-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多