定向定向光并添加到 ARKit 中的场景
Orienting a directional light and adding to scene in ARKit
我发现了一些很好的示例,说明如何将定向光添加到我的代码中,但没有找到如何更改方向以及将其添加到场景中的示例。我如何使用我的代码执行此操作?这是我的灯 class:
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 的过于简洁的文档不同,例如,我到底用 [=26 做了什么=].我把它放在哪里?我在哪里可以找到这些简单问题的答案?
在过去的几天里追着我的尾巴只是为了旋转一个灯是令人沮丧的。
使用以下代码控制平行光的方向:
考虑到定向光的位置并不重要!
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。
我发现了一些很好的示例,说明如何将定向光添加到我的代码中,但没有找到如何更改方向以及将其添加到场景中的示例。我如何使用我的代码执行此操作?这是我的灯 class:
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 的过于简洁的文档不同,例如,我到底用 [=26 做了什么=].我把它放在哪里?我在哪里可以找到这些简单问题的答案? 在过去的几天里追着我的尾巴只是为了旋转一个灯是令人沮丧的。
使用以下代码控制平行光的方向:
考虑到定向光的位置并不重要!
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。