summaryrefslogtreecommitdiffstats
path: root/Third_Party/at.pimaker.ltcgi/Editor/LTCGI_Screen.cs
blob: a8df773785c3aa3184f2918c1736079b5e363c88 (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
#if UNITY_EDITOR
using System;
using UnityEditor;
using UnityEngine;
#endif

namespace pi.LTCGI
{
#if UNITY_EDITOR
    [ExecuteInEditMode]
    public class LTCGI_Screen : MonoBehaviour
    {
        public Color Color = Color.white;

        public bool DoubleSided = false;

        [Tooltip("If enabled, allows moving the screen GameObject during runtime. Has CPU performance overhead (Udon).")]
        public bool Dynamic;

        public int TextureIndex;

        [Tooltip("Specular and Diffuse are the two types of lighting that LTCGI calculates. For performance, try disabling Diffuse and see if the result is visually similar, in which case you should leave it off.")]
        public bool Diffuse = true, Specular = true;
        public bool DiffuseFromLm;
        [SerializeField] private int diffMode;

        public int LightmapChannel;
        public float LightmapIntensity = 1.0f;

        public ColorMode ColorMode;

        public Vector2 SingleUV;

        [Range(0, 3)]
        public int AudioLinkBand;

        [Tooltip("Workaround for some Blender imports. Try to enable it if you notice your reflection is sideways.")]
        public bool FlipUV;

        public RendererMode RendererMode = RendererMode.Distance;
        public MeshRenderer[] RendererList;
        [Min(0.0f)] public float RendererDistance = 15.0f;

        public bool Cylinder;
        public Vector3 CylinderBase;
        public float CylinderHeight = 1.0f;
        public float CylinderRadius = 1.0f;
        [Range(0.0f, Mathf.PI*0.5f)]
        public float CylinderSize = Mathf.PI*0.5f;
        [Range(0.0f, Mathf.PI*2.0f)]
        public float CylinderAngle;

        [Tooltip("If this renderer should affect avatars that use a supported LTCGI shader.")]
        public bool AffectAvatars = true;

        private Vector3 prevPos, prevScale, prevRot;

        private bool update = false;

        private static readonly Color[] GIZMO_COLORS = new Color[]
        {
            Color.white,
            Color.red,
            Color.green,
            Color.blue,
        };

        public void Update()
        {
            // don't mess with Udon emulation
            if (EditorApplication.isPlaying)
                return;
            
            if (this.prevPos != this.transform.position ||
                this.prevRot != this.transform.rotation.eulerAngles ||
                this.prevScale != this.transform.lossyScale)
            {
                this.prevPos = this.transform.position;
                this.prevRot = this.transform.rotation.eulerAngles;
                this.prevScale = this.transform.lossyScale;
                update = true;
            }
            if (update && LTCGI_Controller.Singleton != null)
            {
                LTCGI_Controller.Singleton.UpdateMaterials(true, this);
                update = false;
            }
        }

        void OnDrawGizmos()
        {
            Gizmos.color = GIZMO_COLORS[this.LightmapChannel];
            Gizmos.DrawIcon(transform.position, "LTCGI_Screen_Gizmo.png", true, Gizmos.color);
        }

        private static Mesh cylMesh = null;
        void OnDrawGizmosSelected()
        {
            if (RendererMode == RendererMode.Distance)
            {
                Gizmos.color = Color.cyan;
                Gizmos.DrawWireSphere(transform.position, RendererDistance);
            }

            if (Cylinder)
            {
                if (cylMesh == null)
                {
                    cylMesh = Resources.GetBuiltinResource<Mesh>("Cylinder.fbx");
                }

                Gizmos.color = new Color(0, 1, 1, 0.2f);
                Gizmos.DrawMesh(cylMesh, 0,
                    transform.position + CylinderBase - Vector3.up * 0.5f + Vector3.up * CylinderHeight,
                    Quaternion.AngleAxis(Mathf.Rad2Deg * CylinderAngle, Vector3.up),
                    new Vector3(CylinderRadius, CylinderHeight, CylinderRadius) * 1.01f);
            }
        }
    }

    public enum ColorMode
    {
        Static = 0,
        Texture = 1,
        SingleUV = 2,
        AudioLink = 3,
    }

    public enum RendererMode
    {
        All = 0,
        ExcludeListed = 1,
        OnlyListed = 2,
        Distance = 3,
    }

    [CustomEditor(typeof(LTCGI_Screen))]
    [CanEditMultipleObjects]
    public class LTCGI_ScreenEditor : Editor
    {
        protected SerializedProperty colorProp, sidedProp, dynamicProp, indexProp, colormodeProp, specProp, diffProp, lmProp, singleUVProp, rendererModeProp, rendererListProp, rendererDistProp, diffModeProp, diffuseFromLmProp, flipProp, lmIntensProp, alBandProp, affectAvatarsProp;
        protected SerializedProperty cylProp, cylBaseProp, cylHeightProp, cylAngleProp, cylRadiusProp, cylSizeProp;

        protected static Texture Logo;

        protected enum LMChannel
        {
            Off = 0,
            Red = 1,
            Green = 2,
            Blue = 3,
        }

        protected enum DiffMode
        {
            NoDiffuse = 0,
            LTCDiffuse = 1,
            LightmapDiffuse = 2,
        }

        void OnEnable()
        {
            colorProp = serializedObject.FindProperty("Color");
            sidedProp = serializedObject.FindProperty("DoubleSided");
            dynamicProp = serializedObject.FindProperty("Dynamic");
            indexProp = serializedObject.FindProperty("TextureIndex");
            colormodeProp = serializedObject.FindProperty("ColorMode");
            specProp = serializedObject.FindProperty("Specular");
            diffProp = serializedObject.FindProperty("Diffuse");
            lmProp = serializedObject.FindProperty("LightmapChannel");
            singleUVProp = serializedObject.FindProperty("SingleUV");
            rendererModeProp = serializedObject.FindProperty("RendererMode");
            rendererListProp = serializedObject.FindProperty("RendererList");
            rendererDistProp = serializedObject.FindProperty("RendererDistance");
            diffModeProp = serializedObject.FindProperty("diffMode");
            diffuseFromLmProp = serializedObject.FindProperty("DiffuseFromLm");
            lmIntensProp = serializedObject.FindProperty("LightmapIntensity");
            flipProp = serializedObject.FindProperty("FlipUV");
            alBandProp = serializedObject.FindProperty("AudioLinkBand");
            affectAvatarsProp = serializedObject.FindProperty("AffectAvatars");

            cylProp = serializedObject.FindProperty("Cylinder");
            cylAngleProp = serializedObject.FindProperty("CylinderAngle");
            cylBaseProp = serializedObject.FindProperty("CylinderBase");
            cylHeightProp = serializedObject.FindProperty("CylinderHeight");
            cylRadiusProp = serializedObject.FindProperty("CylinderRadius");
            cylSizeProp = serializedObject.FindProperty("CylinderSize");

            Logo = Resources.Load("LTCGI-Logo") as Texture;
        }

        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 screen = (LTCGI_Screen)target;

            if (!screen.enabled || !screen.gameObject.activeInHierarchy)
            {
                EditorGUILayout.Space();
                EditorGUILayout.HelpBox("This component is disabled, or the GameObject not active! This will cause it to not bake a lightmap, and can cause issues at runtime. If you want this renderer to start disabled, set it's color to Black (0,0,0) or visit https://ltcgi.dev/ to see how you can toggle LTCGI globally.", MessageType.Error, true);
                LTCGIDocsHelper.DrawHelpButton("https://ltcgi.dev/Getting%20Started/Setup/Basic_Toggle", "LTCGI Toggle");
                EditorGUILayout.Space();
            }

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

            serializedObject.Update();

            GUILayout.Label("Area light shape:");
            var isCylinder = cylProp.boolValue;
            using (var hor = new EditorGUILayout.HorizontalScope())
            {
                var leftStyle = EditorStyles.miniButtonLeft;
                var midStyle = EditorStyles.miniButtonMid;
                var rightStyle = EditorStyles.miniButtonRight;
                var toggleMesh = GUILayout.Toggle(!isCylinder, "Quad/Triangle", leftStyle);
                var toggleCyl = GUILayout.Toggle(isCylinder, "Cylinder", rightStyle);

                if ((toggleMesh && isCylinder) || (!toggleMesh && !isCylinder))
                {
                    isCylinder = cylProp.boolValue = !toggleMesh;
                }
                else if ((toggleCyl && !isCylinder) || (!toggleCyl && isCylinder))
                {
                    isCylinder = cylProp.boolValue = toggleCyl;
                }
            }
            EditorGUILayout.Space();
            EditorGUILayout.Space();

            if (!isCylinder)
            {
                var mesh = screen.gameObject.GetComponent<MeshFilter>()?.sharedMesh;
                string error = "";
                if (mesh == null)
                {
                    error = "No mesh or mesh filter assigned!";
                }
                else if (!mesh.isReadable)
                {
                    error = "Mesh is not readable!";
                }
                else if (mesh.vertexCount != 4 && mesh.vertexCount != 3)
                {
                    error = "Mesh does not have exactly 3 or 4 vertices!";
                }
                if (error != "")
                {
                    EditorGUILayout.Space();
                    EditorGUILayout.HelpBox(error, MessageType.Error, true);
                    EditorGUILayout.Space();
                }
            }

            if (isCylinder)
            {
                EditorGUILayout.PropertyField(cylBaseProp);
                EditorGUILayout.PropertyField(cylHeightProp);
                EditorGUILayout.PropertyField(cylRadiusProp);
                EditorGUILayout.PropertyField(cylAngleProp);
                EditorGUILayout.PropertyField(cylSizeProp);
                if (GUILayout.Button("Try cylinder auto-detect"))
                {
                    var coll = screen.gameObject.GetComponent<Collider>();
                    if (coll != null)
                    {
                        cylBaseProp.vector3Value = new Vector3(0, -coll.bounds.extents.y / 2.0f, 0);
                        cylHeightProp.floatValue = coll.bounds.extents.y;
                        cylRadiusProp.floatValue = Mathf.Max(coll.bounds.extents.x, coll.bounds.extents.z);
                    }
                    else
                    {
                        cylHeightProp.floatValue = screen.transform.localScale.y;
                        cylRadiusProp.floatValue = screen.transform.localScale.x / 2.0f;
                    }
                }

                EditorGUILayout.Separator();
            }

            DrawColorSelector(screen);

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

            var dm = (DiffMode)EditorGUILayout.EnumPopup("Diffuse Mode", (DiffMode)diffModeProp.intValue);
            if (diffModeProp.intValue != (int)dm)
            {
                diffModeProp.intValue = (int)dm;
                diffProp.boolValue = dm != DiffMode.NoDiffuse;
                diffuseFromLmProp.boolValue = dm == DiffMode.LightmapDiffuse;
            }

            if (diffuseFromLmProp.boolValue && lmProp.intValue == 0)
            {
                EditorGUILayout.HelpBox("You have \"Lightmap Diffuse\" enabled but no lightmap channel selected - this is probably not what you want.", MessageType.Warning, true);
            }

            EditorGUILayout.PropertyField(specProp);
            EditorGUILayout.PropertyField(dynamicProp);
            if (isCylinder)
            {
                sidedProp.boolValue = false;
            }
            else
            {
                EditorGUILayout.PropertyField(sidedProp);
            }
            EditorGUILayout.PropertyField(flipProp);
            EditorGUILayout.PropertyField(affectAvatarsProp);

            EditorGUILayout.Separator();
            DrawColorModeSelector(true);
            EditorGUILayout.Separator();
            DrawRendererModeSelector();
            EditorGUILayout.Separator();
            DrawLmChannelSelector();

            if (serializedObject.hasModifiedProperties)
            {
                serializedObject.ApplyModifiedProperties();
                LTCGI_Controller.Singleton?.UpdateMaterials();
            }
        }

        protected void DrawColorSelector(LTCGI_Screen screen)
        {
            var newCol = EditorGUILayout.ColorField(new GUIContent("Color"), colorProp.colorValue, true, false, true);
            if (colorProp.colorValue != newCol)
            {
                colorProp.colorValue = newCol;
            }

            if (colorProp.colorValue.maxColorComponent == 0.0f)
            {
                EditorGUILayout.HelpBox("Screen is disabled with color black!", MessageType.Warning, true);
            }
            
            if (GUILayout.Button("Try get Color from Material"))
            {
                var ren = screen.gameObject.GetComponent<Renderer>();
                if (ren != null)
                {
                    var mat = ren.sharedMaterial;
                    if (mat != null)
                    {
                        var col = mat.GetColor("_Color");
                        if (col != null)
                        {
                            colorProp.colorValue = col;
                        }
                    }
                }
            }
        }

        protected void DrawColorModeSelector(bool allowTextured)
        {
            var texmode = (ColorMode)EditorGUILayout.EnumPopup("Color Mode", (ColorMode)colormodeProp.intValue);
            if (colormodeProp.intValue != (int)texmode)
            {
                colormodeProp.intValue = (int)texmode;
            }
            if (targets.Length == 1)
            {
                Action texSelect = () =>
                {
                    EditorGUILayout.IntSlider(indexProp, 0,
                        LTCGI_Controller.Singleton == null && LTCGI_Controller.Singleton.StaticTextures != null ? 2 :
                            LTCGI_Controller.Singleton.StaticTextures.Length);

                    if (LTCGI_Controller.Singleton != null)
                    {
                        if (indexProp.intValue == 0)
                        {
                            EditorGUILayout.HelpBox("Texture: [Live Video]", MessageType.None, false);
                        }
                        else
                        {
                            EditorGUILayout.HelpBox("Texture: " + LTCGI_Controller.Singleton.StaticTextures[indexProp.intValue - 1].name, MessageType.None, false);
                        }
                    }
                };
                switch (texmode)
                {
                    case ColorMode.Static:
                        indexProp.intValue = 0;
                        break;
                    case ColorMode.Texture:
                        if (allowTextured)
                        {
                            texSelect();
                        }
                        else
                        {
                            EditorGUILayout.HelpBox("Texture mode is not allowed for emitters! Falling back to static color.", MessageType.Error, true);
                        }
                        break;
                    case ColorMode.SingleUV:
                        EditorGUILayout.HelpBox("This will always use LOD0 of the video texture as its input.", MessageType.Info);
                        singleUVProp.vector2Value = EditorGUILayout.Vector2Field("Texture UV", singleUVProp.vector2Value);
                        break;
                    case ColorMode.AudioLink:
                        EditorGUILayout.PropertyField(alBandProp);
                        string[] bandNames = new[] {"Bass", "Low Mids", "High Mids", "Treble"};
                        EditorGUILayout.HelpBox($"Selected Band: {bandNames[alBandProp.intValue]}", MessageType.None, false);
                        break;
                }
            }
            else
            {
                EditorGUILayout.HelpBox("(cannot multi-edit 'Color Mode' settings)", MessageType.None, false);
            }
        }

        protected void DrawRendererModeSelector()
        {
            var rendererMode = (RendererMode)EditorGUILayout.EnumPopup("Affected Renderers", (RendererMode)rendererModeProp.intValue);
            if (rendererModeProp.intValue != (int)rendererMode)
            {
                rendererModeProp.intValue = (int)rendererMode;
            }
            if (rendererMode != RendererMode.All && rendererMode != RendererMode.Distance)
            {
                EditorGUILayout.PropertyField(rendererListProp, new GUIContent("Renderer List"), true);
            }
            else if (rendererMode == RendererMode.Distance)
            {
                EditorGUILayout.PropertyField(rendererDistProp);
            }
        }

        protected void DrawLmChannelSelector()
        {
            var lmch = (LMChannel)EditorGUILayout.EnumPopup("Lightmap Channel", (LMChannel)lmProp.intValue);
            if (lmProp.intValue != (int)lmch)
            {
                lmProp.intValue = (int)lmch;
            }

            if (lmch != LMChannel.Off)
            {
                EditorGUILayout.PropertyField(lmIntensProp);
            }
        }
    }
#endif
}