Browse Source

Rework effect managers and fix bloom

Effect panes can now get initial effect manager values now that the data
is initialized at construction.

Bloom has been fixed by modifying CMSystem's BloomValue and changing
CameraMain's bloom effect's bloomDefIntensity to match MultipleMaid's
bloom.

Fixes #7
habeebweeb 4 years ago
parent
commit
140fa86ce7

+ 15 - 11
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/BloomPane.cs

@@ -12,43 +12,47 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private readonly Slider blueSlider;
         private readonly Toggle hdrToggle;
 
-        public BloomPane(EffectManager effectManager) : base(effectManager.Get<BloomEffectManager>())
+        public BloomPane(EffectManager effectManager) : base(effectManager)
         {
-            Bloom bloom = GameMain.Instance.MainCamera.GetComponent<Bloom>();
-
             intensitySlider = new Slider(
-                Translation.Get("effectBloom", "intensity"), 0f, 5.7f, bloom.bloomIntensity
+                Translation.Get("effectBloom", "intensity"), 0f, 100f, EffectManager.BloomValue
             );
             intensitySlider.ControlEvent += (s, a) =>
             {
                 if (updating) return;
-                EffectManager.Intensity = intensitySlider.Value;
+                EffectManager.BloomValue = intensitySlider.Value;
             };
-            blurSlider = new Slider(Translation.Get("effectBloom", "blur"), 0f, 15f, bloom.bloomBlurIterations);
+            blurSlider = new Slider(Translation.Get("effectBloom", "blur"), 0f, 15f, EffectManager.BlurIterations);
             blurSlider.ControlEvent += (s, a) =>
             {
                 if (updating) return;
                 EffectManager.BlurIterations = (int)blurSlider.Value;
             };
-            redSlider = new Slider(Translation.Get("backgroundWindow", "red"), 1f, 0.5f, 1f);
+            redSlider = new Slider(
+                Translation.Get("backgroundWindow", "red"), 1f, 0.5f, EffectManager.BloomThresholdColorRed
+            );
             redSlider.ControlEvent += (s, a) =>
             {
                 if (updating) return;
                 EffectManager.BloomThresholdColorRed = redSlider.Value;
             };
-            greenSlider = new Slider(Translation.Get("backgroundWindow", "green"), 1f, 0.5f, 1f);
+            greenSlider = new Slider(
+                Translation.Get("backgroundWindow", "green"), 1f, 0.5f, EffectManager.BloomThresholdColorGreen
+            );
             greenSlider.ControlEvent += (s, a) =>
             {
                 if (updating) return;
                 EffectManager.BloomThresholdColorGreen = greenSlider.Value;
             };
-            blueSlider = new Slider(Translation.Get("backgroundWindow", "blue"), 1f, 0.5f, 1f);
+            blueSlider = new Slider(
+                Translation.Get("backgroundWindow", "blue"), 1f, 0.5f, EffectManager.BloomThresholdColorBlue
+            );
             blueSlider.ControlEvent += (s, a) =>
             {
                 if (updating) return;
                 EffectManager.BloomThresholdColorBlue = blueSlider.Value;
             };
-            hdrToggle = new Toggle(Translation.Get("effectBloom", "hdrToggle"));
+            hdrToggle = new Toggle(Translation.Get("effectBloom", "hdrToggle"), EffectManager.BloomHDR);
             hdrToggle.ControlEvent += (s, a) =>
             {
                 if (updating) return;
@@ -68,7 +72,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         protected override void UpdateControls()
         {
-            intensitySlider.Value = EffectManager.Intensity;
+            intensitySlider.Value = EffectManager.BloomValue;
             blurSlider.Value = EffectManager.BlurIterations;
             redSlider.Value = EffectManager.BloomThresholdColorRed;
             greenSlider.Value = EffectManager.BloomThresholdColorGreen;

+ 33 - 11
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/DepthOfFieldPane.cs

@@ -11,18 +11,40 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private readonly Slider blurSlider;
         private readonly Toggle thicknessToggle;
 
-        public DepthOfFieldPane(EffectManager effectManager) : base(effectManager.Get<DepthOfFieldEffectManager>())
+        public DepthOfFieldPane(EffectManager effectManager) : base(effectManager)
         {
-            focalLengthSlider = new Slider(Translation.Get("effectDof", "focalLength"), 0f, 10f);
-            focalSizeSlider = new Slider(Translation.Get("effectDof", "focalArea"), 0f, 2f);
-            apertureSlider = new Slider(Translation.Get("effectDof", "aperture"), 0f, 60f);
-            blurSlider = new Slider(Translation.Get("effectDof", "blur"), 0f, 10f);
-            thicknessToggle = new Toggle(Translation.Get("effectDof", "thicknessToggle"));
-            focalLengthSlider.ControlEvent += (s, a) => EffectManager.FocalLength = focalLengthSlider.Value;
-            focalSizeSlider.ControlEvent += (s, a) => EffectManager.FocalSize = focalSizeSlider.Value;
-            apertureSlider.ControlEvent += (s, a) => EffectManager.Aperture = apertureSlider.Value;
-            blurSlider.ControlEvent += (s, a) => EffectManager.MaxBlurSize = blurSlider.Value;
-            thicknessToggle.ControlEvent += (s, a) => EffectManager.VisualizeFocus = thicknessToggle.Value;
+            focalLengthSlider = new Slider(
+                Translation.Get("effectDof", "focalLength"), 0f, 10f, EffectManager.FocalLength
+            );
+            focalSizeSlider = new Slider(Translation.Get("effectDof", "focalArea"), 0f, 2f, EffectManager.FocalSize);
+            apertureSlider = new Slider(Translation.Get("effectDof", "aperture"), 0f, 60f, EffectManager.Aperture);
+            blurSlider = new Slider(Translation.Get("effectDof", "blur"), 0f, 10f, EffectManager.MaxBlurSize);
+            thicknessToggle = new Toggle(Translation.Get("effectDof", "thicknessToggle"), EffectManager.VisualizeFocus);
+            focalLengthSlider.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                EffectManager.FocalLength = focalLengthSlider.Value;
+            };
+            focalSizeSlider.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                EffectManager.FocalSize = focalSizeSlider.Value;
+            };
+            apertureSlider.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                EffectManager.Aperture = apertureSlider.Value;
+            };
+            blurSlider.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                EffectManager.MaxBlurSize = blurSlider.Value;
+            };
+            thicknessToggle.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                EffectManager.VisualizeFocus = thicknessToggle.Value;
+            };
         }
 
         protected override void TranslatePane()

