summaryrefslogtreecommitdiffstats
path: root/Third_Party/at.pimaker.ltcgi/Editor/LTCGI_ControllerExternal.cs
blob: ce9e07d7f1dc09159369283719bbef3fe03fc339 (plain)
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#if UNITY_EDITOR
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEditor.Build.Reporting;
#if VRC_SDK_VRCSDK2 || UDONSHARP
using VRC.SDKBase.Editor.BuildPipeline;
#endif
#endif

namespace pi.LTCGI
{
    #if UNITY_EDITOR
    [CustomEditor(typeof(LTCGI_Controller))]
    public class LTCGI_ControllerEditor : Editor
    {
        const string VERSION = "v1.0.2";

        private static readonly string[] CONFIGURATION_PROPS = new[] {
            "StaticTextures",
            "DynamicRenderers",
            "CustomBlurChain",
            "LightmapIntensity",
            "LightmapMultiplier"
        };

        private static Texture Logo;

        private bool debugFoldout = false;

        private static string configPath;

        private static Dictionary<string, object> configChangedValues = new Dictionary<string, object>();

        private enum ConfigType
        {
            Boolean,
            Float,
        }

        public void OnEnable()
        {
            Logo = Resources.Load("LTCGI-LogoController") as Texture;

            if (configChangedValues != null)
            {
                configChangedValues.Clear();
            }
        }

        private bool _configChanged = false;

        public override void OnInspectorGUI()
        {
            GUIStyle style = new GUIStyle(EditorStyles.label);
            style.alignment = TextAnchor.MiddleCenter;
            style.fixedHeight = 150;
            GUI.Box(GUILayoutUtility.GetRect(300, 150, style), Logo, style);

            var rightAlignedLabel = new GUIStyle(EditorStyles.label);
            rightAlignedLabel.alignment = TextAnchor.MiddleRight;
            GUILayout.Label(VERSION, rightAlignedLabel);


            bool applyButtonPressed = false;
            bool revertButtonPressed = false;

            var resetCol = GUI.backgroundColor;

            LTCGIDocsHelper.DrawHelpButton("https://ltcgi.dev/Getting%20Started/Setup/Controller");

            if (PrefabUtility.IsPartOfPrefabAsset(target))
            {
                var large = new GUIStyle(EditorStyles.wordWrappedLabel);
                large.fontSize = 32;
                GUILayout.Label("Please put exactly 1 instance of this prefab into your scene!", large);
                return;
            }

            #if !UDONSHARP
            if (LTCGI_Controller.RuntimeMode == LTCGI_Controller.LTCGIRuntimeMode.VRChatWorld)
            {
                const string msg = "LTCGI for VRChat requires at least UdonSharp 1.0 to be installed in your project!";
                Debug.LogError(msg);
                EditorGUILayout.HelpBox(msg, MessageType.Error);
            }
            #endif

            serializedObject.Update();

            if (GUILayout.Button("Force Update"))
            {
                LTCGI_Controller.Singleton.UpdateMaterials();
            }

            if (GUILayout.Button("Precompute Static Textures"))
            {
                LTCGI_Controller.Singleton.CreateLODTextureArrays();
            }
            EditorGUILayout.PropertyField(serializedObject.FindProperty("PrecomputeOnBuild"));

            EditorGUILayout.Separator();

            LTCGIDocsHelper.DrawHelpButton("https://ltcgi.dev/Advanced/Shadowmaps", "Shadowmap Baking");

            if (!LTCGI_Controller.Singleton.bakeInProgress && GUILayout.Button("Bake Shadowmap"))
            {
                LTCGI_Controller.BakeLightmap();
            }
            else if (!LTCGI_Controller.Singleton.bakeInProgress && GUILayout.Button("Bake Shadowmap and Normal Lightmap"))
            {
                LTCGI_Controller.BakeLightmapFollowup();
            }
            if (LTCGI_Controller.Singleton.bakeInProgress && GUILayout.Button("Bake Shadowmap - FORCE FINISH"))
            {
                LTCGI_Controller.Singleton.BakeComplete();
            }

            GUI.backgroundColor = Color.red;
            if (LTCGI_Controller.Singleton.bakeMaterialReset_key != null && GUILayout.Button("DEBUG: Force Settings Reset after Bake"))
            {
                LTCGI_Controller.Singleton.ResetConfiguration();
            }
            
            if (!LTCGI_Controller.Singleton.bakeInProgress && LTCGI_Controller.Singleton.HasLightmapData() &&
                GUILayout.Button("Clear Baked Data"))
            {
                LTCGI_Controller.Singleton.ResetConfiguration();
                LTCGI_Controller.Singleton.ClearLightmapData();
                LTCGI_Controller.Singleton.UpdateMaterials();
            }
            GUI.backgroundColor = resetCol;

            EditorGUILayout.Space(); EditorGUILayout.Space();

            LTCGI_Controller.DrawAutoSetupEditor(LTCGI_Controller.Singleton);

            EditorGUILayout.Space(); EditorGUILayout.Space();

            if (LTCGI_Controller.Singleton.cachedMeshRenderers != null && LTCGI_Controller.Singleton._LTCGI_ScreenTransforms != null)
            {
                    EditorGUILayout.HelpBox(
$@"Affected Renderers Total: {LTCGI_Controller.Singleton.cachedMeshRenderers.Length}
LTCGI_Screen Components: {LTCGI_Controller.Singleton._LTCGI_ScreenTransforms.Count(x => x != null)} / {LTCGI_Controller.MAX_SOURCES}
AudioLink: {(LTCGI_Controller.AudioLinkAvailable == LTCGI_Controller.AudioLinkAvailability.Unavailable ? "Not Detected" : (LTCGI_Controller.AudioLinkAvailable == LTCGI_Controller.AudioLinkAvailability.AvailableAsset ? "Detected (Asset)" : "Detected (Package)"))}",
                    MessageType.Info, true
                );

                if (LTCGI_Controller.AudioLinkAvailable == LTCGI_Controller.AudioLinkAvailability.Unavailable)
                {
                    if (GUILayout.Button("Re-Detect AudioLink"))
                    {
                        LTCGI_Controller.audioLinkAvailable = LTCGI_Controller.AudioLinkAvailability.NeedsCheck;
                        var _ignored = LTCGI_Controller.AudioLinkAvailable;
                    }
                }
            }
            else
            {
                EditorGUILayout.HelpBox("Hit \"Force Update\" or CTRL-S to calculate info!", MessageType.Info);
            }

            EditorGUILayout.Space(); EditorGUILayout.Space();
            var header = new GUIStyle(EditorStyles.boldLabel);
            header.fontSize += 4;
            GUILayout.Label("LTCGI Configuration", header);
            EditorGUILayout.Space();

            var vidTex = serializedObject.FindProperty("VideoTexture");
            EditorGUILayout.PropertyField(vidTex, true);
            if (vidTex.objectReferenceValue == null)
            {
                EditorGUILayout.HelpBox("Video Texture is not set! This means video player will not reflect their screen. use Auto-Configure options above or refer to documentation on how to set this up if required.", MessageType.Warning);
            }

            foreach (var prop in CONFIGURATION_PROPS)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty(prop), true);
            }

