При условии, что вы добавили легкий узел с именем 'light' с SCNLight
, прикрепленным к нему в вашем "ship.scn" SCNScene
, и что ваш ViewController
соответствует ARSessionDelegate, так что вы можете получить оценку освещенности на кадр :
class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate {
@IBOutlet var sceneView: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
let scene = SCNScene(named: "art.scnassets/ship.scn")!
sceneView.scene = scene
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let configuration = ARWorldTrackingConfiguration()
configuration.isLightEstimationEnabled = true
sceneView.session.run(configuration)
sceneView.session.delegate = self
}
func session(_ session: ARSession, didUpdate frame: ARFrame) {
guard let lightEstimate = frame.lightEstimate,
let light = sceneView.scene.rootNode.childNode(withName: "light", recursively: false)?.light else {return}
light.temperature = lightEstimate.ambientColorTemperature
light.intensity = lightEstimate.ambientIntensity
}
}
В результате, если вы приглушите свет в своей комнате, SceneKit также приглушит виртуальный свет.