From 60938c4d95e1e5978c296dc86a9d6943f85d2f05 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 26 Feb 2025 23:39:50 +0800 Subject: [PATCH 1/3] stash rework draw text --- Runtime/DebugX.cs | 41 ++- Runtime/Gizmos/DebugX.text.cs | 321 ++++++++--------------- Runtime/Gizmos/DebugX.text.cs.meta | 2 + Runtime/Utils/DebugXTextSettings.cs | 41 +++ Runtime/Utils/DebugXTextSettings.cs.meta | 2 + Samples/Sample.unity | 2 +- Samples/Scripts/DebugXSample_Other.cs | 6 +- 7 files changed, 190 insertions(+), 225 deletions(-) create mode 100644 Runtime/Gizmos/DebugX.text.cs.meta create mode 100644 Runtime/Utils/DebugXTextSettings.cs create mode 100644 Runtime/Utils/DebugXTextSettings.cs.meta diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 891e2ad..aaba190 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -24,19 +24,25 @@ namespace DCFApixels { private static PauseStateX _pauseState = PauseStateX.Unpaused; private static bool _isCameraContext = false; - private static ulong _lastEditorTicks = 1000; private static double _lastUnityTime; private static float _deltaTime = 0; private static ulong _editorTicks = 0; + private static ulong _lastEditorToRenderTicks = 1000; private static ulong _renderTicks = 100; + //private static ulong _lastEditorToRenderGizmosTicks = 1000; + //private static ulong _renderGizmosTicks = 100; private static ulong _timeTicks = 0; public static ulong RenderTicks { get { return _renderTicks; } } + //public static ulong RenderGizmosTicks + //{ + // get { return _renderGizmosTicks; } + //} public static ulong TimeTicks { get { return _timeTicks; } @@ -291,14 +297,16 @@ namespace DCFApixels private static void PostRender_General(ICommandBufferExecutor cbExecutor, Camera camera) { - if (_lastEditorTicks != _editorTicks) + if (_lastEditorToRenderTicks != _editorTicks) { _renderTicks++; - _lastEditorTicks = _editorTicks; + _lastEditorToRenderTicks = _editorTicks; } if (DebugXUtility.IsGizmosRender()) { + CallDrawGizmos(camera); + RenderContextController.StaicContextController.Prepare(); RenderContextController.StaicContextController.Render(cbExecutor); } @@ -308,6 +316,7 @@ namespace DCFApixels RenderContextController contextController = RenderContextController.GetController(new RenderContext(camera)); contextController.Prepare(); contextController.Render(cbExecutor); + } #if UNITY_EDITOR @@ -317,13 +326,28 @@ namespace DCFApixels { if (obj != Camera.main) { return; } - Camera camera = Camera.current; + //if (_lastEditorToRenderGizmosTicks != _editorTicks) + //{ + // _renderGizmosTicks++; + // _lastEditorToRenderGizmosTicks = _editorTicks; + //} + + //Camera camera = Camera.current; + //CallDrawGizmos(camera); + } +#endif + + private static void CallDrawGizmos(Camera camera) + { Color guiColor = GUI.color; + Color guiContextColor = GUI.contentColor; + Color guiBackgroundColor = GUI.backgroundColor; Color gizmosColor = Gizmos.color; Color handlesColor = Handles.color; GL.MultMatrix(Handles.matrix); + //TODO раскоментить RenderContextController.StaicContextController.Render_UnityGizmos(); if (camera == null) { return; } @@ -331,10 +355,11 @@ namespace DCFApixels RenderContextController.GetController(new RenderContext(camera)).Render_UnityGizmos(); GUI.color = guiColor; + GUI.contentColor = guiContextColor; + GUI.backgroundColor = guiBackgroundColor; Gizmos.color = gizmosColor; Handles.color = handlesColor; } -#endif #endregion @@ -532,7 +557,6 @@ namespace DCFApixels // //} //} - [IN(LINE)] public void Prepare() { @@ -559,7 +583,6 @@ namespace DCFApixels _buffers[i].Render(cbExecutor); } - RunEnd(); } } @@ -576,6 +599,8 @@ namespace DCFApixels { _buffers[i].Render_UnityGizmos(); } + + RunEnd(); } } @@ -697,6 +722,7 @@ namespace DCFApixels } return removeCount; } + public sealed override int RunEnd() { int removeCount = 0; @@ -774,6 +800,7 @@ namespace DCFApixels public override void Render_UnityGizmos() { if (_rendererUnityGizmos == null) { return; } + Debug.Log(_gizmos._count); if (_gizmos.Count <= 0) { return; } #if DEV_MODE using (_renderMarker.Auto()) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 1e8427b..3e5bd88 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -5,81 +5,45 @@ using UnityEditor; using UnityEngine; using UnityEngine.Rendering; -namespace DCFApixels { +namespace DCFApixels +{ using IN = System.Runtime.CompilerServices.MethodImplAttribute; - - public static partial class DebugX { - public readonly partial struct DrawHandler { + + public static partial class DebugX + { + public readonly partial struct DrawHandler + { #region Text + [IN(LINE)] public DrawHandler TextWorldScale(Vector3 position, object text) => Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default, true)); + [IN(LINE)] public DrawHandler TextWorldScale(Vector3 position, object text, DebugXTextSettings settings) => Gizmo(new TextGizmo(position, text, settings, true)); + [IN(LINE)] public DrawHandler Text(Vector3 position, object text) => Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default, false)); + [IN(LINE)] public DrawHandler Text(Vector3 position, object text, DebugXTextSettings settings) => Gizmo(new TextGizmo(position, text, settings, false)); - /// - /// Draw text at the world position. - /// Can pass any object where ToString() will be called. - /// - /// World position. - /// String or any other object. - [IN(LINE)] public DrawHandler Text(Vector3 position, object text) => Gizmo(new TextGizmo(new TextBuilder(position, text))); - - /// - /// Draw text at the world position. - /// Can pass any object where ToString() will be called. - /// - /// World position. - /// String or any other object. - /// Text font size. - [IN(LINE)] public DrawHandler Text(Vector3 position, object text, int fontSize) => Gizmo(new TextGizmo(new TextBuilder(position, text, fontSize))); - - /// - /// Draw text at the world position. - /// Can pass any object where ToString() will be called. - /// - /// World position. - /// String or any other object. - /// Text font size. - /// Text alignment. - [IN(LINE)] public DrawHandler Text(Vector3 position, object text, int fontSize, TextAnchor textAnchor) => Gizmo(new TextGizmo(new TextBuilder(position, text, fontSize, textAnchor))); - - /// - /// Use text builder to pass more details about how text-gizmo should be drawn. - /// - /// Settings with a builder pattern. - [IN(LINE)] public DrawHandler Text(TextBuilder textBuilder) => Gizmo(new TextGizmo(textBuilder)); - - private readonly struct TextGizmo : IGizmo { - private const int BACKGROUND_TEXTURE_WIDTH = 2; - private const int BACKGROUND_TEXTURE_HEIGHT = 2; - private const int BACKGROUND_TEXTURE_PIXELS_COUNT = BACKGROUND_TEXTURE_WIDTH * BACKGROUND_TEXTURE_HEIGHT; - - // TODO: Normally Texture2D should be destroyed when not needed anymore. Though it will live through entire app lifetime. What about editor? - private static Texture2D _backgroundTexture; - private static Color32[] _backgroundTexturePixels; - public readonly TextBuilder TextBuilderInstance; + private readonly struct TextGizmo : IGizmo + { + public readonly Vector3 Position; + public readonly string Text; + public readonly DebugXTextSettings Settings; + public readonly bool IsWorldSpaceScale; [IN(LINE)] - public TextGizmo(TextBuilder textBuilder) { - TextBuilderInstance = textBuilder; - CheckTextureInstance(); + public TextGizmo(Vector3 position, object text, DebugXTextSettings settings, bool isWorldSpaceScale) + { + Position = position; + Text = text.ToString(); + Settings = settings; + IsWorldSpaceScale = isWorldSpaceScale; } - /// - /// Texture should be set once after the app domain is cleared. - /// - private void CheckTextureInstance() { - if (_backgroundTexture != null) { - return; - } - - _backgroundTexture = new Texture2D(BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT); - _backgroundTexturePixels = new Color32[BACKGROUND_TEXTURE_PIXELS_COUNT]; - _backgroundTexture.SetPixels32(_backgroundTexturePixels); - _backgroundTexture.Apply(); - } - public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } #region Renderer private class Renderer : IGizmoRenderer_UnityGizmos { - private static GUIStyle _labelStyle; + private static Color32[] _backgroundTexturePixels; + private static Texture2D _backgroundTexture; + + private static GUIStyle _labelStyle; + private static GUIStyle _labelStyleWithBackground; private static GUIContent _labelDummy; public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); public bool IsStaticRender => false; @@ -88,176 +52,105 @@ namespace DCFApixels { public void Render_UnityGizmos(Camera camera, GizmosList list) { #if UNITY_EDITOR - Color defaultColor = GUI.color; - if (_labelStyle == null || _labelDummy == null) - { - _labelStyle = new GUIStyle(GUI.skin.label) { - richText = false - }; - _labelDummy = new GUIContent(); - } + InitStatic(); + var zoom = GetCameraZoom(camera); - if (list.Count == 0) { - return; - } - - var zoom = GetCameraZoom(); - Handles.BeginGUI(); foreach (ref readonly var item in list) { - GUI.color = item.Color * GlobalColor; - _labelDummy.text = item.Value.TextBuilderInstance.Text.ToString(); + GUI.contentColor = item.Color * GlobalColor; + GUI.backgroundColor = item.Value.Settings.BackgroundColor * GlobalColor; - _labelStyle.fontSize = item.Value.TextBuilderInstance.UseWorldScale - ? Mathf.FloorToInt(item.Value.TextBuilderInstance.FontSize / zoom) - : item.Value.TextBuilderInstance.FontSize; - - _labelStyle.alignment = item.Value.TextBuilderInstance.TextAnchor; + _labelDummy.text = item.Value.Text; + GUIStyle style = item.Value.Settings.IsHasBackground ? _labelStyleWithBackground : _labelStyle; - _labelStyle.normal = new GUIStyleState { + style.fontSize = item.Value.IsWorldSpaceScale + ? Mathf.FloorToInt(item.Value.Settings.FontSize / zoom) + : item.Value.Settings.FontSize; + + style.alignment = item.Value.Settings.TextAnchor; + + _labelStyle.normal = new GUIStyleState + { textColor = item.Color * GlobalColor, }; - - if (item.Value.TextBuilderInstance.UseBackground) { - for (int i = 0; i < BACKGROUND_TEXTURE_PIXELS_COUNT; i++) { - _backgroundTexturePixels[i] = item.Value.TextBuilderInstance.BackgroundColor; - } - _backgroundTexture.SetPixels32(_backgroundTexturePixels); - _backgroundTexture.Apply(); - _labelStyle.normal.background = _backgroundTexture; - } - - if (!(HandleUtility.WorldToGUIPointWithDepth(item.Value.TextBuilderInstance.Position).z < 0f)) + if (!(HandleUtility.WorldToGUIPointWithDepth(item.Value.Position).z < 0f)) { - GUI.Label(HandleUtility.WorldPointToSizedRect(item.Value.TextBuilderInstance.Position, _labelDummy, _labelStyle), _labelDummy, _labelStyle); + GUI.Label(HandleUtility.WorldPointToSizedRect(item.Value.Position, _labelDummy, _labelStyle), _labelDummy, style); } } Handles.EndGUI(); - GUI.color = defaultColor; - - float GetCameraZoom() { - const float DEFAULT_ZOOM = 1f; - - if (camera != null) { - return camera.orthographicSize; - } - - var currentDrawingSceneView = SceneView.currentDrawingSceneView; - - if (currentDrawingSceneView == null) { - return DEFAULT_ZOOM; - } - - var localCamera = currentDrawingSceneView.camera; - - if (localCamera != null) { - return localCamera.orthographicSize; - } - - return DEFAULT_ZOOM; - } #endif } + private void InitStatic() + { + const int BACKGROUND_TEXTURE_WIDTH = 2; + const int BACKGROUND_TEXTURE_HEIGHT = 2; + const int BACKGROUND_TEXTURE_PIXELS_COUNT = BACKGROUND_TEXTURE_WIDTH * BACKGROUND_TEXTURE_HEIGHT; + + if (_labelStyle == null || _labelDummy == null || _labelStyleWithBackground == null) + { + _labelStyle = new GUIStyle(GUI.skin.label) + { + richText = false, + padding = new RectOffset(0, 0, 0, 0), + margin = new RectOffset(0, 0, 0, 0) + }; + _labelDummy = new GUIContent(); + + var backgroundTexturePixels = new Color32[BACKGROUND_TEXTURE_PIXELS_COUNT]; + for (int i = 0; i < backgroundTexturePixels.Length; i++) + { + backgroundTexturePixels[i] = new Color32(255, 255, 255, 255); + } + _backgroundTexturePixels = backgroundTexturePixels; + + var backgroundTexture = new Texture2D(BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT); + backgroundTexture.SetPixels32(backgroundTexturePixels); + backgroundTexture.Apply(); + + _backgroundTexture = backgroundTexture; + + _labelStyleWithBackground = new GUIStyle(_labelStyle); + _labelStyleWithBackground.normal.background = backgroundTexture; + _labelStyleWithBackground.active.background = backgroundTexture; + _labelStyleWithBackground.focused.background = backgroundTexture; + _labelStyleWithBackground.hover.background = backgroundTexture; + } + } + private static float GetCameraZoom(Camera camera) + { + const float DEFAULT_ZOOM = 1f; + + if (camera != null) + { + return camera.orthographicSize; + } + return DEFAULT_ZOOM; + + //var currentDrawingSceneView = SceneView.currentDrawingSceneView; + // + //if (currentDrawingSceneView == null) + //{ + // return DEFAULT_ZOOM; + //} + // + //var localCamera = currentDrawingSceneView.camera; + // + //if (localCamera != null) + //{ + // return localCamera.orthographicSize; + //} + // + //return DEFAULT_ZOOM; + } } #endregion } - #endregion - - #region TextBuilder - /// - /// Set text gizmos instance settings using a builder pattern. - /// - public struct TextBuilder { - private const TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.MiddleLeft; - private const int DEFAULT_FONT_SIZE = 16; - - /// - /// Text world position. - /// - public Vector3 Position { get; set; } - - /// - /// Text. Uses ToString() of the passed object. - /// - public object Text { get; set; } - /// - /// Font size. Default is . - /// - public int FontSize { get; set; } - - /// - /// Text alignment. Default is . - /// - public TextAnchor TextAnchor { get; set; } - - /// - /// Background texture color. - /// - public Color BackgroundColor { get; set; } - - /// - /// Flag to use background texture and background color when rendering text gizmo instance. - /// - public bool UseBackground { get; set; } - - /// - /// If set true - camera zooming will affect text scale to keep same size in the world. - /// - public bool UseWorldScale { get; set; } - - public TextBuilder(Vector3 position, object text, int fontSize = DEFAULT_FONT_SIZE, TextAnchor textAnchor = DEFAULT_TEXT_ANCHOR) : this() { - Position = position; - Text = text; - FontSize = fontSize; - TextAnchor = textAnchor; - } - - public TextBuilder SetPosition(Vector3 position) { - Position = position; - return this; - } - - public TextBuilder SetText(object text) { - Text = text; - return this; - } - - public TextBuilder SetFontSize(int fontSize) { - FontSize = fontSize; - return this; - } - - public TextBuilder SetTextAnchor(TextAnchor textAnchor) { - TextAnchor = textAnchor; - return this; - } - - public TextBuilder SetBackground(Color backgroundColor) { - UseBackground = true; - BackgroundColor = backgroundColor; - return this; - } - - public TextBuilder RemoveBackground() { - UseBackground = false; - return this; - } - - public TextBuilder SetWorldScaling() { - UseWorldScale = true; - return this; - } - - public TextBuilder RemoveWorldScaling() { - UseWorldScale = false; - return this; - } - } - #endregion +#endregion } } } \ No newline at end of file diff --git a/Runtime/Gizmos/DebugX.text.cs.meta b/Runtime/Gizmos/DebugX.text.cs.meta new file mode 100644 index 0000000..baa2bda --- /dev/null +++ b/Runtime/Gizmos/DebugX.text.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c465dc7f5c78c9549a063ecfc5e8326d \ No newline at end of file diff --git a/Runtime/Utils/DebugXTextSettings.cs b/Runtime/Utils/DebugXTextSettings.cs new file mode 100644 index 0000000..8bba295 --- /dev/null +++ b/Runtime/Utils/DebugXTextSettings.cs @@ -0,0 +1,41 @@ +п»їusing UnityEngine; + +namespace DCFApixels +{ + public struct DebugXTextSettings + { + public const TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.MiddleLeft; + public const int DEFAULT_FONT_SIZE = 16; + public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default); + + public int FontSize; + public TextAnchor TextAnchor; + public Color BackgroundColor; + public bool IsHasBackground + { + get { return BackgroundColor.a > 0; } + } + public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor) + { + FontSize = fontSize; + TextAnchor = textAnchor; + BackgroundColor = backgroundColor; + } + + public DebugXTextSettings SetSize(int fontSize) + { + FontSize = fontSize; + return this; + } + public DebugXTextSettings SetAnchor(TextAnchor textAnchor) + { + TextAnchor = textAnchor; + return this; + } + public DebugXTextSettings SetBackground(Color backgroundColor) + { + BackgroundColor = backgroundColor; + return this; + } + } +} \ No newline at end of file diff --git a/Runtime/Utils/DebugXTextSettings.cs.meta b/Runtime/Utils/DebugXTextSettings.cs.meta new file mode 100644 index 0000000..c0e5dfb --- /dev/null +++ b/Runtime/Utils/DebugXTextSettings.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 5a9b0ba5a6770294b9777fca87f3b7a4 \ No newline at end of file diff --git a/Samples/Sample.unity b/Samples/Sample.unity index 919aeec..3bfdc5a 100644 --- a/Samples/Sample.unity +++ b/Samples/Sample.unity @@ -4135,7 +4135,7 @@ GameObject: m_Component: - component: {fileID: 1482731616} m_Layer: 0 - m_Name: Hello World + m_Name: Hello World Hello World Hello World m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 diff --git a/Samples/Scripts/DebugXSample_Other.cs b/Samples/Scripts/DebugXSample_Other.cs index 437bca1..71c06a1 100644 --- a/Samples/Scripts/DebugXSample_Other.cs +++ b/Samples/Scripts/DebugXSample_Other.cs @@ -8,6 +8,7 @@ namespace DCFApixels.DebugXCore.Samples public Gradient Gradient; public float GradientMultiplier = 5; public Transform[] Points; + private static readonly Color _background = new Color(0, 0, 0, 0.5f); #if UNITY_EDITOR private void OnDrawGizmos() @@ -20,7 +21,6 @@ namespace DCFApixels.DebugXCore.Samples Draw(); } #endif - private void Draw() { int i = -1; @@ -29,7 +29,7 @@ namespace DCFApixels.DebugXCore.Samples i++; DebugX.Draw(GetColor(Points[i])).Cross(Points[i].position, Points[i].localScale.x); i++; DebugX.Draw(GetColor(Points[i])).BillboardCircle(Points[i].position, Points[i].localScale.x * RADIUS_M); i++; DebugX.Draw(GetColor(Points[i])).WireMesh(Points[i].position, Points[i].rotation, Points[i].localScale * RADIUS_M); - i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name); + i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.Default.SetBackground(Color.black)); i++; DebugX.Draw(GetColor(Points[i])).Dot(Points[i].position); i++; DebugX.Draw(GetColor(Points[i])).WireDot(Points[i].position); @@ -50,4 +50,4 @@ namespace DCFApixels.DebugXCore.Samples return Gradient.Evaluate(Mathf.Clamp01(t)); } } -} +} \ No newline at end of file From c5703adc4de31797f3a3a984f1cdb493f9be9226 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 27 Feb 2025 16:46:55 +0800 Subject: [PATCH 2/3] fix text render --- Runtime/DebugX.cs | 9 ++++----- Runtime/Gizmos/DebugX.text.cs | 26 +++++++++----------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index aaba190..cbac3a6 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -305,10 +305,10 @@ namespace DCFApixels if (DebugXUtility.IsGizmosRender()) { - CallDrawGizmos(camera); - RenderContextController.StaicContextController.Prepare(); RenderContextController.StaicContextController.Render(cbExecutor); + + CallDrawGizmos(camera); } if (camera == null) { return; } @@ -347,7 +347,6 @@ namespace DCFApixels Color handlesColor = Handles.color; GL.MultMatrix(Handles.matrix); - //TODO раскоментить RenderContextController.StaicContextController.Render_UnityGizmos(); if (camera == null) { return; } @@ -600,7 +599,7 @@ namespace DCFApixels _buffers[i].Render_UnityGizmos(); } - RunEnd(); + //RunEnd(); } } @@ -800,7 +799,7 @@ namespace DCFApixels public override void Render_UnityGizmos() { if (_rendererUnityGizmos == null) { return; } - Debug.Log(_gizmos._count); + //Debug.Log(_gizmos._count); if (_gizmos.Count <= 0) { return; } #if DEV_MODE using (_renderMarker.Auto()) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 3e5bd88..27a36f9 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -39,9 +39,6 @@ namespace DCFApixels #region Renderer private class Renderer : IGizmoRenderer_UnityGizmos { - private static Color32[] _backgroundTexturePixels; - private static Texture2D _backgroundTexture; - private static GUIStyle _labelStyle; private static GUIStyle _labelStyleWithBackground; private static GUIContent _labelDummy; @@ -58,11 +55,8 @@ namespace DCFApixels Handles.BeginGUI(); foreach (ref readonly var item in list) { - GUI.contentColor = item.Color * GlobalColor; - GUI.backgroundColor = item.Value.Settings.BackgroundColor * GlobalColor; - _labelDummy.text = item.Value.Text; - GUIStyle style = item.Value.Settings.IsHasBackground ? _labelStyleWithBackground : _labelStyle; + GUIStyle style = _labelStyle; style.fontSize = item.Value.IsWorldSpaceScale ? Mathf.FloorToInt(item.Value.Settings.FontSize / zoom) @@ -70,15 +64,16 @@ namespace DCFApixels style.alignment = item.Value.Settings.TextAnchor; - _labelStyle.normal = new GUIStyleState - { - textColor = item.Color * GlobalColor, - }; - - if (!(HandleUtility.WorldToGUIPointWithDepth(item.Value.Position).z < 0f)) { - GUI.Label(HandleUtility.WorldPointToSizedRect(item.Value.Position, _labelDummy, _labelStyle), _labelDummy, style); + + Rect rect = HandleUtility.WorldPointToSizedRect(item.Value.Position, _labelDummy, _labelStyle); + Color c = item.Value.Settings.BackgroundColor * GlobalColor; + GUI.color = c; + EditorGUI.DrawRect(rect, c); + + GUI.color = item.Color * GlobalColor; + GUI.Label(rect, _labelDummy, style); } } Handles.EndGUI(); @@ -105,14 +100,11 @@ namespace DCFApixels { backgroundTexturePixels[i] = new Color32(255, 255, 255, 255); } - _backgroundTexturePixels = backgroundTexturePixels; var backgroundTexture = new Texture2D(BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT); backgroundTexture.SetPixels32(backgroundTexturePixels); backgroundTexture.Apply(); - _backgroundTexture = backgroundTexture; - _labelStyleWithBackground = new GUIStyle(_labelStyle); _labelStyleWithBackground.normal.background = backgroundTexture; _labelStyleWithBackground.active.background = backgroundTexture; From a11b31412bc7b6cd8c0f3f3f0169d105b7be55a0 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 27 Feb 2025 20:01:22 +0800 Subject: [PATCH 3/3] fix text stash --- Runtime/DebugX.cs | 4 + Runtime/Gizmos/DebugX.text.cs | 169 ++++++++++++++++++++++++++++------ 2 files changed, 145 insertions(+), 28 deletions(-) diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index cbac3a6..4147200 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -344,7 +344,9 @@ namespace DCFApixels Color guiContextColor = GUI.contentColor; Color guiBackgroundColor = GUI.backgroundColor; Color gizmosColor = Gizmos.color; +#if Handles Color handlesColor = Handles.color; +#endif GL.MultMatrix(Handles.matrix); RenderContextController.StaicContextController.Render_UnityGizmos(); @@ -357,7 +359,9 @@ namespace DCFApixels GUI.contentColor = guiContextColor; GUI.backgroundColor = guiBackgroundColor; Gizmos.color = gizmosColor; +#if Handles Handles.color = handlesColor; +#endif } #endregion diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 27a36f9..784402f 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -40,7 +40,6 @@ namespace DCFApixels private class Renderer : IGizmoRenderer_UnityGizmos { private static GUIStyle _labelStyle; - private static GUIStyle _labelStyleWithBackground; private static GUIContent _labelDummy; public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); public bool IsStaticRender => false; @@ -49,6 +48,13 @@ namespace DCFApixels public void Render_UnityGizmos(Camera camera, GizmosList list) { #if UNITY_EDITOR + if (Event.current.type != EventType.Repaint) { return; } + //bool c = camera.name == "SceneCamera"; + bool ccc = camera == Camera.main; + //bool x = camera == Camera.main; + bool x = true; + + if (camera == null) { return; } InitStatic(); var zoom = GetCameraZoom(camera); @@ -63,29 +69,152 @@ namespace DCFApixels : item.Value.Settings.FontSize; style.alignment = item.Value.Settings.TextAnchor; - - if (!(HandleUtility.WorldToGUIPointWithDepth(item.Value.Position).z < 0f)) + if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) { - - Rect rect = HandleUtility.WorldPointToSizedRect(item.Value.Position, _labelDummy, _labelStyle); + Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); + //if (x) Debug.Log(rect); + + + ////GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture); + //Rect screenRect = default; + //Rect originRect = default; + //CalculateScaledTextureRects(rect, ScaleMode.StretchToFill, ref screenRect, ref originRect); + + + GL.PushMatrix(); + GL.LoadPixelMatrix(0, Screen.width, Screen.height, 0); + + //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); + Color c = item.Value.Settings.BackgroundColor * GlobalColor; GUI.color = c; - EditorGUI.DrawRect(rect, c); + GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture); + + //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); + //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); GUI.color = item.Color * GlobalColor; - GUI.Label(rect, _labelDummy, style); + style.Draw(rect, _labelDummy, false, false, false, false); + + GL.PopMatrix(); } } Handles.EndGUI(); #endif } + + + #region Utils + public static Vector3 WorldToGUIPointWithDepth(Camera camera, Vector3 world) + { +#if UNITY_EDITOR + world = Handles.matrix.MultiplyPoint(world); + Vector3 vector = camera.WorldToScreenPoint(world); + vector.y = camera.pixelHeight - vector.y; + Vector2 vector2 = EditorGUIUtility.PixelsToPoints(vector); + return new Vector3(vector2.x, vector2.y, vector.z); +#endif + } + public static Rect WorldPointToSizedRect(Camera camera, Vector3 position, GUIContent content, GUIStyle style) + { +#if UNITY_EDITOR + Vector2 vector = (Vector2)WorldToGUIPointWithDepth(camera, position); + Vector2 vector2 = style.CalcSize(content); + Rect rect = new Rect(vector.x, vector.y, vector2.x, vector2.y); + switch (style.alignment) + { + case TextAnchor.UpperCenter: + rect.x -= rect.width * 0.5f; + break; + case TextAnchor.UpperRight: + rect.x -= rect.width; + break; + case TextAnchor.MiddleLeft: + rect.y -= rect.height * 0.5f; + break; + case TextAnchor.MiddleCenter: + rect.x -= rect.width * 0.5f; + rect.y -= rect.height * 0.5f; + break; + case TextAnchor.MiddleRight: + rect.x -= rect.width; + rect.y -= rect.height * 0.5f; + break; + case TextAnchor.LowerLeft: + rect.y -= rect.height; + break; + case TextAnchor.LowerCenter: + rect.x -= rect.width * 0.5f; + rect.y -= rect.height; + break; + case TextAnchor.LowerRight: + rect.x -= rect.width; + rect.y -= rect.height; + break; + } + + return style.padding.Add(rect); +#endif + } + //internal static bool CalculateScaledTextureRects(Rect position, ScaleMode scaleMode, float imageAspect, ref Rect outScreenRect, ref Rect outSourceRect) + internal static bool CalculateScaledTextureRects(Rect position, ScaleMode scaleMode, ref Rect outScreenRect, ref Rect outSourceRect) + { + const float imageAspect = 1; + + + float num = position.width / position.height; + bool result = false; + switch (scaleMode) + { + case ScaleMode.StretchToFill: + outScreenRect = position; + outSourceRect = new Rect(0f, 0f, 1f, 1f); + result = true; + break; + case ScaleMode.ScaleAndCrop: + if (num > imageAspect) + { + float num4 = imageAspect / num; + outScreenRect = position; + outSourceRect = new Rect(0f, (1f - num4) * 0.5f, 1f, num4); + result = true; + } + else + { + float num5 = num / imageAspect; + outScreenRect = position; + outSourceRect = new Rect(0.5f - num5 * 0.5f, 0f, num5, 1f); + result = true; + } + + break; + case ScaleMode.ScaleToFit: + if (num > imageAspect) + { + float num2 = imageAspect / num; + outScreenRect = new Rect(position.xMin + position.width * (1f - num2) * 0.5f, position.yMin, num2 * position.width, position.height); + outSourceRect = new Rect(0f, 0f, 1f, 1f); + result = true; + } + else + { + float num3 = num / imageAspect; + outScreenRect = new Rect(position.xMin, position.yMin + position.height * (1f - num3) * 0.5f, position.width, num3 * position.height); + outSourceRect = new Rect(0f, 0f, 1f, 1f); + result = true; + } + + break; + } + + return result; + } + #endregion + + private void InitStatic() { - const int BACKGROUND_TEXTURE_WIDTH = 2; - const int BACKGROUND_TEXTURE_HEIGHT = 2; - const int BACKGROUND_TEXTURE_PIXELS_COUNT = BACKGROUND_TEXTURE_WIDTH * BACKGROUND_TEXTURE_HEIGHT; - - if (_labelStyle == null || _labelDummy == null || _labelStyleWithBackground == null) + if (_labelStyle == null || _labelDummy == null) { _labelStyle = new GUIStyle(GUI.skin.label) { @@ -94,22 +223,6 @@ namespace DCFApixels margin = new RectOffset(0, 0, 0, 0) }; _labelDummy = new GUIContent(); - - var backgroundTexturePixels = new Color32[BACKGROUND_TEXTURE_PIXELS_COUNT]; - for (int i = 0; i < backgroundTexturePixels.Length; i++) - { - backgroundTexturePixels[i] = new Color32(255, 255, 255, 255); - } - - var backgroundTexture = new Texture2D(BACKGROUND_TEXTURE_WIDTH, BACKGROUND_TEXTURE_HEIGHT); - backgroundTexture.SetPixels32(backgroundTexturePixels); - backgroundTexture.Apply(); - - _labelStyleWithBackground = new GUIStyle(_labelStyle); - _labelStyleWithBackground.normal.background = backgroundTexture; - _labelStyleWithBackground.active.background = backgroundTexture; - _labelStyleWithBackground.focused.background = backgroundTexture; - _labelStyleWithBackground.hover.background = backgroundTexture; } } private static float GetCameraZoom(Camera camera)