+ 8 - 3
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/EffectPane.cs

@@ -18,13 +18,17 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             }
         }
 
-        protected EffectPane(T effectManager)
+        protected EffectPane(EffectManager effectManager)
         {
-            EffectManager = effectManager;
+            EffectManager = effectManager.Get<T>();
             resetEffectButton = new Button(Translation.Get("effectsPane", "reset"));
             resetEffectButton.ControlEvent += (s, a) => ResetEffect();
             effectToggle = new Toggle(Translation.Get("effectsPane", "onToggle"));
-            effectToggle.ControlEvent += (s, a) => Enabled = effectToggle.Value;
+            effectToggle.ControlEvent += (s, a) =>
+            {
+                if (updating) return;
+                Enabled = effectToggle.Value;
+            };
         }
 
         protected override void ReloadTranslation()
@@ -41,6 +45,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         public override void UpdatePane()
         {
             if (!EffectManager.Ready) return;
+            if (EffectManager.Active != effectToggle.Value) EffectManager.SetEffectActive(effectToggle.Value);
             updating = true;
             effectToggle.Value = EffectManager.Active;
             UpdateControls();

+ 1 - 4
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/EffectsPane.cs

@@ -37,10 +37,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             currentEffectPane.UpdatePane();
         }
 
-        public override void UpdatePane()
-        {
-            currentEffectPane.UpdatePane();
-        }
+        public override void UpdatePane() => currentEffectPane.UpdatePane();
 
         public override void Draw()
         {

+ 6 - 6
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/FogPane.cs

@@ -13,21 +13,21 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private readonly Slider greenSlider;
         private readonly Slider blueSlider;
 
-        public FogPane(EffectManager effectManager) : base(effectManager.Get<FogEffectManager>())
+        public FogPane(EffectManager effectManager) : base(effectManager)
         {
             distanceSlider = new Slider(
-                Translation.Get("effectFog", "distance"), 0f, 30f, FogEffectManager.InitialDistance
+                Translation.Get("effectFog", "distance"), 0f, 30f, EffectManager.Distance
             );
             densitySlider = new Slider(
-                Translation.Get("effectFog", "density"), 0f, 10f, FogEffectManager.InitialDensity
+                Translation.Get("effectFog", "density"), 0f, 10f, EffectManager.Density
             );
             heightScaleSlider = new Slider(
-                Translation.Get("effectFog", "strength"), -5f, 20f, FogEffectManager.InitialHeightScale
+                Translation.Get("effectFog", "strength"), -5f, 20f, EffectManager.HeightScale
             );
             heightSlider = new Slider(
-                Translation.Get("effectFog", "height"), -10f, 10f, FogEffectManager.InitialHeight
+                Translation.Get("effectFog", "height"), -10f, 10f, EffectManager.Height
             );
-            Color initialFogColour = FogEffectManager.InitialColour;
+            Color initialFogColour = EffectManager.FogColour;
             redSlider = new Slider(Translation.Get("backgroundWIndow", "red"), 0f, 1f, initialFogColour.r);
             greenSlider = new Slider(Translation.Get("backgroundWIndow", "green"), 0f, 1f, initialFogColour.g);
             blueSlider = new Slider(Translation.Get("backgroundWIndow", "blue"), 0f, 1f, initialFogColour.b);

+ 1 - 1
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/GUI/Panes/BackgroundWindowPanes/EffectsPanes/VignettePane.cs

@@ -10,7 +10,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private readonly Slider blurSpreadSlider;
         private readonly Slider aberrationSlider;
 
-        public VignettePane(EffectManager effectManager) : base(effectManager.Get<VignetteEffectManager>())
+        public VignettePane(EffectManager effectManager) : base(effectManager)
         {
             intensitySlider = new Slider(Translation.Get("effectVignette", "intensity"), -40f, 70f);
             intensitySlider.ControlEvent += (s, a) =>

+ 46 - 27
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EffectManagers/BloomEffectManager.cs

@@ -1,3 +1,4 @@
+using System.Reflection;
 using UnityEngine;
 
 namespace COM3D2.MeidoPhotoStudio.Plugin
@@ -5,14 +6,31 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
     internal class BloomEffectManager : IEffectManager
     {
         public const string header = "EFFECT_BLOOM";
+        private const float bloomDefIntensity = 5.7f;
+        private static readonly CameraMain camera = GameMain.Instance.MainCamera;
         private Bloom Bloom { get; set; }
+        // CMSystem's bloomValue;
+        private static int backupBloomValue;
+        private static readonly float backup_m_fBloomDefIntensity;
+        private static readonly FieldInfo m_fBloomDefIntensity
+            = Utility.GetFieldInfo<CameraMain>("m_fBloomDefIntensity");
+        private static float BloomDefIntensity
+        {
+            set => m_fBloomDefIntensity.SetValue(camera, value);
+            get => (float)m_fBloomDefIntensity.GetValue(camera);
+        }
         private float initialIntensity;
         private int initialBlurIterations;
         private Color initialThresholdColour;
         private Bloom.HDRBloomMode initialHDRBloomMode;
         public bool Ready { get; private set; }
         public bool Active { get; private set; }
-        public float Intensity { get; set; }
+        private float bloomValue;
+        public float BloomValue
+        {
+            get => bloomValue;
+            set => GameMain.Instance.CMSystem.BloomValue = (int)(bloomValue = value);
+        }
         private int blurIterations;
         public int BlurIterations
         {
@@ -52,21 +70,23 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             get => bloomThresholdColour;
             set => bloomThresholdColour = Bloom.bloomThreshholdColor = value;
         }
-        private bool HDRBloomMode;
+        private bool bloomHdr;
         public bool BloomHDR
         {
-            get => HDRBloomMode;
+            get => bloomHdr;
             set
             {
                 Bloom.hdr = value ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
-                HDRBloomMode = value;
+                bloomHdr = value;
             }
         }
 
+        static BloomEffectManager() => backup_m_fBloomDefIntensity = BloomDefIntensity;
+
         public void Serialize(System.IO.BinaryWriter binaryWriter)
         {
             binaryWriter.Write(header);
-            binaryWriter.Write(Intensity);
+            binaryWriter.Write(BloomValue);
             binaryWriter.Write(BlurIterations);
             binaryWriter.WriteColour(BloomThresholdColour);
             binaryWriter.Write(BloomHDR);
@@ -75,7 +95,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
 
         public void Deserialize(System.IO.BinaryReader binaryReader)
         {
-            Intensity = binaryReader.ReadSingle();
+            BloomValue = binaryReader.ReadSingle();
             BlurIterations = binaryReader.ReadInt32();
             BloomThresholdColour = binaryReader.ReadColour();
             BloomHDR = binaryReader.ReadBoolean();
@@ -87,57 +107,56 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             if (Bloom == null)
             {
                 Ready = true;
-                Bloom = Utility.GetFieldValue<CameraMain, Bloom>(GameMain.Instance.MainCamera, "m_gcBloom");
-                initialIntensity = Intensity = Bloom.bloomIntensity;
-                initialBlurIterations = BlurIterations = Bloom.bloomBlurIterations;
-                initialThresholdColour = BloomThresholdColour = Bloom.bloomThreshholdColor;
+                Bloom = GameMain.Instance.MainCamera.GetComponent<Bloom>();
+                initialIntensity = bloomValue = 50f;
+                initialBlurIterations = blurIterations = Bloom.bloomBlurIterations;
+                initialThresholdColour = bloomThresholdColour = Bloom.bloomThreshholdColor;
                 initialHDRBloomMode = Bloom.hdr;
-                BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
+                bloomHdr = Bloom.hdr == Bloom.HDRBloomMode.On;
+
+                backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
             }
+            SetEffectActive(false);
         }
 
         public void Deactivate()
         {
-            Intensity = initialIntensity;
+            BloomValue = initialIntensity;
             BlurIterations = initialBlurIterations;
             BloomThresholdColour = initialThresholdColour;
             BloomHDR = initialHDRBloomMode == Bloom.HDRBloomMode.On;
             BloomHDR = false;
-            Bloom.enabled = true;
             Active = false;
+
+            BloomDefIntensity = backup_m_fBloomDefIntensity;
+            GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
         }
 
         public void Reset()
         {
-            Bloom.bloomIntensity = initialIntensity;
+            GameMain.Instance.CMSystem.BloomValue = backupBloomValue;
             Bloom.bloomBlurIterations = initialBlurIterations;
             Bloom.bloomThreshholdColor = initialThresholdColour;
             Bloom.hdr = initialHDRBloomMode;
+
+            BloomDefIntensity = backup_m_fBloomDefIntensity;
         }
 
         public void SetEffectActive(bool active)
         {
-            Bloom.enabled = active;
             if (Active = active)
             {
-                Bloom.bloomIntensity = Intensity;
+                backupBloomValue = GameMain.Instance.CMSystem.BloomValue;
+                GameMain.Instance.CMSystem.BloomValue = (int)BloomValue;
                 Bloom.bloomBlurIterations = BlurIterations;
                 Bloom.bloomThreshholdColor = BloomThresholdColour;
                 Bloom.hdr = BloomHDR ? Bloom.HDRBloomMode.On : Bloom.HDRBloomMode.Auto;
+
+                BloomDefIntensity = bloomDefIntensity;
             }
             else Reset();
         }
 
-        public void Update()
-        {
-            if (Active)
-            {
-                // Fuck this stupid shit
-                // 2020/08/15 this stupid shit doesn't even work anymore
-                // TODO: Fix this stupid shit
-                Bloom.enabled = true;
-                Bloom.bloomIntensity = Intensity;
-            }
-        }
+        public void Update() { }
     }
 }

+ 1 - 0
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EffectManagers/DepthOfFieldManager.cs

@@ -81,6 +81,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                     DepthOfField.dx11BokehTexture = Resources.Load("Textures/hexShape") as Texture2D;
                 }
             }
+            SetEffectActive(false);
         }
 
         public void Deactivate()

