Skip to content

Commit b74afed

Browse files
authored
[EyebrowFix] Improve Screencap compatibility (#78)
1 parent ec23245 commit b74afed

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/Core_Fix_Eyebrows/EyebrowFix.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using BepInEx;
1+
using System;
2+
using BepInEx;
23
using BepInEx.Configuration;
34
using BepInEx.Logging;
45
using ChaCustom;
@@ -9,6 +10,7 @@
910
using KKAPI.Maker;
1011
using KKAPI.Studio;
1112
using System.Linq;
13+
using Screencap;
1214
using UnityEngine;
1315
using UnityEngine.Rendering;
1416
using UnityEngine.SceneManagement;
@@ -18,6 +20,7 @@ namespace IllusionFixes
1820
{
1921
[BepInPlugin(PluginGUID, PluginName, Constants.PluginsVersion)]
2022
[BepInDependency(KoikatuAPI.GUID, KoikatuAPI.VersionConst)]
23+
[BepInDependency(ScreenshotManager.GUID, ScreenshotManager.Version)]
2124
public class EyebrowFix : BaseUnityPlugin
2225
{
2326
public const string PluginGUID = "KK_Fix_Eyebrow";
@@ -26,10 +29,10 @@ public class EyebrowFix : BaseUnityPlugin
2629

2730
public static RenderTexture rt;
2831
public static Material mat;
29-
32+
private static bool captureActive;
3033
public static ConfigEntry<bool> ConfigEnabled { get; private set; }
3134

32-
internal void Awake()
35+
private void Awake()
3336
{
3437
ConfigEnabled = Config.Bind("", "Enabled", false, "Whether the plugin is enabled. Restart the game after changing this setting.\n\nWarning: This plugin is still experiemental and not recommended for general use.");
3538
if (!ConfigEnabled.Value)
@@ -44,6 +47,9 @@ internal void Awake()
4447
SceneManager.sceneLoaded += InitStudioUI;
4548

4649
Camera.onPreCull += OnPreCull;
50+
51+
ScreenshotManager.OnPreCapture += () => captureActive = true;
52+
ScreenshotManager.OnPostCapture += () => captureActive = false;
4753
}
4854

4955
private void InitStudioUI(Scene scene, LoadSceneMode loadSceneMode)
@@ -72,35 +78,35 @@ private void InitStudioUI(Scene scene, LoadSceneMode loadSceneMode)
7278
//pre render
7379
private void Update()
7480
{
75-
if (rt != null)
76-
{
77-
RenderTexture.ReleaseTemporary(rt);
78-
rt = null;
79-
}
80-
8181
int rx;
8282
int ry;
83-
84-
if (Screencap.ScreenshotManager.KeyCaptureAlpha.Value.IsDown())
83+
if (captureActive)
8584
{
86-
rx = Screencap.ScreenshotManager.ResolutionX.Value * Screencap.ScreenshotManager.DownscalingRate.Value;
87-
ry = Screencap.ScreenshotManager.ResolutionY.Value * Screencap.ScreenshotManager.DownscalingRate.Value;
85+
rx = ScreenshotManager.ResolutionX.Value * ScreenshotManager.DownscalingRate.Value;
86+
ry = ScreenshotManager.ResolutionY.Value * ScreenshotManager.DownscalingRate.Value;
8887
}
8988
else
9089
{
9190
rx = Screen.width;
9291
ry = Screen.height;
9392
}
9493

95-
rt = RenderTexture.GetTemporary(rx, ry, 0, RenderTextureFormat.ARGBHalf);
94+
if (rt == null || rt.width != rx || rt.height != ry)
95+
{
96+
if (rt != null) RenderTexture.ReleaseTemporary(rt);
97+
rt = RenderTexture.GetTemporary(rx, ry, 0, RenderTextureFormat.ARGBHalf);
98+
}
9699
}
97100

98101
/// <summary>
99102
/// Clear RenderTexture before rendering
100103
/// </summary>
101-
static internal void OnPreCull(Camera cam)
104+
internal static void OnPreCull(Camera cam)
102105
{
103-
if (rt == null || (LayerName.CharaMask & cam.cullingMask) == 0)
106+
if ((LayerName.CharaMask & cam.cullingMask) == 0)
107+
return;
108+
109+
if (rt == null)
104110
return;
105111

106112
var rta = RenderTexture.active;
@@ -176,10 +182,12 @@ public static void SetEyebrows(ChaControl chaControl, byte value)
176182
return;
177183

178184
if (value == 0) //From config
185+
{
179186
if (Manager.Config.EtcData.ForegroundEyebrow)
180187
EnableEyebrows(chaControl);
181188
else
182189
DisableEyebrows(chaControl);
190+
}
183191
else if (value == 1) //Behind hair
184192
DisableEyebrows(chaControl);
185193
else if (value == 2) //In front of hair
@@ -208,10 +216,12 @@ public static void SetEyeliners(ChaControl chaControl, byte value)
208216
return;
209217

210218
if (value == 0) //From config
219+
{
211220
if (Manager.Config.EtcData.ForegroundEyes)
212221
EnableEyeliners(chaControl);
213222
else
214223
DisableEyeliners(chaControl);
224+
}
215225
else if (value == 1) //Behind hair
216226
DisableEyeliners(chaControl);
217227
else if (value == 2) //In front of hair

0 commit comments

Comments
 (0)