当前位置:   article > 正文

【iOS ARKit】光照估计

【iOS ARKit】光照估计

光照估计

        AR与VR 在光照上最大的不同在于VR 世界是纯数字世界,有一套完整的数学模型,而AR则是将计算机生成的虚拟物体或关于真实物体的非几何信息叠加到真实世界的场景之上实现对真实世界的增强,融合了真实世界与数字世界。就光照而言,VR中的光照完全由开发人员决定,光照效果是一致的,即不会受到运行时其他因素的影响,而AR 中则不得不考虑真实世界的光照与虚拟的3D光照信息的一致性,举个例子,假如在AR 3D应用中设置了一个模拟太阳的高亮度方向光,而用户是在晚上使用这个 AR应用,如果不考虑光照一致性,那么渲染出来的虚拟物体的光照与真实世界其他物体的光照反差将会非常明显,由于人眼对光照信息的高度敏感性,这种渲染可以说是完全失败的,完全没有沉浸感。在AR 中,由于用户与真实世界的联系并未被切断,光照的交互方式也要求更自然,如果真实世界的阴影向左而渲染出来的虚拟物体图影向右,这也是让人难以接受的,所以在 AR 中,必须要能达到虛拟光照与真实光照的一致,虚拟物体渲染出来的阴影应与真实环境中的阴影基本保持一致,这样才能提商虛拟物体的可信度和真实感。