+ 21 - 20
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EffectManagers/FogEffectManager.cs

@@ -8,11 +8,11 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
         private GlobalFog Fog { get; set; }
         public bool Ready { get; }
         public bool Active { get; private set; }
-        public static float InitialDistance { get; } = 4f;
-        public static float InitialDensity { get; } = 1f;
-        public static float InitialHeightScale { get; } = 1f;
-        public static float InitialHeight { get; }
-        public static Color InitialColour { get; private set; } = Color.white;
+        private readonly float initialDistance = 4f;
+        private readonly float initialDensity = 1f;
+        private readonly float initialHeightScale = 1f;
+        private readonly float initialHeight = 0f;
+        private readonly Color initialColour = Color.white;
         private float distance;
         public float Distance
         {
@@ -98,32 +98,33 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
             {
                 Fog = GameMain.Instance.MainCamera.GetOrAddComponent<GlobalFog>();
                 if (Fog.fogShader == null) Fog.fogShader = Shader.Find("Hidden/GlobalFog");
-                Distance = InitialDistance;
-                Density = InitialDensity;
-                HeightScale = InitialHeightScale;
-                Height = InitialHeight;
-                FogColour = InitialColour;
+                Distance = initialDistance;
+                Density = initialDensity;
+                HeightScale = initialHeightScale;
+                Height = initialHeight;
+                FogColour = initialColour;
             }
+            SetEffectActive(false);
         }
 
         public void Deactivate()
         {
-            Distance = InitialDistance;
-            Density = InitialDensity;
-            HeightScale = InitialHeightScale;
-            Height = InitialHeight;
-            FogColour = InitialColour;
+            Distance = initialDistance;
+            Density = initialDensity;
+            HeightScale = initialHeightScale;
+            Height = initialHeight;
+            FogColour = initialColour;
             Fog.enabled = false;
             Active = false;
         }
 
         public void Reset()
         {
-            Fog.startDistance = InitialDistance;
-            Fog.globalDensity = InitialDensity;
-            Fog.heightScale = InitialHeightScale;
-            Fog.height = InitialHeight;
-            Fog.globalFogColor = InitialColour;
+            Fog.startDistance = initialDistance;
+            Fog.globalDensity = initialDensity;
+            Fog.heightScale = initialHeightScale;
+            Fog.height = initialHeight;
+            Fog.globalFogColor = initialColour;
         }
 
         public void SetEffectActive(bool active)

+ 1 - 0
COM3D2.MeidoPhotoStudio.Plugin/MeidoPhotoStudio/Managers/EffectManagers/VignetteEffectManager.cs

@@ -67,6 +67,7 @@ namespace COM3D2.MeidoPhotoStudio.Plugin
                 initialBlurSpread = Vignette.blurSpread;
                 initialChromaticAberration = Vignette.chromaticAberration;
             }
+            SetEffectActive(false);
         }
 
         public void Deactivate()