            // multiplier clamp, negative light go brrr
            var lmm = serializedObject.FindProperty("LightmapMultiplier");
            lmm.vector3Value = new Vector3(Mathf.Max(0.0f, lmm.vector3Value.x), Mathf.Max(0.0f, lmm.vector3Value.y), Mathf.Max(0.0f, lmm.vector3Value.z));

            EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space();
            GUILayout.Label("Global Shader Options", header);
            EditorGUILayout.Space();

            if (_configChanged)
            {
                using (new GUILayout.HorizontalScope())
                {
                    var bigButton = new GUIStyle(GUI.skin.button);
                    bigButton.fixedHeight = 40.0f;
                    bigButton.fontStyle = FontStyle.Bold;
                    bigButton.fontSize = 18;
                    bigButton.normal.textColor = Color.white;
                    bigButton.hover.textColor = Color.white;
                    resetCol = GUI.backgroundColor;
                    GUI.backgroundColor = Color.red;
                    if (GUILayout.Button("Apply", bigButton))
                    {
                        applyButtonPressed = true;
                    }
                    GUI.backgroundColor = Color.blue;
                    if (GUILayout.Button("Revert", bigButton))
                    {
                        revertButtonPressed = true;
                    }
                    GUI.backgroundColor = resetCol;
                }
            }

            RecalculateAutoConfig(target as LTCGI_Controller);

