This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomPostProcessing.cs
235 lines (215 loc) · 10.2 KB
/
CustomPostProcessing.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Dawn.PostProcessing.PostProcessObjects;
using MelonLoader;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using Object = UnityEngine.Object;
using static Dawn.PostProcessing.Core;
namespace Dawn.PostProcessing
{
internal class CustomPostProcessing
{
internal static GameObject Base;
internal static void CreateProcessingObjects() // OnUIManagerInit
{
Base = new GameObject("Post-Processing");
Object.DontDestroyOnLoad(Base);
CreateCustomObjects();
}
internal static bool m_ObjectsCreated;
#region Junk for Later
internal static CustomVolume s_DarkMode; // Separate volumes as m_PostProcessVolume.weight is a factor.
internal static CustomVolume s_Saturation;
internal static CustomVolume s_Bloom;
internal static CustomVolume s_Contrast;
internal static CustomVolume s_Temperature;
internal static PostProcessProfile m_DarkMode;
internal static PostProcessProfile m_Saturation;
internal static PostProcessProfile m_Bloom;
internal static PostProcessProfile m_Contrast;
internal static PostProcessProfile m_Temperature;
internal static float m_DarknessValue // Shouldn't need a try/catch (let's hope)
{
set //Value is stabilized here to be manageable (0 -> 100)
{ // Example: 75 *- 0.0888 = -6.66 which is our intended value for 75%, doing this, 100 would be -8.88, 50 would be -4.44 all down to 0
var cleanedValue = value *- 0.0888f;
s_DarkMode.m_PostProcessProfile.GetSetting<ColorGrading>().postExposure.value = cleanedValue;
// Darkness value is intentionally set to be able to go VERY dark to be able to be used as a panic button to prevent seizures.
// This will be more compatible when I can get Post Processing not to apply to the UI (See LayerArrayCache)
}
}
internal static float m_BloomValue
{
set //Value is stabilized here to be manageable (0 -> 100)
{ // Example: 20 / 10 = 2 which is our intended value, the scale is 0-10 for clean range.
var cleanedvalue = value / 10;
s_Bloom.m_PostProcessProfile.GetSetting<Bloom>().intensity.value = cleanedvalue;
}
}
internal static float m_SaturationValue // Doesn't need to be stabilized.
{
set => s_Saturation.m_PostProcessProfile.GetSetting<ColorGrading>().saturation.value = value;
}
internal static float m_ContrastValue // Doesn't need to be stabilized, -90 -> 90 is fine, -100 would leave a white screen so we don't give people that option.
{
set => s_Contrast.m_PostProcessProfile.GetSetting<ColorGrading>().contrast.value = value;
}
internal static float m_TemperatureValue// Doesn't need to be stabilized.
{
set => s_Temperature.m_PostProcessProfile.GetSetting<ColorGrading>().temperature.value = value;
}
#endregion
private static void CreateCustomObjects() // Priority Limit: 1223 -> 1250 (For Volume ID Purposes)
{
s_DarkMode = new CustomVolume("Dark-Mode", m_DarkMode, 0.2f);
s_DarkMode.m_PostProcessVolume.priority = 1224; // Should be the Highest in this case.
m_DarkMode = null; // We unload this as we don't need it anymore, unloading any lower causes unity to force an unload. eg. Assets.cs #Region Loading
s_Saturation = new CustomVolume("Saturation", m_Saturation, 1f);
m_Saturation = null;
s_Bloom = new CustomVolume("Bloom", m_Bloom, 1f);
m_Bloom = null;
s_Contrast = new CustomVolume("Contrast", m_Contrast, 0.2f);
m_Contrast = null;
s_Temperature = new CustomVolume("Temperature", m_Temperature, 1f);
m_Temperature = null;
m_ObjectsCreated = true;
}
// private static LayerMask CachedWorldJoinMask;
internal static bool hasNativePostProcessing;
internal static IEnumerator WorldJoin()
{
hasNativePostProcessing = true;
//10 Second Timeout
for (int i = 0; i < 10; i++)
{
var PPL = MainCamera.gameObject != null ? MainCamera.gameObject.GetComponent<PostProcessLayer>() : null;
if (PPL != null)
{
yield return new WaitForSeconds(1);
continue;
}
// if (PPL != null)
// {
// CachedWorldJoinMask = PPL.volumeLayer;
// //PPL.volumeLayer = LayerMask.GetMask(m_LayerArray);
// //PPL.volumeLayer = ~LayerMask.GetMask(new[]{"UI", "UiMenu"}); // Doesn't work rn;
// Console.WriteLine($@"Old Mask: {CachedWorldJoinMask.value}");
// Console.WriteLine($@"New Mask: {PPL.volumeLayer.value}");
// return;
// }
Log("Detected: World Contains no PostProcessLayer. Adding one manually.");
hasNativePostProcessing = false;
SetupPostProcessing().Coroutine();
break;
}
}
private static PostProcessResources m_CachedResources;
internal static PostProcessResources CachedResources
{
get => m_CachedResources != null ? m_CachedResources : null;
set => m_CachedResources = value;
}
internal static IEnumerator GrabLayer()
{
//25 Second Timeout
for (int i = 0; i < 25; i++)
{
var ProcessLayer = MainCamera.gameObject != null ? MainCamera.gameObject.GetComponent<PostProcessLayer>() : null;
if (ProcessLayer == null) { yield return new WaitForSeconds(1); continue; }
var postProcessVolume = Object.FindObjectsOfType<PostProcessVolume>().FirstOrDefault(v => v.priority is < 1223 or > 1250); // My Mod Range
if (postProcessVolume == null) { yield return new WaitForSeconds(1); continue; }
foreach (var x in CustomVolume.instances)
{
x.gameObject.layer = postProcessVolume.gameObject.layer;
}
Log($"PostProcessing has been set to Layer {postProcessVolume.gameObject.layer}");
break;
}
}
private static IEnumerator SetupPostProcessing()
{
if (CachedResources != null)
{
// Log("Using Cached <PostProcessResources>"); // Likely Spammed too much
//25 Second Timeout
for (int i = 0; i < 25; i++)
{
var PPL = MainCamera.gameObject != null ? MainCamera.gameObject.AddComponent<PostProcessLayer>() : null;
if (PPL == null) { yield return new WaitForSeconds(1); continue; }
PPL.antialiasingMode = PostProcessLayer.Antialiasing.FastApproximateAntialiasing; // FXAA
var FXAA = new FastApproximateAntialiasing {fastMode = true, keepAlpha = true};
PPL.fastApproximateAntialiasing = FXAA;
PPL.volumeTrigger = MainCamera.transform;
PPL.m_ShowCustomSorter = true;
PPL.m_Resources = CachedResources;
PPL.volumeLayer = ~LayerMask.GetMask(new[]{"UI", "UiMenu"}); // This sadly doesn't work rn :/
//LayerMask.GetMask(m_LayerArray); //-1; //LayerMask.GetMask(m_LayerArray); // Gonna Try To Make this work @ Some Point
PPL.enabled = s_PostProcessing;
break;
}
}
else
{ // Resources Lookup moved to OnSceneInitialize
MelonLogger.Error("Could not find the Resources necessary to construct Post Processing in a Non-PostProcessing World!");
yield break;
}
}
private static string[] m_LayerArrayCache;
private static string[] m_LayerArray //Should Return everything except UI layers.
{
get
{
if (m_LayerArrayCache != null) return m_LayerArrayCache;
var List = new List<string>();
for (var i = 0; i < 32; i++)
{
// If the layer Doesnt Contain "UI"
if (!LayerMask.LayerToName(i).ToUpper().Contains("UI") && !string.IsNullOrWhiteSpace(LayerMask.LayerToName(i)))
{
List.Add(LayerMask.LayerToName(i));
}
}
m_LayerArrayCache = List.ToArray();
return m_LayerArrayCache;
}
}
[Flags] // Might use
private enum Layers
{
Everything = -1,
Nothing = 0,
Default = 1 << 0,
TransparentFX = 1 << 1,
IgnoreRaycast = 1 << 2,
Water = 1 << 4,
UI = 1 << 5,
Interactive = 1 << 8,
Player = 1 << 9,
PlayerLocal = 1 << 10,
Environment = 1 << 11,
UiMenu = 1 << 12,
Pickup = 1 << 13,
PickupNoEnvironment = 1 << 14,
StereoLeft = 1 << 15,
StereoRight = 1 << 16,
Walkthrough = 1 << 17,
MirrorReflection = 1 << 18,
reserved2 = 1 << 19,
reserved3 = 1 << 20,
reserved4 = 1 << 21,
user0 = 1 << 22,
user1 = 1 << 23,
user2 = 1 << 24,
user3 = 1 << 25,
user4 = 1 << 26,
user5 = 1 << 27,
user6 = 1 << 28,
user7 = 1 << 29,
user8 = 1 << 30,
user9 = 1 << 31
}
}
}