summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Editor/MB3_MeshBakerEditorWindowAddObjectsTab.cs
blob: a897a3f6b7c565d80ef6a76b11433d99fcc5133a (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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//----------------------------------------------
//            MeshBaker
// Copyright © 2011-2012 Ian Deane
//----------------------------------------------
using UnityEditor;
using UnityEngine;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using DigitalOpus.MB.Core;

namespace DigitalOpus.MB.MBEditor
{
    public class MB3_MeshBakerEditorWindowAddObjectsTab : MB3_MeshBakerEditorWindowInterface
    {
        static string[] LODLevelLabels = new string[]
        {
        "All LOD Levels", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
        };

        static int[] LODLevelValues = new int[]
        {
        -1,0,1,2,3,4,5,6,7,8,9
        };

        public MB3_MeshBakerRoot _target = null;
        public MonoBehaviour target
        {
            get { return _target; }
            set { _target = (MB3_MeshBakerRoot)value; }
        }

        GameObject targetGO = null;
        GameObject oldTargetGO = null;
        MB3_TextureBaker textureBaker;
        MB3_MeshBaker meshBaker;
        UnityEngine.Object[] targs = new UnityEngine.Object[1];
        SerializedObject serializedObject;

        GUIContent GUIContentRegExpression = new GUIContent("Matches Regular Expression", @"A valid # regular express. Examples:" + "\n\n" +
            @" ([A-Za-z0-9\-]+)(LOD1) matches one or more chars,numbers and hyphen ending with LOD1." + "\n\n" +
            @" (Grass)([A-Za-z0-9\-\(\) ]+) matches the string 'Grass' followed by characters, numbers, hyphen, brackets or space." + "\n\n");

        string helpBoxString = "";
        string regExParseError = "";
        bool onlyStaticObjects = false;
        bool onlyEnabledObjects = false;
        bool excludeMeshesWithOBuvs = true;
        bool excludeMeshesAlreadyAddedToBakers = true;
        int lodLevelToInclude = -1;
        int lightmapIndex = -2;
        string searchRegEx = "";
        Material shaderMat = null;
        Material mat = null;

        bool tbFoldout = false;
        bool mbFoldout = false;

        MB3_MeshBakerEditorInternal mbe = new MB3_MeshBakerEditorInternal();
        MB3_TextureBakerEditorInternal tbe = new MB3_TextureBakerEditorInternal();

        public void OnEnable()
        {
            if (textureBaker != null)
            {
                serializedObject = new SerializedObject(textureBaker);
                tbe.OnEnable(serializedObject);
            }
            else if (meshBaker != null)
            {
                serializedObject = new SerializedObject(meshBaker);
                mbe.OnEnable(serializedObject);
            }
        }

        public void OnDisable()
        {
            tbe.OnDisable();
            mbe.OnDisable();
        }

        public void drawTabAddObjectsToBakers()
        {
            if (helpBoxString == null) helpBoxString = "";
            EditorGUILayout.HelpBox("To add, select one or more objects in the hierarchy view. Child Game Objects with MeshRender or SkinnedMeshRenderer will be added. Use the fields below to filter what is added." +
                                    "To remove, use the fields below to filter what is removed.\n" + helpBoxString, UnityEditor.MessageType.None);
            target = (MB3_MeshBakerRoot)EditorGUILayout.ObjectField("Target to add objects to", target, typeof(MB3_MeshBakerRoot), true);

            if (target != null)
            {
                targetGO = target.gameObject;
            }
            else
            {
                targetGO = null;
            }

            if (targetGO != oldTargetGO && targetGO != null)
            {
                textureBaker = targetGO.GetComponent<MB3_TextureBaker>();
                meshBaker = targetGO.GetComponent<MB3_MeshBaker>();
                tbe = new MB3_TextureBakerEditorInternal();
                mbe = new MB3_MeshBakerEditorInternal();
                oldTargetGO = targetGO;
                if (textureBaker != null)
                {
                    serializedObject = new SerializedObject(textureBaker);
                    tbe.OnEnable(serializedObject);
                }
                else if (meshBaker != null)
                {
                    serializedObject = new SerializedObject(meshBaker);
                    mbe.OnEnable(serializedObject);
                }
            }


            EditorGUIUtility.labelWidth = 300;
            onlyStaticObjects = EditorGUILayout.Toggle("Only Static Objects", onlyStaticObjects);

            onlyEnabledObjects = EditorGUILayout.Toggle("Only Enabled Objects", onlyEnabledObjects);

            excludeMeshesWithOBuvs = EditorGUILayout.Toggle("Exclude meshes with out-of-bounds UVs", excludeMeshesWithOBuvs);

            excludeMeshesAlreadyAddedToBakers = EditorGUILayout.Toggle("Exclude GameObjects already added to bakers", excludeMeshesAlreadyAddedToBakers);

            lodLevelToInclude = EditorGUILayout.IntPopup("Only include objects on LOD Level", lodLevelToInclude, LODLevelLabels, LODLevelValues);

            mat = (Material)EditorGUILayout.ObjectField("Using Material", mat, typeof(Material), true);
            shaderMat = (Material)EditorGUILayout.ObjectField("Using Shader", shaderMat, typeof(Material), true);

            string[] lightmapDisplayValues = new string[257];
            int[] lightmapValues = new int[257];
            lightmapValues[0] = -2;
            lightmapValues[1] = -1;
            lightmapDisplayValues[0] = "don't filter on lightmapping";
            lightmapDisplayValues[1] = "not lightmapped";
            for (int i = 2; i < lightmapDisplayValues.Length; i++)
            {
                lightmapDisplayValues[i] = "" + i;
                lightmapValues[i] = i;
            }
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Using Lightmap Index ");
            lightmapIndex = EditorGUILayout.IntPopup(lightmapIndex,
                                                     lightmapDisplayValues,
                                                     lightmapValues);
            EditorGUILayout.EndHorizontal();
            if (regExParseError != null && regExParseError.Length > 0)
            {
                EditorGUILayout.HelpBox("Error In Regular Expression:\n" + regExParseError, MessageType.Error);
            }
            searchRegEx = EditorGUILayout.TextField(GUIContentRegExpression, searchRegEx);


            EditorGUILayout.Separator();

            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Add Selected Meshes To Target"))
            {
                addSelectedObjects();
            }
            if (GUILayout.Button("Remove Matching Meshes From Target"))
            {
                removeSelectedObjects();
            }
            EditorGUILayout.EndHorizontal();

            if (textureBaker != null)
            {
                MB_EditorUtil.DrawSeparator();
                tbFoldout = EditorGUILayout.Foldout(tbFoldout, "Texture Baker");
                if (tbFoldout)
                {
                    if (targs == null) targs = new UnityEngine.Object[1];
                    targs[0] = textureBaker;
                    tbe.DrawGUI(serializedObject, (MB3_TextureBaker)textureBaker, targs, typeof(MB3_MeshBakerEditorWindow));
                }

            }
            if (meshBaker != null)
            {
                MB_EditorUtil.DrawSeparator();
                mbFoldout = EditorGUILayout.Foldout(mbFoldout, "Mesh Baker");
                if (mbFoldout)
                {
                    if (targs == null) targs = new UnityEngine.Object[1];
                    targs[0] = meshBaker;
                    mbe.DrawGUI(serializedObject, (MB3_MeshBaker)meshBaker, targs, typeof(MB3_MeshBakerEditorWindow));
                }
            }
        }

        List<GameObject> GetFilteredList(bool addingObjects)
        {
            List<GameObject> newMomObjs = new List<GameObject>();
            MB3_MeshBakerRoot mom = (MB3_MeshBakerRoot)target;
            if (mom == null)
            {
                Debug.LogError("Must select a target MeshBaker");
                return newMomObjs;
            }

            GameObject dontAddMe = null;
            Renderer r = MB_Utility.GetRenderer(mom.gameObject);
            if (r != null)
            { //make sure that this MeshBaker object is not in list
                dontAddMe = r.gameObject;
            }

            MB3_MeshBakerRoot[] allBakers = GameObject.FindObjectsOfType<MB3_MeshBakerRoot>();
            HashSet<GameObject> objectsAlreadyIncludedInBakers = new HashSet<GameObject>();

            if (addingObjects)
            {
                for (int i = 0; i < allBakers.Length; i++)
                {
                    List<GameObject> objsToCombine = allBakers[i].GetObjectsToCombine();
                    for (int j = 0; j < objsToCombine.Count; j++)
                    {
                        if (objsToCombine[j] != null) objectsAlreadyIncludedInBakers.Add(objsToCombine[j]);
                    }
                }
            }

            GameObject[] gos = Selection.gameObjects;
            if (gos.Length == 0)
            {
                Debug.LogWarning("No objects selected in hierarchy view. Nothing added. Try selecting some objects.");
                return newMomObjs;
            }

            List<GameObject> mrs = new List<GameObject>();
            for (int i = 0; i < gos.Length; i++)
            {
                GameObject go = gos[i];
                Renderer[] rs = go.GetComponentsInChildren<Renderer>(true);
                for (int j = 0; j < rs.Length; j++)
                {
                    if (rs[j] is MeshRenderer || rs[j] is SkinnedMeshRenderer)
                    {
                        mrs.Add(rs[j].gameObject);
                    }
                }
            }

            newMomObjs = FilterList(mrs, objectsAlreadyIncludedInBakers, dontAddMe, addingObjects);
            return newMomObjs;
        }

        int GetLODLevelForRenderer(Renderer r)
        {
            if (r != null)
            {
                LODGroup lodGroup = r.GetComponentInParent<LODGroup>();
                if (lodGroup != null)
                {
                    LOD[] lods = lodGroup.GetLODs();
                    for (int lodIdx = 0; lodIdx < lods.Length; lodIdx++)
                    {
                        Renderer[] rs = lods[lodIdx].renderers;
                        for (int j = 0; j < rs.Length; j++)
                        {
                            if (rs[j] == r)
                            {
                                return lodIdx;
                            }
                        }
                    }
                }
            }
            return 0;
        }

        List<GameObject> FilterList(List<GameObject> mrss, 
                    HashSet<GameObject> objectsAlreadyIncludedInBakers, 
                    GameObject dontAddMe,
                    bool addingObjects)
        {
            int numInSelection = 0;
            int numStaticExcluded = 0;
            int numEnabledExcluded = 0;
            int numLightmapExcluded = 0;
            int numLodLevelExcluded = 0;
            int numOBuvExcluded = 0;
            int numMatExcluded = 0;
            int numShaderExcluded = 0;
            int numRegExExcluded = 0;
            int numAlreadyIncludedExcluded = 0;
            System.Text.RegularExpressions.Regex regex = null;
            if (searchRegEx != null && searchRegEx.Length > 0)
            {

                try
                {
                    regex = new System.Text.RegularExpressions.Regex(searchRegEx);
                    regExParseError = "";
                }
                catch (Exception ex)
                {
                    regExParseError = ex.Message;
                }
            }

            Dictionary<int, MB_Utility.MeshAnalysisResult> meshAnalysisResultsCache = new Dictionary<int, MB_Utility.MeshAnalysisResult>(); //cache results
            List<GameObject> newMomObjs = new List<GameObject>();
            for (int j = 0; j < mrss.Count; j++)
            {
                if (mrss[j] == null)
                {
                    continue;
                }
                Renderer mrs = mrss[j].GetComponent<Renderer>();
                if (mrs is MeshRenderer || mrs is SkinnedMeshRenderer)
                {
                    if (mrs.GetComponent<TextMesh>() != null)
                    {
                        continue; //don't add TextMeshes
                    }

                    numInSelection++;
                    if (!newMomObjs.Contains(mrs.gameObject))
                    {
                        bool addMe = true;
                        if (!mrs.gameObject.isStatic && onlyStaticObjects)
                        {
                            numStaticExcluded++;
                            addMe = false;
                            continue;
                        }

                        if ((!mrs.enabled || !mrs.gameObject.activeInHierarchy) && onlyEnabledObjects)
                        {
                            numEnabledExcluded++;
                            addMe = false;
                            continue;
                        }

                        if (lightmapIndex != -2)
                        {
                            if (mrs.lightmapIndex != lightmapIndex)
                            {
                                numLightmapExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        if (lodLevelToInclude == -1)
                        {
                            // not filtering on LODLevel
                        }
                        else
                        {
                            if (GetLODLevelForRenderer(mrs) != lodLevelToInclude)
                            {
                                numLodLevelExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        // only do this check when adding objects. If removing objects shouldn't do it
                        if (addingObjects &&
                            excludeMeshesAlreadyAddedToBakers && 
                            objectsAlreadyIncludedInBakers.Contains(mrs.gameObject))
                        {
                            numAlreadyIncludedExcluded++;
                            addMe = false;
                            continue;
                        }

                        Mesh mm = MB_Utility.GetMesh(mrs.gameObject);
                        if (mm != null)
                        {
                            MB_Utility.MeshAnalysisResult mar;
                            if (!meshAnalysisResultsCache.TryGetValue(mm.GetInstanceID(), out mar))
                            {
                                MB_Utility.hasOutOfBoundsUVs(mm, ref mar);
                                meshAnalysisResultsCache.Add(mm.GetInstanceID(), mar);
                            }
                            if (mar.hasOutOfBoundsUVs && excludeMeshesWithOBuvs)
                            {
                                numOBuvExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        if (shaderMat != null)
                        {
                            Material[] nMats = mrs.sharedMaterials;
                            bool usesShader = false;
                            foreach (Material nMat in nMats)
                            {
                                if (nMat != null && nMat.shader == shaderMat.shader)
                                {
                                    usesShader = true;
                                }
                            }
                            if (!usesShader)
                            {
                                numShaderExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        if (mat != null)
                        {
                            Material[] nMats = mrs.sharedMaterials;
                            bool usesMat = false;
                            foreach (Material nMat in nMats)
                            {
                                if (nMat == mat)
                                {
                                    usesMat = true;
                                }
                            }
                            if (!usesMat)
                            {
                                numMatExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        if (regex != null)
                        {
                            if (!regex.IsMatch(mrs.gameObject.name))
                            {
                                numRegExExcluded++;
                                addMe = false;
                                continue;
                            }
                        }

                        if (addMe && mrs.gameObject != dontAddMe)
                        {
                            if (!newMomObjs.Contains(mrs.gameObject))
                            {
                                newMomObjs.Add(mrs.gameObject);
                            }
                        }
                    }
                }
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //sb.AppendFormat("Total objects in selection {0}\n", numInSelection);
            //Debug.Log( "Total objects in selection " + numInSelection);
            if (numStaticExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they were not static\n", numStaticExcluded);
                Debug.Log(numStaticExcluded + " objects were excluded because they were not static\n");
            }
            if (numEnabledExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they were disabled\n", numEnabledExcluded);
                Debug.Log(numEnabledExcluded + " objects were excluded because they were disabled\n");
            }
            if (numOBuvExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they were had out of bounds uvs\n", numOBuvExcluded);
                Debug.Log(numOBuvExcluded + " objects were excluded because they had out of bounds uvs\n");
            }
            if (numLightmapExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did not match lightmap filter.\n", numLightmapExcluded);
                Debug.Log(numLightmapExcluded + " objects did not match lightmap filter.\n");
            }
            if (numLodLevelExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did not match the selected LOD level filter.\n", numLodLevelExcluded);
                Debug.Log(numLodLevelExcluded + " objects did not match LOD level filter.\n");
            }
            if (numShaderExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did not use the selected shader.\n", numShaderExcluded);
                Debug.Log(numShaderExcluded + " objects were excluded because they did not use the selected shader.\n");
            }
            if (numMatExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did not use the selected material.\n", numMatExcluded);
                Debug.Log(numMatExcluded + " objects were excluded because they did not use the selected material.\n");
            }
            if (numRegExExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did not match the regular expression.\n", numRegExExcluded);
                Debug.Log(numRegExExcluded + " objects were excluded because they did not match the regular expression.\n");
            }
            if (numAlreadyIncludedExcluded > 0)
            {
                sb.AppendFormat("   {0} objects were excluded because they did were already included in other bakers.\n", numAlreadyIncludedExcluded);
                Debug.Log(numAlreadyIncludedExcluded + " objects were excluded because they did were already included in other bakers.\n");
            }

            helpBoxString = sb.ToString();
            return newMomObjs;
        }

        void removeSelectedObjects()
        {
            MB3_MeshBakerRoot mom = (MB3_MeshBakerRoot)target;
            if (mom == null)
            {
                Debug.LogError("Must select a target MeshBaker");
                return;
            }
            List<GameObject> objsToCombine = mom.GetObjectsToCombine();
            HashSet<GameObject> objectsAlreadyIncludedInBakers = new HashSet<GameObject>();
            GameObject dontAddMe = null;
            Renderer r = MB_Utility.GetRenderer(mom.gameObject);
            if (r != null)
            { //make sure that this MeshBaker object is not in list
                dontAddMe = r.gameObject;
            }

            List<GameObject> objsSelectedMatchingFilter = GetFilteredList(addingObjects:false);
            Debug.Log("Matching filter " + objsSelectedMatchingFilter.Count);
            List<GameObject> objsToRemove = new List<GameObject>();
            for (int i = 0; i < objsSelectedMatchingFilter.Count; i++)
            {
                if (objsToCombine.Contains(objsSelectedMatchingFilter[i]))
                {
                    objsToRemove.Add(objsSelectedMatchingFilter[i]);
                }
            }

            MBVersionEditor.RegisterUndo(mom, "Remove Objects");
            for (int i = 0; i < objsToRemove.Count; i++)
            {
                objsToCombine.Remove(objsToRemove[i]);
            }

            SerializedObject so = new SerializedObject(mom);
            so.SetIsDifferentCacheDirty();
            Debug.Log("Removed " + objsToRemove.Count + " objects from " + mom.name);
            helpBoxString += String.Format("\nRemoved {0} objects from {1}", objsToRemove.Count, mom.name);
        }

        void addSelectedObjects()
        {
            MB3_MeshBakerRoot mom = (MB3_MeshBakerRoot)target;
            if (mom == null)
            {
                Debug.LogError("Must select a target MeshBaker to add objects to");
                return;
            }

            List<GameObject> newMomObjs = GetFilteredList(addingObjects:true);
            MBVersionEditor.RegisterUndo(mom, "Add Objects");
            List<GameObject> momObjs = mom.GetObjectsToCombine();
            int numAdded = 0;
            int numAlreadyInList = 0;
            for (int i = 0; i < newMomObjs.Count; i++)
            {
                if (!momObjs.Contains(newMomObjs[i]))
                {
                    momObjs.Add(newMomObjs[i]);
                    numAdded++;
                }
                else
                {
                    numAlreadyInList++;
                }
            }

            SerializedObject so = new SerializedObject(mom);
            so.SetIsDifferentCacheDirty();
            if (numAlreadyInList > 0)
            {
                Debug.Log(String.Format("Skipped adding {0} objects to Target because these objects had already been added to this Target. ", numAlreadyInList));
            }

            if (numAdded == 0)
            {
                Debug.LogWarning("Added 0 objects. Make sure some or all objects are selected in the hierarchy view. Also check ths 'Only Static Objects', 'Using Material' and 'Using Shader' settings");
            }
            else
            {
                Debug.Log(string.Format("Added {0} objects to {1}. ", numAdded, mom.name));
            }

            helpBoxString += String.Format("\nAdded {0} objects to {1}", numAdded, mom.name);
        }
    }
}