我正在尝试将朝向相机的定向光放置为模拟太阳(如用户将相机指向太阳,并按下按钮以放置定向光源)。
到目前为止,我试图采用相机视角的quaternion conjugate并将其分配给灯光旋转。然而,这种方法似乎不起作用,并且光的结果位置看起来相当随机。非常感谢任何关于如何处理这种情况的建议。这是我的代码:
//Disable the default lightning updates
sceneView.autoenablesDefaultLighting = false
sceneView.automaticallyUpdatesLighting = false
var sunLight = SCNLight()
sunLight.intensity = 1000
sunLight.type = .directional
sunLight.color = UIColor.white
sunLight.castsShadow = true
sunLight.shadowMode = .forward
sunLight.automaticallyAdjustsShadowProjection = true
var sunNode = SCNNode()
sunNode.light = sunLight
sunNode.castsShadow = true
guard let cameraNode = self.sceneView.pointOfView else { return }
let q = cameraNode.orientation
let qConjugate = SCNVector4(x: -q.x, y: -q.y, z: -q.z, w: q.w)
sunNode.rotation = qConjugate
sceneView.scene.rootNode.addChildNode(sunNode)
通过放置一个球体,我可以看到光源来自哪个方向:
let sphere = SCNSphere(radius: 0.3)
sphere.firstMaterial?.diffuse.contents = UIColor.green.withAlphaComponent(1)
let sphereNode = SCNNode(geometry: sphere)
sphereNode.position = SCNVector3(0,0,0)
sceneView.scene.rootNode.addChildNode(sphereNode)
这是你应该使用SCNConstraint
的情况。定向节点始终指向当前相机的约束是SCNBillboardConstraint
。
SCNBillboardConstraint
对象自动调整节点的方向,使其局部z轴始终指向当前用于渲染场景的pointOfView
节点。例如,您可以使用广告牌约束来使用二维精灵图像而不是三维几何图形高效渲染场景的一部分 - 通过将精灵映射到受广告牌约束影响的平面上,精灵保持其相对于查看者的方向。
let sunNode = SCNNode()
sunNode.light = SCNLight()
sunNode.light!.type = .directional
sunNode.light!.color = UIColor.yellow
sunNode.light!.intensity = 5000
let sunConstraint = SCNBillboardConstraint()
sunNode.constraints = [SCNBillboardConstraint()]
sunNode.constraints?.append(sunConstraint)