neinnew / FogController

https://steamcommunity.com/sharedfiles/filedetails/?id=2483920668
0 stars 0 forks source link

Possible performance improvements #1

Closed originalfoo closed 2 years ago

originalfoo commented 2 years ago

Suggestion for your ModThreading class which can significantly reduce fps drop.

Note I've not tested this code, it's just "rough sketch" with some comments explaining the approach...

namespace FogController
{
    public class ModThreading : ThreadingExtensionBase
    {
        private RenderProperties renderProps;

        public void OnEnabled()
        {
            FCSettings.LoadSettings();

            // only need to do this once; the value isn't going to be changing every frame
            renderProps = UnityEngine.Object.FindObjectOfType<RenderProperties>();

            enabled = FCSettings.inscatteringcolor == 1;
        }
        public void OnDisabled() // or is it OnDestroy() ?
        {
           renderProps = null;
        }

        public override void OnUpdate(float realTimeDelta, float simulationTimeDelta)
        {
                inscol.m_inscatteringColor = DayNightProperties.instance.currentLightColor;
        }
    }
}

FindObjectOfType() is crazy slow and not something you want to do every frame; removing it from OnUpdate() should give immediate performance improvement to your mod.

I can't remember if ThreadingExtensionBase is MonoBehaviour (been a while since I was CSL coding) but, assuming it is, then when enabled = false the OnUpdate method won't even be called thus no per-frame impact from that method.

Store a reference to the ModThreading instance once it's created; if user changes FCSettings.inscatteringcolor set the .enabled accordingly to activate/deactivate the OnUpdate cycle.

I did something similar in the Additive Shader mod I was working on some time ago (it uses a MonoBehaviour directly, but you get the general idea of what's going on): https://github.com/CitiesSkylinesMods/AdditiveShader/blob/master/Source/AdditiveShader/Manager/AdditiveShaderManager.cs

neinnew commented 2 years ago

Thanks for your insight! I'll look into this soon