美文网首页AR之道
AR Foudation案例解析(四) ------ Light

AR Foudation案例解析(四) ------ Light

作者: 千喜Ya | 来源:发表于2019-07-01 14:36 被阅读1次

    一.环境

    案例网址 : https://github.com/Unity-Technologies/arfoundation-samples.git
    官方文档网址 : https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@2.2/api/UnityEngine.XR.ARFoundation.html
    Unity版本 : 2019.3.0
    SDK版本 :

    二.案例解析

    1.功能

    获取光估计信息

    2.项目结构分析


    主要脚本在Directional Light :


    3.代码解析

    using UnityEngine;
    using UnityEngine.XR.ARFoundation;
    
    /// <summary>
    /// 一种组件,可用于访问AR设备所观察到的物理环境中最近接收到的光估计信息。
    /// </summary>
    [RequireComponent(typeof(Light))]
    public class LightEstimation : MonoBehaviour
    {
        [SerializeField]
        [Tooltip("The ARCameraManager which will produce frame events containing light estimation information.")]
        ARCameraManager m_CameraManager;
    
        /// <summary>
        /// Get or set the <c>ARCameraManager</c>.
        /// </summary>
        public ARCameraManager cameraManager
        {
            get { return m_CameraManager; }
            set
            {
                if (m_CameraManager == value)
                    return;
    
                if (m_CameraManager != null)
                    m_CameraManager.frameReceived -= FrameChanged;
    
                m_CameraManager = value;
    
                if (m_CameraManager != null & enabled)
                    m_CameraManager.frameReceived += FrameChanged;
            }
        }
    
        /// <summary>
        ///物理环境的估计亮度(如果可用)。
        /// </summary>
        public float? brightness { get; private set; }
    
        /// <summary>
        /// 物理环境的估计色温(如果可用)。
        /// </summary>
        public float? colorTemperature { get; private set; }
    
        /// <summary>
        /// 估计的物理环境的颜色校正值,如果可用。
        /// </summary>
        public Color? colorCorrection { get; private set; }
    
        void Awake ()
        {
            m_Light = GetComponent<Light>();
        }
    
        void OnEnable()
        {
            //添加ARCameraFrameEventArgs每帧监听事件
            if (m_CameraManager != null)
                m_CameraManager.frameReceived += FrameChanged;
        }
    
        void OnDisable()
        {
            //移除监听事件
            if (m_CameraManager != null)
                m_CameraManager.frameReceived -= FrameChanged;
        }
    
        void FrameChanged(ARCameraFrameEventArgs args)
        {
            if (args.lightEstimation.averageBrightness.HasValue)
            {
                brightness = args.lightEstimation.averageBrightness.Value;
                m_Light.intensity = brightness.Value;
            }
    
            if (args.lightEstimation.averageColorTemperature.HasValue)
            {
                colorTemperature = args.lightEstimation.averageColorTemperature.Value;
                m_Light.colorTemperature = colorTemperature.Value;
            }
            
            if (args.lightEstimation.colorCorrection.HasValue)
            {
                colorCorrection = args.lightEstimation.colorCorrection.Value;
                m_Light.color = colorCorrection.Value;
            }
        }
    
        Light m_Light;
    }
    

    相关文章

      网友评论

        本文标题:AR Foudation案例解析(四) ------ Light

        本文链接:https://www.haomeiwen.com/subject/vtpgcctx.html