            var config = File.ReadAllLines(configPath);
            var description = "";
            var resetDesc = false;
            for (int i = 0; i < config.Length; i++)
            {
                string lineRaw = config[i];
                var line = lineRaw.Trim();
                if (string.IsNullOrEmpty(line)) continue;

                if (line.StartsWith("///"))
                {
                    if (resetDesc)
                    {
                        description = "";
                        resetDesc = false;
                    }
                    else if (!string.IsNullOrEmpty(description))
                    {
                        description += Environment.NewLine;
                    }
                    description += line.Substring(3).Trim();
                }
                else if (description != "" && (line.StartsWith("//#define") || line.StartsWith("#define")))
                {
                    var name = line.Substring(line.IndexOf(' ') + 1);
                    var nextSpace = name.IndexOf(' ');
                    if (nextSpace < 0) nextSpace = name.Length;
                    name = name.Substring(0, nextSpace);

                    object ccvValue;
                    var existsInCcv = configChangedValues.TryGetValue(name, out ccvValue);

                    var debug = description.StartsWith("[DEBUG]");
                    description = description.Replace(Environment.NewLine, " ");

                    var type = line.Count(char.IsWhiteSpace) > 1 ? ConfigType.Float : ConfigType.Boolean;

                    if (type == ConfigType.Boolean)
                    {
                        var enabledInConfig = line.StartsWith("#");
                        var enabled = (existsInCcv && (bool)ccvValue) || (!existsInCcv && enabledInConfig);

                        var toggleStyle = new GUIStyle(GUI.skin.toggle);
                        if (debug)
                        {
                            toggleStyle.normal.textColor = Color.gray;
                            toggleStyle.hover.textColor = Color.gray;
                        }
                        var set = GUILayout.Toggle(enabled, name, toggleStyle);

                        EditorGUILayout.HelpBox(description, MessageType.None, true);
                        EditorGUILayout.Space();

                        if (set != enabledInConfig)
                        {
                            configChangedValues[name] = set;
                            if (set)
                            {
                                config[i] = config[i].Substring(2);
                            }
                            else
                            {
                                config[i] = "//" + config[i];
                            }
                        }
                        else
                        {
                            configChangedValues.Remove(name);
                        }
                    }
                    else if (type == ConfigType.Float)
                    {
                        var valueInConfig = float.Parse(line.Substring(line.LastIndexOf(' ')).Replace('f', ' '), System.Globalization.CultureInfo.InvariantCulture);
                        var value = existsInCcv ? (float)ccvValue : valueInConfig;

                        var labelStyle = new GUIStyle(GUI.skin.label);
                        if (debug)
                        {
                            labelStyle.normal.textColor = Color.gray;
                            labelStyle.hover.textColor = Color.gray;
                        }
                        float set;
                        using (new GUILayout.HorizontalScope())
                        {
                            GUILayout.Label(name, labelStyle);
                            set = EditorGUILayout.FloatField(value);
                        }

                        EditorGUILayout.HelpBox(description, MessageType.None, true);
                        EditorGUILayout.Space();

                        if (set != valueInConfig)
                        {
                            configChangedValues[name] = set;
                            config[i] = $"#define {name} {set}";
                        }
                        else
                        {
                            configChangedValues.Remove(name);
                        }
                    }

                    resetDesc = true;
                }
            }

            _configChanged = configChangedValues.Count > 0;

            if (applyButtonPressed)
            {
                File.WriteAllLines(configPath, config);
                AssetDatabase.Refresh();
                configChangedValues = new Dictionary<string, object>();
            }
            else if (revertButtonPressed)
            {
                configChangedValues = new Dictionary<string, object>();
            }

            EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space();
            if ((debugFoldout = EditorGUILayout.Foldout(debugFoldout, "[ Debug Menu (Default Inspector) ]")))
            {
                DrawDefaultInspector();
            }

            var update = serializedObject.hasModifiedProperties;
            serializedObject.ApplyModifiedProperties();

