Exemple #1
0
        private void ProcessSettings(GlobalDynamicResolutionSettings settings)
        {
            m_Enabled = settings.enabled && (Application.isPlaying || settings.forceResolution);
            if (!m_Enabled)
            {
                m_CurrentFraction = 1.0f;
            }
            else
            {
                type = settings.dynResType;
                float minScreenFrac = Mathf.Clamp(settings.minPercentage / 100.0f, 0.1f, 1.0f);
                m_MinScreenFraction = minScreenFrac;
                float maxScreenFrac = Mathf.Clamp(settings.maxPercentage / 100.0f, m_MinScreenFraction, 3.0f);
                m_MaxScreenFraction = maxScreenFrac;

                filter = settings.upsampleFilter;
                m_ForcingRes = settings.forceResolution;

                if (m_ForcingRes)
                {
                    float fraction = Mathf.Clamp(settings.forcedPercentage / 100.0f, 0.1f, 1.5f);
                    m_CurrentFraction = fraction;
                }
            }
        }
        /// <summary>
        /// Update the state of the dynamic resolution system.
        /// </summary>
        /// <param name="settings">The settings that are to be used by the dynamic resolution system.</param>
        /// <param name="OnResolutionChange">An action that will be called every time the dynamic resolution system triggers a change in resolution.</param>
        public void Update(GlobalDynamicResolutionSettings settings, Action OnResolutionChange = null)
        {
            ProcessSettings(settings);

            if (!m_Enabled || !s_ActiveInstanceDirty)
            {
                FlushScalableBufferManagerState();
                s_ActiveInstanceDirty = false;
                return;
            }

            if (!m_ForcingRes)
            {
                ref ScalerContainer scaler = ref s_ScalerContainers[(int)s_ActiveScalerSlot];
                if (scaler.type == DynamicResScalePolicyType.ReturnsMinMaxLerpFactor)
                {
                    float currLerp   = scaler.method();
                    float lerpFactor = Mathf.Clamp(currLerp, 0.0f, 1.0f);
                    m_CurrentFraction = Mathf.Lerp(m_MinScreenFraction, m_MaxScreenFraction, lerpFactor);
                }
                else if (scaler.type == DynamicResScalePolicyType.ReturnsPercentage)
                {
                    float percentageRequested = Mathf.Max(scaler.method(), 5.0f);
                    m_CurrentFraction = Mathf.Clamp(percentageRequested / 100.0f, m_MinScreenFraction, m_MaxScreenFraction);
                }
            }
        private void ProcessSettings(GlobalDynamicResolutionSettings settings)
        {
            m_Enabled = settings.enabled && (Application.isPlaying || settings.forceResolution);

            if (!m_Enabled)
            {
                m_CurrentFraction = 1.0f;
            }
            else
            {
                type         = settings.dynResType;
                m_UseMipBias = settings.useMipBias;
                float minScreenFrac = Mathf.Clamp(settings.minPercentage / 100.0f, 0.1f, 1.0f);
                m_MinScreenFraction = minScreenFrac;
                float maxScreenFrac = Mathf.Clamp(settings.maxPercentage / 100.0f, m_MinScreenFraction, 3.0f);
                m_MaxScreenFraction = maxScreenFrac;

                // Check if a filter has been set via user API, if so we use that, otherwise we use the default from the GlobalDynamicResolutionSettings
                bool hasUserRequestedFilter = s_CameraUpscaleFilters.TryGetValue(s_ActiveCameraId, out DynamicResUpscaleFilter requestedFilter);

                filter       = hasUserRequestedFilter ? requestedFilter : settings.upsampleFilter;
                m_ForcingRes = settings.forceResolution;

                if (m_ForcingRes)
                {
                    float fraction = Mathf.Clamp(settings.forcedPercentage / 100.0f, 0.1f, 1.5f);
                    m_CurrentFraction = fraction;
                }
            }
            m_CachedSettings = settings;
        }
        /// <summary>
        /// Update the state of the dynamic resolution system.
        /// </summary>
        /// <param name="settings">The settings that are to be used by the dynamic resolution system.</param>
        /// <param name="OnResolutionChange">An action that will be called every time the dynamic resolution system triggers a change in resolution.</param>
        public void Update(GlobalDynamicResolutionSettings settings, Action OnResolutionChange = null)
        {
            ProcessSettings(settings);

            if (!m_Enabled)
            {
                return;
            }

            if (!m_ForcingRes)
            {
                if (m_ScalerType == DynamicResScalePolicyType.ReturnsMinMaxLerpFactor)
                {
                    float currLerp   = m_DynamicResMethod();
                    float lerpFactor = Mathf.Clamp(currLerp, 0.0f, 1.0f);
                    m_CurrentFraction = Mathf.Lerp(m_MinScreenFraction, m_MaxScreenFraction, lerpFactor);
                }
                else if (m_ScalerType == DynamicResScalePolicyType.ReturnsPercentage)
                {
                    float percentageRequested = Mathf.Max(m_DynamicResMethod(), 5.0f);
                    m_CurrentFraction = Mathf.Clamp(percentageRequested / 100.0f, m_MinScreenFraction, m_MaxScreenFraction);
                }
            }

            if (m_CurrentFraction != m_PrevFraction)
            {
                m_PrevFraction = m_CurrentFraction;

                if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware)
                {
                    ScalableBufferManager.ResizeBuffers(m_CurrentFraction, m_CurrentFraction);
                }

                if (OnResolutionChange != null)
                {
                    OnResolutionChange();
                }
            }
            else
            {
                // Unity can change the scale factor by itself so we need to trigger the Action if that happens as well.
                if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware)
                {
                    if (ScalableBufferManager.widthScaleFactor != m_PrevHWScaleWidth ||
                        ScalableBufferManager.heightScaleFactor != m_PrevHWScaleHeight)
                    {
                        if (OnResolutionChange != null)
                        {
                            OnResolutionChange();
                        }
                    }
                }
            }

            m_PrevHWScaleWidth  = ScalableBufferManager.widthScaleFactor;
            m_PrevHWScaleHeight = ScalableBufferManager.heightScaleFactor;
        }
        /// <summary>
        /// Update the state of the dynamic resolution system.
        /// </summary>
        /// <param name="settings">The settings that are to be used by the dynamic resolution system.</param>
        /// <param name="OnResolutionChange">An action that will be called every time the dynamic resolution system triggers a change in resolution.</param>
        public void Update(GlobalDynamicResolutionSettings settings, Action OnResolutionChange = null)
        {
            ProcessSettings(settings);

            if (!m_Enabled && !s_ActiveInstanceDirty)
            {
                s_ActiveInstanceDirty = false;
                return;
            }

            if (!m_ForcingRes)
            {
                if (s_ScalerType == DynamicResScalePolicyType.ReturnsMinMaxLerpFactor)
                {
                    float currLerp   = s_DynamicResMethod();
                    float lerpFactor = Mathf.Clamp(currLerp, 0.0f, 1.0f);
                    m_CurrentFraction = Mathf.Lerp(m_MinScreenFraction, m_MaxScreenFraction, lerpFactor);
                }
                else if (s_ScalerType == DynamicResScalePolicyType.ReturnsPercentage)
                {
                    float percentageRequested = Mathf.Max(s_DynamicResMethod(), 5.0f);
                    m_CurrentFraction = Mathf.Clamp(percentageRequested / 100.0f, m_MinScreenFraction, m_MaxScreenFraction);
                }
            }

            bool hardwareResolutionChanged = false;
            bool softwareResolutionChanged = m_CurrentFraction != m_PrevFraction;

            m_PrevFraction = m_CurrentFraction;

            if (!m_ForceSoftwareFallback && type == DynamicResolutionType.Hardware)
            {
                hardwareResolutionChanged = FlushScalableBufferManagerState();
                if (ScalableBufferManager.widthScaleFactor != m_PrevHWScaleWidth ||
                    ScalableBufferManager.heightScaleFactor != m_PrevHWScaleHeight)
                {
                    hardwareResolutionChanged = true;
                }
            }


            if ((softwareResolutionChanged || hardwareResolutionChanged) && OnResolutionChange != null)
            {
                OnResolutionChange();
            }

            s_ActiveInstanceDirty = false;
            m_PrevHWScaleWidth    = ScalableBufferManager.widthScaleFactor;
            m_PrevHWScaleHeight   = ScalableBufferManager.heightScaleFactor;
        }