using System; using System.Linq; using System.Collections.Generic; using UnityEngine; using UnityEngine.Accessibility; using UnityEngine.Rendering.Universal; using UnityEngine.SceneManagement; using UnityEngine.UIElements; namespace UnityEditor.Rendering.Universal { internal class LightBatchingDebugger : EditorWindow { private const string ResourcePath = "Packages/com.unity.render-pipelines.universal/Editor/2D/LightBatchingDebugger/"; private class LayerBatch { public List LayerNames = new List(); public List Lights = new List(); public List Shadows = new List(); public int batchId; } [MenuItem("Window/2D/Light Batching Debugger")] public static void ShowExample() { // Open Game View EditorApplication.ExecuteMenuItem("Window/General/Game"); LightBatchingDebugger wnd = GetWindow(); wnd.titleContent = new GUIContent("Light Batching Debugger"); } VisualElement root => rootVisualElement; private static Color[] batchColors = new Color[10]; private List batchList = new List(); private List selectedIndices = new List(); private ListView batchListView; private int lightCount = 0; private int shadowCount = 0; // Variables used for refresh view private bool doRefresh; private int cachedSceneHandle; private int totalLightCount; private int totalShadowCount; private Vector3 cachedCamPos; ILight2DCullResult lightCullResult { get { // Game view main camera var renderer = Camera.main?.GetUniversalAdditionalCameraData().scriptableRenderer as Renderer2D; var data = renderer?.GetRenderer2DData(); if (data != null && data.lightCullResult.IsGameView()) return data?.lightCullResult; return null; } } private bool PopulateData() { if (lightCullResult == null) return false; batchList.Clear(); var layers = Light2DManager.GetCachedSortingLayer(); var batches = LayerUtility.CalculateBatches(lightCullResult, out var batchCount); for (var i = 0; i < batchCount; i++) { var batchInfo = new LayerBatch { batchId = i }; var batch = batches[i]; // Get the lights foreach (var light in lightCullResult.visibleLights) { // If the lit layers are different, or if they are lit but this is a shadow casting light then don't batch. if (light.IsLitLayer(batch.startLayerID)) { batchInfo.Lights.Add(light); } } // Get the shadows var visibleShadows = lightCullResult.visibleShadows.SelectMany(x => x.GetShadowCasters()); foreach (var shadowCaster in visibleShadows) { if (shadowCaster.IsShadowedLayer(batch.startLayerID)) batchInfo.Shadows.Add(shadowCaster); } for (var batchIndex = batch.startIndex; batchIndex <= batch.endIndex; batchIndex++) { batchInfo.LayerNames.Add(layers[batchIndex].name); } batchList.Add(batchInfo); } return true; } private VisualElement MakePill(UnityEngine.Object obj) { var bubble = new Button(); bubble.AddToClassList("Pill"); bubble.text = obj.name; bubble.clicked += () => { Selection.activeObject = obj; }; return bubble; } private VisualElement GetInfoView() { // Hide initial prompt DisplayInitialPrompt(false); return root.Query("InfoView").First(); } private void ViewBatch(int index) { if (index >= batchList.Count()) return; var infoView = GetInfoView(); var batch1 = batchList[index]; var title = root.Query