ARKit 中的光照估汁

     ARKit 支持对用户所处环境光照信息的估计,在 ARConfiguration 类中定义了 isLightEstimationEnabled 布尔属性用于开启和关闭光照估计,该功能默认为启用状态。由于 ARConfiguration 是所有其他配置类的父类,因此 ARKit所有的配置类都支持光照估计功能。

     当设置 isLightEstimationEnabled 值为true 时,ARKit 每帧都会对从设备摄像头中采集的图像进行光照估计计算,并且将估计值保存在 frame. lightEstimate 属性中,frame. lightEstimate 是 ARLightEstimate类的实例,ARLightEstimate类只包含两个属性,

  1. 环境光强度值 (ambientIntensity) 取值范围[0,2000],在光照条件良好的环境中,该值约为1000,0表示环境非常黑暗,2000表示环境非常明亮
  2. 环境光温度值(ambientColorTemperature) 单位开尔文(K),纯白光次6500,低于该值表示环境更温暖,更偏向于黄光或者橘黄光,而高于该值表示环境更冷,更偏向于蓝光

     在 RealityKit 中,一般情况下开发人员无须关注 frame. lightEstimate 中的值,当设置 isLightEstimationEnabled值为 true 时,RealityKit 会自动使用环境光照估计值渲染虚拟元素的光照。但在使用自定义渲染时,开发人员必须自行处理光照估计。

    但在一些情况下,我们也可能需要实时获取当前环境光照估计值,如根据环境光照动态调整特效类型,下面演示如何启用光照估计并获取实时的光照估计值,如代码所示。

  1. //
  2. // LightEstimate.swift
  3. // ARKitDeamo
  4. //
  5. // Created by zhaoquan du on 2024/1/29.
  6. //
  7. import SwiftUI
  8. import ARKit
  9. import RealityKit
  10. import Combine
  11. struct LightEstimate: View {
  12. @State var isFaceTracking = false
  13. var body: some View {
  14. LightEstimateContainer(isFaceTracking: isFaceTracking)
  15. .overlay(content: {
  16. VStack{
  17. Spacer()
  18. Button {
  19. isFaceTracking.toggle()
  20. } label: {
  21. Text( !isFaceTracking ? "人脸追踪光照": "普通光照估计")
  22. .frame(width:150,height:50)
  23. .font(.system(size: 17))
  24. .foregroundColor(.black)
  25. .background(Color.white)
  26. .opacity(0.6)
  27. }
  28. .cornerRadius(10)
  29. Spacer().frame(height: 40)
  30. }
  31. })
  32. .edgesIgnoringSafeArea(.all)
  33. .navigationTitle("光照估计")
  34. }
  35. }
  36. struct LightEstimateContainer: UIViewRepresentable {
  37. var isFaceTracking: Bool = false
  38. init(isFaceTracking: Bool = false) {
  39. self.isFaceTracking = isFaceTracking
  40. }
  41. func makeUIView(context: Context) -> ARView {
  42. let arView = ARView(frame: .zero)
  43. return arView
  44. }
  45. func updateUIView(_ uiView: ARView, context: Context) {
  46. if isFaceTracking {
  47. let config = ARFaceTrackingConfiguration()
  48. config.isLightEstimationEnabled = true
  49. uiView.session.delegate = context.coordinator
  50. context.coordinator.times = 0
  51. uiView.session.run(config, options: [.resetTracking,.removeExistingAnchors])
  52. return
  53. }
  54. let config = ARWorldTrackingConfiguration()
  55. config.planeDetection = .horizontal
  56. config.isLightEstimationEnabled = true
  57. context.coordinator.arView = uiView
  58. uiView.session.delegate = context.coordinator
  59. uiView.session.run(config)
  60. }
  61. func makeCoordinator() -> Coordinator {
  62. Coordinator(parent: self)
  63. }
  64. class Coordinator: NSObject,ARSessionDelegate {
  65. var arView:ARView? = nil
  66. var isPlaced = false
  67. var times = 0
  68. var parent: LightEstimateContainer
  69. init(parent: LightEstimateContainer) {
  70. self.parent = parent
  71. }
  72. func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
  73. guard let anchor = anchors.first as? ARPlaneAnchor,!isPlaced else {
  74. return
  75. }
  76. do {
  77. let planEntity = AnchorEntity(anchor: anchor)
  78. let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.003)
  79. let texture = MaterialParameters.Texture.init(try TextureResource.load(named: "Box_Texture.jpg"))
  80. var meterial = SimpleMaterial(color: .blue,roughness: 0.8 ,isMetallic: false)
  81. meterial.color = .init(tint:.blue,texture:texture)
  82. let modelEntity = ModelEntity(mesh: mesh, materials: [meterial])
  83. planEntity.addChild(modelEntity)
  84. arView?.installGestures(for:modelEntity)
  85. arView?.scene.addAnchor(planEntity)
  86. }catch{
  87. print("无法加载纹理")
  88. }
  89. }
  90. func session(_ session: ARSession, didUpdate frame: ARFrame) {
  91. guard let estimatLight = frame.lightEstimate , times < 10 else {return }
  92. print("light intensity: \(estimatLight.ambientIntensity),light temperature: \(estimatLight.ambientColorTemperature)")
  93. if let estimatLight = frame.lightEstimate as? ARDirectionalLightEstimate {
  94. print("primary light direction: \(estimatLight.primaryLightDirection), primary light intensity: \(estimatLight.primaryLightIntensity)")
  95. }
  96. times += 1
  97. }
  98. }
  99. }

        代码中代码逻辑非常清晰,我们使用 session(:didUpdate frame:)代理方法实时地获取每一帧的光照估计值,并打印了光照估计值强度及色温信息。运行代码,在检测到的水平平面上加载木箱物体后,改变真实环境中的光照,可以看到虚拟的木箱光照信息也发生了明显的变化。

  • 经过测试,发现在使用 RealityKit 渲染时,即使设置 isLightEstimation Enabled 为 false 也会在渲染虛拟元素时考虑光照估计,但此时无法从 frame. lightEstimate 中获取光照估计值。

      除了通用的光照估计,在使用 ARFaceTrackingConfiguration 配置运行 ARSession 时,ARKit 会提供更多关于环境光照的估计信息。在使用 ARFace TrackingConfiguration 配置运行 ARSession,当设置 isLightEstimationEnabled 值为 true 时,ARKit 每帧都会对从设备摄像头中采集的图像进行光照估计计算,并且将估计值保存在 frame. lightEstimate 属性中,但此时 frame. lightEstimate 为 ARDirectionalLightEstimate 类的实例,ARDirectionalLightEstimate类为 ARLightEstimate 的子类,不仅包括 ARLightEstimate 中的属性,还包含另外3个光照估计值属性。

  • primary LightDirectio 场景中最强光线的方向向量,这是一个在世界空间中归一化的向量
  • primary LightIntensit  场景中最强光线的光照强度,单位为流明,取值范围[0,2000],在光照条件良好的环境中,该值约在1000,0表示环境非常黑暗,2000表示环境非常明亮
  • spherical HarmonicsCoefficients  对环境中多个光源方向与强度的综合表达。球谐因子提供了一种在某点反映全局环境光照信息的简洁紧凑模型,描述了在该点的多个光源光照分布与颜色值。在IML. 渲染中,球谐因子非常高效。ARKit 光照估计提供二级(Second level)红、绿、蓝分离的3通道球谐因子,总数据包括3级9个因子,总计27个32位的浮点值。

      在Reality Kit 中,开发人员亦无须关注这些光照估计值,当设置 isLightEstimationEnabled 值为 true5,Reality Kit就会自动使用环境光照估计值渲染虚拟元素。但在使用自定义谊染时,开发人员必须自己处理光照估计。下面代码是演示在使用 ARFaceTrackingConfiguration 配置时,启用光照估计并获取实时的光照估计值。

  1. func updateUIView(_ uiView: ARView, context: Context) {
  2. let config = ARFaceTrackingConfiguration()
  3. config.isLightEstimationEnabled = true
  4. uiView.session.delegate = context.coordinator
  5. context.coordinator.times = 0
  6. uiView.session.run(config, options: [.resetTracking,.removeExistingAnchors])
  7. return
  8. }
  9. func session(_ session: ARSession, didUpdate frame: ARFrame) {
  10. guard let estimatLight = frame.lightEstimate , times < 10 else {return }
  11. print("light intensity: \(estimatLight.ambientIntensity),light temperature: \(estimatLight.ambientColorTemperature)")
  12. if let estimatLight = frame.lightEstimate as? ARDirectionalLightEstimate {
  13. print("primary light direction: \(estimatLight.primaryLightDirection), primary light intensity: \(estimatLight.primaryLightIntensity)")
  14. }
  15. times += 1
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/47886
推荐阅读
相关标签
  

闽ICP备14008679号