            if (update)
            {
                LTCGI_Controller.Singleton.UpdateMaterials();
            }
        }
        
        public static void RecalculateAutoConfig(LTCGI_Controller controller)
        {
            if (configPath == null || !File.Exists(configPath))
            {
                configPath = AssetDatabase.GUIDToAssetPath("01c8aa443e7001b45b28cfe65d1c6786");
                if (string.IsNullOrEmpty(configPath))
                {
                    Debug.LogError("LTCGI: Could not find config file! Please don't change the GUID or move the meta file!");
                    return;
                }
            }

            var config = File.ReadAllLines(configPath);
            var changed = false;
            for (int i = 0; i < config.Length; i++)
            {
                string lineRaw = config[i];
                var line = lineRaw.Trim();
                if (string.IsNullOrEmpty(line)) continue;

                if (line.EndsWith("#define LTCGI_AUDIOLINK"))
                {
                    var enabledInConfig = !line.StartsWith("//");
                    var available = LTCGI_Controller.AudioLinkAvailable != LTCGI_Controller.AudioLinkAvailability.Unavailable;
                    if (enabledInConfig != available)
                    {
                        config[i] = (available ? "" : "//") + "#define LTCGI_AUDIOLINK";
                        changed = true;
                    }
                }
                if (line.StartsWith("#include")) // cursed, but audiolink is the only include for now
                {
                    var newConfig = (LTCGI_Controller.AudioLinkAvailable != LTCGI_Controller.AudioLinkAvailability.Unavailable ? (
                        LTCGI_Controller.AudioLinkAvailable == LTCGI_Controller.AudioLinkAvailability.AvailableAsset ?
                        "#include \"Assets/AudioLink/Shaders/AudioLink.cginc\"" :
                        "#include \"Packages/com.llealloo.audiolink/Runtime/Shaders/AudioLink.cginc\""
                    ) : "#include \"not-available\"");
                    if (config[i] != newConfig)
                    {
                        config[i] = newConfig;
                        changed = true;
                    }
                }

                if (controller != null)
                {
                    if (line.EndsWith("#define LTCGI_STATIC_UNIFORMS"))
                    {
                        var enabledInConfig = !line.StartsWith("//");
                        var available = !controller.HasDynamicScreens;
                        if (enabledInConfig != available)
                        {
                            config[i] = (available ? "" : "//") + "#define LTCGI_STATIC_UNIFORMS";
                            changed = true;
                        }
                    }

                    if (line.EndsWith("#define LTCGI_CYLINDER"))
                    {
                        var enabledInConfig = !line.StartsWith("//");
                        var available = controller.HasCylinders;
                        if (enabledInConfig != available)
                        {
                            config[i] = (available ? "" : "//") + "#define LTCGI_CYLINDER";
                            changed = true;
                        }
                    }
                }

                if (line.EndsWith("#define LTCGI_AVATAR_MODE"))
                {
                    var enabledInConfig = !line.StartsWith("//");
                    var enabledByProject = LTCGI_Controller.RuntimeMode == LTCGI_Controller.LTCGIRuntimeMode.VRChatAvatar;
                    if (enabledInConfig != enabledByProject)
                    {
                        config[i] = (enabledByProject ? "" : "//") + "#define LTCGI_AVATAR_MODE";
                        changed = true;
                    }
                }
            }
            if (changed)
            {
                File.WriteAllLines(configPath, config);
                AssetDatabase.Refresh();
            }
        }
    }

    public static class LTCGIDocsHelper
    {
        public static void DrawHelpButton(string url, string name = "Help &")
        {
            EditorGUILayout.Space();
            var bigButton = new GUIStyle(GUI.skin.button);
            bigButton.fixedHeight = 30.0f;
            bigButton.fontStyle = FontStyle.Bold;
            bigButton.fontSize = 14;
            bigButton.normal.textColor = Color.white;
            bigButton.hover.textColor = Color.white;
            var resetCol = GUI.backgroundColor;
            GUI.backgroundColor = Color.blue;
            if (GUILayout.Button($"Open {name} Documentation", bigButton))
            {
                System.Diagnostics.Process.Start(url);
            }
            GUI.backgroundColor = resetCol;
            EditorGUILayout.Space();
        }
    }

    // automatic callbacks
    public class ShaderPostprocessLTCGI : AssetPostprocessor
    {
        static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
        {
            if (LTCGI_Controller.Singleton != null)
            {
                EditorApplication.delayCall += LTCGI_Controller.Singleton.UpdateMaterials;
            }
        }
    }

    #if UDONSHARP
    public class VRCSDKHookLTCGI : IVRCSDKBuildRequestedCallback
    {
        public int callbackOrder => 68;

        public bool OnBuildRequested(VRCSDKRequestedBuildType requestedBuildType)
        {
            if (LTCGI_Controller.Singleton != null)
            {
                LTCGI_Controller.Singleton.UpdateMaterials();
                if (LTCGI_Controller.Singleton.PrecomputeOnBuild)
                {
                    LTCGI_Controller.Singleton.CreateLODTextureArrays();
                }
            }
            return true;
        }
    }
    #else
    public class PostBuildCallbackLTCGI : UnityEditor.Build.IPreprocessBuildWithReport
    {
        public int callbackOrder => 68;

        public void OnPreprocessBuild(BuildReport report)
        {
            if (LTCGI_Controller.Singleton != null)
            {
                LTCGI_Controller.Singleton.UpdateMaterials();
                if (LTCGI_Controller.Singleton.PrecomputeOnBuild)
                {
                    LTCGI_Controller.Singleton.CreateLODTextureArrays();
                }
            }
        }
    }
    #endif
    #endif
}