【iOS ARKit】光照估计

本文探讨了AR与VR在光照处理上的区别,重点介绍了ARKit如何进行光照估计,包括环境光强度和色温的获取,以及在使用ARFaceTrackingConfiguration时的额外光照信息。开发者需注意在自定义渲染时处理光照一致性问题。
摘要由CSDN通过智能技术生成

光照估计

        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 会自动使用环境光照估计值渲染虚拟元素的光照。但在使用自定义渲染时,开发人员必须自行处理光照估计。

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

//
//  LightEstimate.swift
//  ARKitDeamo
//
//  Created by zhaoquan du on 2024/1/29.
//

import SwiftUI
import ARKit
import RealityKit
import Combine

struct LightEstimate: View {
    @State var isFaceTracking = false
    var body: some View {
        LightEstimateContainer(isFaceTracking: isFaceTracking)
            .overlay(content: {
                VStack{
                    Spacer()
                    
                        Button {
                            isFaceTracking.toggle()
                        } label: {
                            
                            Text( !isFaceTracking ? "人脸追踪光照": "普通光照估计")
                                .frame(width:150,height:50)
                                .font(.system(size: 17))
                                .foregroundColor(.black)
                                .background(Color.white)
                            
                                .opacity(0.6)
                                
                        }
                            
                        .cornerRadius(10)
                        Spacer().frame(height: 40)
                    
                    

                }
            })
            .edgesIgnoringSafeArea(.all)
            .navigationTitle("光照估计")
    }
}

struct LightEstimateContainer: UIViewRepresentable {
    var isFaceTracking: Bool = false
    init(isFaceTracking: Bool = false) {
        self.isFaceTracking = isFaceTracking
    }
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        
        
        return arView
    }
    
    func updateUIView(_ uiView: ARView, context: Context) {
        if isFaceTracking {
            
            let config = ARFaceTrackingConfiguration()
            config.isLightEstimationEnabled = true
            uiView.session.delegate = context.coordinator
            context.coordinator.times = 0
            uiView.session.run(config, options: [.resetTracking,.removeExistingAnchors])
            return
        }
        
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = .horizontal
        config.isLightEstimationEnabled = true
        
        context.coordinator.arView = uiView
        uiView.session.delegate = context.coordinator
        uiView.session.run(config)
        
        
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(parent: self)
    }
    
    
    class  Coordinator: NSObject,ARSessionDelegate {
        var arView:ARView? = nil
        var isPlaced = false
        var times = 0
        var parent: LightEstimateContainer
        init(parent: LightEstimateContainer) {
            self.parent = parent
        }
        
        func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
            guard let anchor = anchors.first as? ARPlaneAnchor,!isPlaced else {
                return
            }
            do {
                let planEntity = AnchorEntity(anchor: anchor)
                let mesh = MeshResource.generateBox(size: 0.1, cornerRadius: 0.003)
                let texture = MaterialParameters.Texture.init(try TextureResource.load(named: "Box_Texture.jpg"))
                var meterial = SimpleMaterial(color: .blue,roughness: 0.8 ,isMetallic: false)
                meterial.color = .init(tint:.blue,texture:texture)
                
                let modelEntity = ModelEntity(mesh: mesh, materials: [meterial])
                planEntity.addChild(modelEntity)
                
                arView?.installGestures(for:modelEntity)
                arView?.scene.addAnchor(planEntity)
                
            }catch{
                print("无法加载纹理")
            }
        }
        
        func session(_ session: ARSession, didUpdate frame: ARFrame) {
            guard let estimatLight = frame.lightEstimate , times < 10 else {return }
            print("light intensity: \(estimatLight.ambientIntensity),light temperature: \(estimatLight.ambientColorTemperature)")
            if let estimatLight = frame.lightEstimate as? ARDirectionalLightEstimate {
                print("primary light direction: \(estimatLight.primaryLightDirection), primary light intensity: \(estimatLight.primaryLightIntensity)")
            }
            times += 1
            
        }
    }
}

        代码中代码逻辑非常清晰,我们使用 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 配置时,启用光照估计并获取实时的光照估计值。

func updateUIView(_ uiView: ARView, context: Context) {
        
            
            let config = ARFaceTrackingConfiguration()
            config.isLightEstimationEnabled = true
            uiView.session.delegate = context.coordinator
            context.coordinator.times = 0
            uiView.session.run(config, options: [.resetTracking,.removeExistingAnchors])
            return
        
        
        
    }


func session(_ session: ARSession, didUpdate frame: ARFrame) {
            guard let estimatLight = frame.lightEstimate , times < 10 else {return }
            print("light intensity: \(estimatLight.ambientIntensity),light temperature: \(estimatLight.ambientColorTemperature)")
            if let estimatLight = frame.lightEstimate as? ARDirectionalLightEstimate {
                print("primary light direction: \(estimatLight.primaryLightDirection), primary light intensity: \(estimatLight.primaryLightIntensity)")
            }
            times += 1
            
        }

  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值