|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Reflection; |
| 4 | +using UnityEditor; |
| 5 | +using UnityEngine; |
| 6 | +using HarmonyLib; |
| 7 | +using AmplifyShaderEditor; |
| 8 | + |
| 9 | +namespace Nessie.ASE |
| 10 | +{ |
| 11 | + [InitializeOnLoad] |
| 12 | + internal class ColorDebug |
| 13 | + { |
| 14 | + #region Private Fields |
| 15 | + |
| 16 | + private const string m_harmonyID = "Nessie.ASE.ColorDebugPatch"; |
| 17 | + |
| 18 | + private static bool m_useHotkey = true; |
| 19 | + |
| 20 | + private static Texture2D m_previewPixel; |
| 21 | + |
| 22 | + #endregion Private Fields |
| 23 | + |
| 24 | + #region Public Fields |
| 25 | + |
| 26 | + public static bool UseHotkey { get { return m_useHotkey; } set { m_useHotkey = value; } } |
| 27 | + |
| 28 | + public static Texture2D PreviewPixel |
| 29 | + { |
| 30 | + get |
| 31 | + { |
| 32 | + if (m_previewPixel == null) |
| 33 | + m_previewPixel = new Texture2D( |
| 34 | + 1, 1, |
| 35 | + UnityEngine.Experimental.Rendering.GraphicsFormat.R32G32B32A32_SFloat, |
| 36 | + UnityEngine.Experimental.Rendering.TextureCreationFlags.None); |
| 37 | + |
| 38 | + return m_previewPixel; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + #endregion Public Fields |
| 43 | + |
| 44 | + #region Injection Methods |
| 45 | + |
| 46 | + static ColorDebug() |
| 47 | + { |
| 48 | + AssemblyReloadEvents.afterAssemblyReload += PostAssemblyReload; |
| 49 | + } |
| 50 | + |
| 51 | + static void PostAssemblyReload() |
| 52 | + { |
| 53 | + Harmony harmony = new Harmony(m_harmonyID); |
| 54 | + |
| 55 | + harmony.PatchAll(); |
| 56 | + } |
| 57 | + |
| 58 | + [HarmonyPatch(typeof(ParentGraph), nameof(ParentGraph.Draw))] |
| 59 | + static class PatchDrawPreview |
| 60 | + { |
| 61 | + static void Postfix(DrawInfo drawInfo, List<ParentNode> ___m_nodes) |
| 62 | + { |
| 63 | + if (drawInfo.CurrentEventType == EventType.Repaint && (!UseHotkey || Event.current.control)) |
| 64 | + { |
| 65 | + ParentNode node = GetActiveNode(drawInfo, ___m_nodes); |
| 66 | + if (node) |
| 67 | + ShowColorTooltip(drawInfo, node); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + [HarmonyPatch(typeof(AmplifyShaderEditorWindow), nameof(AmplifyShaderEditorWindow.OnDisable))] |
| 73 | + static class PatchDisable |
| 74 | + { |
| 75 | + static void Postfix() |
| 76 | + { |
| 77 | + if (m_previewPixel == null) |
| 78 | + UnityEngine.Object.DestroyImmediate(PreviewPixel); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #endregion Injection Methods |
| 83 | + |
| 84 | + #region ASE Methods |
| 85 | + |
| 86 | + static void ShowColorTooltip(DrawInfo drawInfo, ParentNode node) |
| 87 | + { |
| 88 | + bool isTextureNode = node.GetType().IsSubclassOf(typeof(TexturePropertyNode)); |
| 89 | + bool drawPreview = isTextureNode ? ((TexturePropertyNode)node).IsValid : GetPrivateField<bool>(node, "m_drawPreview"); |
| 90 | + Rect previewRect = GetPrivateField<Rect>(node, "m_previewRect"); |
| 91 | + if (previewRect.Contains(drawInfo.MousePosition) && (node.ShowPreview || node.ContainerGraph.ParentWindow.GlobalPreview) && drawPreview) |
| 92 | + { |
| 93 | + Vector2 mousePos = drawInfo.MousePosition; |
| 94 | + Vector2 mouseRel = mousePos - new Vector2(previewRect.x, previewRect.y); |
| 95 | + |
| 96 | + // Push a single pixel from a RT into a Tex2D. |
| 97 | + RenderTexture previousRT = RenderTexture.active; |
| 98 | + RenderTexture.active = node.PreviewTexture; |
| 99 | + PreviewPixel.ReadPixels(new Rect(mouseRel.x, mouseRel.y, 1, 1), 0, 0, false); |
| 100 | + RenderTexture.active = previousRT; |
| 101 | + |
| 102 | + // Each channel is represented with a full 32-bit float. |
| 103 | + byte[] bytes = PreviewPixel.GetRawTextureData(); |
| 104 | + float colorR = BitConverter.ToSingle(bytes, 0); |
| 105 | + float colorG = BitConverter.ToSingle(bytes, 4); |
| 106 | + float colorB = BitConverter.ToSingle(bytes, 8); |
| 107 | + float colorA = BitConverter.ToSingle(bytes, 12); |
| 108 | + |
| 109 | + string[] colorValues = new string[] |
| 110 | + { |
| 111 | + $"R: {colorR.ToString()}", |
| 112 | + $"G: {colorG.ToString()}", |
| 113 | + $"B: {colorB.ToString()}", |
| 114 | + $"A: {colorA.ToString()}" |
| 115 | + }; |
| 116 | + |
| 117 | + Array previewChannels = GetPrivateField<Array>(typeof(ParentNode), node, "m_previewChannels"); |
| 118 | + int activeChannels = GetActiveChannels(node); |
| 119 | + int usedChannels = 0; |
| 120 | + string labelText = ""; |
| 121 | + for (int i = 0; i < activeChannels; i++) |
| 122 | + { |
| 123 | + if ((bool)previewChannels.GetValue(i)) |
| 124 | + labelText += usedChannels++ >= 1 ? $"\n{colorValues[i]}" : colorValues[i]; |
| 125 | + } |
| 126 | + if (usedChannels == 0) return; |
| 127 | + |
| 128 | + GUIStyle labelStyle = new GUIStyle(UIUtils.Textfield); |
| 129 | + labelStyle.padding.left += 2; |
| 130 | + labelStyle.padding.right += 2; |
| 131 | + labelStyle.padding.top += 2; |
| 132 | + labelStyle.padding.bottom += 2; |
| 133 | + Vector2 rectSize = labelStyle.CalcSize(new GUIContent(labelText)); |
| 134 | + Rect labelRect = new Rect(mousePos.x + EditorGUIUtility.singleLineHeight / 1.5f, mousePos.y + EditorGUIUtility.singleLineHeight / 1.5f, rectSize.x + 1, rectSize.y); |
| 135 | + |
| 136 | + GUI.Label(labelRect, labelText, labelStyle); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + static ParentNode GetActiveNode(DrawInfo drawInfo, List<ParentNode> nodes) |
| 141 | + { |
| 142 | + int nodeCount = nodes.Count; |
| 143 | + for (int i = nodeCount - 1; i >= 0; i--) |
| 144 | + { |
| 145 | + ParentNode node = nodes[i]; |
| 146 | + if (node.IsVisible && !node.IsMoving) |
| 147 | + { |
| 148 | + if (node.GlobalPosition.Contains(drawInfo.MousePosition)) return node; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + static int GetActiveChannels(ParentNode node) |
| 156 | + { |
| 157 | + switch (node.OutputPorts[0].DataType) |
| 158 | + { |
| 159 | + case WirePortDataType.FLOAT: |
| 160 | + return 1; |
| 161 | + case WirePortDataType.FLOAT2: |
| 162 | + return 2; |
| 163 | + case WirePortDataType.COLOR: |
| 164 | + case WirePortDataType.FLOAT4: |
| 165 | + case WirePortDataType.SAMPLER1D: |
| 166 | + case WirePortDataType.SAMPLER2D: |
| 167 | + case WirePortDataType.SAMPLER3D: |
| 168 | + case WirePortDataType.SAMPLERCUBE: |
| 169 | + case WirePortDataType.SAMPLER2DARRAY: |
| 170 | + return 4; |
| 171 | + default: |
| 172 | + return 3; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + #endregion ASE Methods |
| 177 | + |
| 178 | + #region Reflections |
| 179 | + |
| 180 | + static T GetPrivateField<T>(object obj, string fieldName) |
| 181 | + { |
| 182 | + BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; |
| 183 | + Type type = obj.GetType(); |
| 184 | + FieldInfo finfo = type.GetField(fieldName, bindingFlags); |
| 185 | + |
| 186 | + return (T)finfo.GetValue(obj); |
| 187 | + } |
| 188 | + |
| 189 | + static T GetPrivateField<T>(Type type, object obj, string fieldName) |
| 190 | + { |
| 191 | + BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic; |
| 192 | + FieldInfo finfo = type.GetField(fieldName, bindingFlags); |
| 193 | + |
| 194 | + return (T)finfo.GetValue(obj); |
| 195 | + } |
| 196 | + |
| 197 | + #endregion Reflections |
| 198 | + } |
| 199 | +} |
0 commit comments