【发布时间】: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