From e8c9fce40e3186488f7900d1b8451aac9a88dafe Mon Sep 17 00:00:00 2001 From: Demexis Date: Wed, 26 Feb 2025 01:31:16 +0200 Subject: [PATCH 01/17] feat: improved text-gizmos API. + Can change font size. + Can change text alignment (TextAnchor). + Can set background color. + Can set world scaling. --- Runtime/Gizmos/DebugX.base.cs | 52 ------- Runtime/Gizmos/DebugX.text.cs | 253 ++++++++++++++++++++++++++++++++++ 2 files changed, 253 insertions(+), 52 deletions(-) create mode 100644 Runtime/Gizmos/DebugX.text.cs diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index 6f4649f..201f021 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -222,58 +222,6 @@ namespace DCFApixels } #endregion - #region Text - [IN(LINE)] public DrawHandler Text(Vector3 position, object text) => Gizmo(new TextGizmo(position, text)); - private readonly struct TextGizmo : IGizmo - { - public readonly Vector3 Position; - public readonly string Text; - [IN(LINE)] - public TextGizmo(Vector3 position, object text) - { - Position = position; - Text = text.ToString(); - } - public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } - - #region Renderer - private class Renderer : IGizmoRenderer_UnityGizmos - { - private static GUIStyle _labelStyle; - private static GUIContent _labelDummy; - public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); - public bool IsStaticRender => false; - public void Prepare(Camera camera, GizmosList list) { } - public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } - public void Render_UnityGizmos(Camera camera, GizmosList list) - { -#if UNITY_EDITOR - Color defaultColor = GUI.color; - if (_labelStyle == null || _labelDummy == null) - { - _labelStyle = GUI.skin.label; - _labelStyle.richText = false; - _labelDummy = new GUIContent(); - } - Handles.BeginGUI(); - foreach (ref readonly var item in list) - { - GUI.color = item.Color * GlobalColor; - _labelDummy.text = item.Value.Text; - if (!(HandleUtility.WorldToGUIPointWithDepth(item.Value.Position).z < 0f)) - { - GUI.Label(HandleUtility.WorldPointToSizedRect(item.Value.Position, _labelDummy, _labelStyle), _labelDummy, _labelStyle); - } - } - Handles.EndGUI(); - GUI.color = defaultColor; -#endif - } - } - #endregion - } - #endregion - // Base Renderers #region MeshRendererBase diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs new file mode 100644 index 0000000..578dd42 --- /dev/null +++ b/Runtime/Gizmos/DebugX.text.cs @@ -0,0 +1,253 @@ +using DCFApixels.DebugXCore; +#if UNITY_EDITOR +using UnityEditor; +#endif +using UnityEngine; +using UnityEngine.Rendering; + +namespace DCFApixels { + using IN = System.Runtime.CompilerServices.MethodImplAttribute; + + public static partial class DebugX { + public readonly partial struct DrawHandler { + #region Text + + /// + /// 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; + [IN(LINE)] + public TextGizmo(TextBuilder textBuilder) { + TextBuilderInstance = textBuilder; + CheckTextureInstance(); + } + + /// + /// Should be called 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 GUIContent _labelDummy; + public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); + public bool IsStaticRender => false; + public void Prepare(Camera camera, GizmosList list) { } + public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } + public void Render_UnityGizmos(Camera camera, GizmosList list) + { +#if UNITY_EDITOR + Color defaultColor = GUI.color; + if (_labelStyle == null || _labelDummy == null) + { + _labelStyle = GUI.skin.label; + _labelStyle.richText = false; + _labelDummy = new GUIContent(); + } + Handles.BeginGUI(); + foreach (ref readonly var item in list) + { + GUI.color = item.Color * GlobalColor; + _labelDummy.text = item.Value.TextBuilderInstance.Text.ToString(); + + if (item.Value.TextBuilderInstance.UseWorldScale) { + var zoom = 1f; + + var cam = GetCurrentCamera(); + if (cam != null) { + zoom = cam.orthographicSize; + } else { + var currentDrawingSceneView = SceneView.currentDrawingSceneView; + + if (currentDrawingSceneView != null + && SceneView.currentDrawingSceneView.camera != null) { + cam = currentDrawingSceneView.camera; + + if (camera != null) { + zoom = cam.orthographicSize; + } + } + } + + _labelStyle.fontSize = Mathf.FloorToInt(item.Value.TextBuilderInstance.FontSize / zoom); + } else { + _labelStyle.fontSize = item.Value.TextBuilderInstance.FontSize; + } + + _labelStyle.alignment = item.Value.TextBuilderInstance.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)) + { + GUI.Label(HandleUtility.WorldPointToSizedRect(item.Value.TextBuilderInstance.Position, _labelDummy, _labelStyle), _labelDummy, _labelStyle); + } + } + Handles.EndGUI(); + GUI.color = defaultColor; +#endif + } + } + #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 + } + } +} \ No newline at end of file From eff3652307f18af6ccfb739a136037039bb9fa9e Mon Sep 17 00:00:00 2001 From: Demexis Date: Wed, 26 Feb 2025 02:00:42 +0200 Subject: [PATCH 02/17] refactor: skipping gizmos list iteration and camera zoom search if a list is empty. --- Runtime/Gizmos/DebugX.text.cs | 56 +++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 578dd42..84b98fa 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -95,35 +95,22 @@ namespace DCFApixels { _labelStyle.richText = false; _labelDummy = new GUIContent(); } + + 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(); - if (item.Value.TextBuilderInstance.UseWorldScale) { - var zoom = 1f; - - var cam = GetCurrentCamera(); - if (cam != null) { - zoom = cam.orthographicSize; - } else { - var currentDrawingSceneView = SceneView.currentDrawingSceneView; - - if (currentDrawingSceneView != null - && SceneView.currentDrawingSceneView.camera != null) { - cam = currentDrawingSceneView.camera; - - if (camera != null) { - zoom = cam.orthographicSize; - } - } - } - - _labelStyle.fontSize = Mathf.FloorToInt(item.Value.TextBuilderInstance.FontSize / zoom); - } else { - _labelStyle.fontSize = item.Value.TextBuilderInstance.FontSize; - } + _labelStyle.fontSize = item.Value.TextBuilderInstance.UseWorldScale + ? Mathf.FloorToInt(item.Value.TextBuilderInstance.FontSize / zoom) + : item.Value.TextBuilderInstance.FontSize; _labelStyle.alignment = item.Value.TextBuilderInstance.TextAnchor; @@ -149,6 +136,29 @@ namespace DCFApixels { } Handles.EndGUI(); GUI.color = defaultColor; + + float GetCameraZoom() { + const float DEFAULT_ZOOM = 1f; + + var localCamera = GetCurrentCamera(); + if (localCamera != null) { + return localCamera.orthographicSize; + } + + var currentDrawingSceneView = SceneView.currentDrawingSceneView; + + if (currentDrawingSceneView == null) { + return DEFAULT_ZOOM; + } + + localCamera = currentDrawingSceneView.camera; + + if (camera != null) { + return localCamera.orthographicSize; + } + + return DEFAULT_ZOOM; + } #endif } } From 2c74be99a9966a1a020c0bf64d4ee1204b85b98e Mon Sep 17 00:00:00 2001 From: Demexis Date: Wed, 26 Feb 2025 02:21:34 +0200 Subject: [PATCH 03/17] fix: misspelled variable name. --- Runtime/Gizmos/DebugX.text.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 84b98fa..06751bc 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -153,7 +153,7 @@ namespace DCFApixels { localCamera = currentDrawingSceneView.camera; - if (camera != null) { + if (localCamera != null) { return localCamera.orthographicSize; } From 4bf61527a544a0704e43c8bf17ead00467b003ed Mon Sep 17 00:00:00 2001 From: Demexis Date: Wed, 26 Feb 2025 03:08:32 +0200 Subject: [PATCH 04/17] fix: avoid changing editor's GUI.skin by creating a new instance of GUIStyle. --- Runtime/Gizmos/DebugX.text.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 06751bc..34fcf32 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -91,8 +91,9 @@ namespace DCFApixels { Color defaultColor = GUI.color; if (_labelStyle == null || _labelDummy == null) { - _labelStyle = GUI.skin.label; - _labelStyle.richText = false; + _labelStyle = new GUIStyle(GUI.skin.label) { + richText = false + }; _labelDummy = new GUIContent(); } From 37aab9ca44f73f60739d72bd9e88550c4b464768 Mon Sep 17 00:00:00 2001 From: Demexis Date: Wed, 26 Feb 2025 03:52:53 +0200 Subject: [PATCH 05/17] refactor: using camera that was given in params. --- Runtime/Gizmos/DebugX.text.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 34fcf32..1e8427b 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -61,7 +61,7 @@ namespace DCFApixels { } /// - /// Should be called once after the app domain is cleared. + /// Texture should be set once after the app domain is cleared. /// private void CheckTextureInstance() { if (_backgroundTexture != null) { @@ -141,9 +141,8 @@ namespace DCFApixels { float GetCameraZoom() { const float DEFAULT_ZOOM = 1f; - var localCamera = GetCurrentCamera(); - if (localCamera != null) { - return localCamera.orthographicSize; + if (camera != null) { + return camera.orthographicSize; } var currentDrawingSceneView = SceneView.currentDrawingSceneView; @@ -152,7 +151,7 @@ namespace DCFApixels { return DEFAULT_ZOOM; } - localCamera = currentDrawingSceneView.camera; + var localCamera = currentDrawingSceneView.camera; if (localCamera != null) { return localCamera.orthographicSize; 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 06/17] 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 07/17] 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 08/17] 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) From a4269ac7f57aa12d8db0586daade363cf624f453 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 27 Feb 2025 21:19:54 +0800 Subject: [PATCH 09/17] fix text render --- Runtime/DebugX.cs | 2 +- Runtime/Gizmos/DebugX.text.cs | 128 +++++++++++++++------------------- 2 files changed, 56 insertions(+), 74 deletions(-) diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 4147200..215f83f 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -346,8 +346,8 @@ namespace DCFApixels Color gizmosColor = Gizmos.color; #if Handles Color handlesColor = Handles.color; -#endif GL.MultMatrix(Handles.matrix); +#endif RenderContextController.StaicContextController.Render_UnityGizmos(); diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 784402f..69d5e5f 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -37,28 +37,30 @@ namespace DCFApixels public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } #region Renderer - private class Renderer : IGizmoRenderer_UnityGizmos + private class Renderer : IGizmoRenderer { private static GUIStyle _labelStyle; private static GUIContent _labelDummy; + private static Texture2D _whiteTexture; public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); public bool IsStaticRender => false; public void Prepare(Camera camera, GizmosList list) { } - public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } + public void Render(Camera camera, GizmosList list, CommandBuffer cb) + { + Render_UnityGizmos(camera, list); + } public void Render_UnityGizmos(Camera camera, GizmosList list) { -#if UNITY_EDITOR + //return; +//#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; + Color dfColor = GUI.color; if (camera == null) { return; } InitStatic(); var zoom = GetCameraZoom(camera); - Handles.BeginGUI(); + //Handles.BeginGUI(); foreach (ref readonly var item in list) { _labelDummy.text = item.Value.Text; @@ -88,9 +90,9 @@ namespace DCFApixels Color c = item.Value.Settings.BackgroundColor * GlobalColor; GUI.color = c; - GUI.DrawTexture(rect, EditorGUIUtility.whiteTexture); + //GUI.DrawTexture(rect, _whiteTexture); - //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); + //Graphics.DrawTexture(rect, EditorGUIUtility.whiteTexture); //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); GUI.color = item.Color * GlobalColor; @@ -99,8 +101,9 @@ namespace DCFApixels GL.PopMatrix(); } } - Handles.EndGUI(); -#endif + //Handles.EndGUI(); + GUI.color = dfColor; +//#endif } @@ -109,15 +112,17 @@ namespace DCFApixels { #if UNITY_EDITOR world = Handles.matrix.MultiplyPoint(world); +#endif 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); + Vector2 vector2 = (Vector2)(vector); +#if UNITY_EDITOR + vector2 = EditorGUIUtility.PixelsToPoints(vector); #endif + return new Vector3(vector2.x, vector2.y, vector.z); } 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); @@ -154,75 +159,52 @@ namespace DCFApixels } 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() { - if (_labelStyle == null || _labelDummy == null) + if (_labelStyle == null || _labelDummy == null || _whiteTexture == null) { - _labelStyle = new GUIStyle(GUI.skin.label) + + GUIStyleState GenerateGUIStyleState() + { + var result = new GUIStyleState(); + result.textColor = Color.white; + result.background = null; + return result; + } + + GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin + //GUISkin skin = GUI.skin; + _labelStyle = new GUIStyle(skin.label) { richText = false, - padding = new RectOffset(0, 0, 0, 0), - margin = new RectOffset(0, 0, 0, 0) + padding = new RectOffset(0, 0, 3, 3), + margin = new RectOffset(4, 4, 4, 4), + normal = GenerateGUIStyleState(), + active = GenerateGUIStyleState(), + hover = GenerateGUIStyleState(), + focused = GenerateGUIStyleState(), }; + + _labelDummy = new GUIContent(); + + + _whiteTexture = new Texture2D(2, 2); + Color32[] color = new Color32[] + { + new Color32(255,255,255,255), + new Color32(255,255,255,255), + new Color32(255,255,255,255), + new Color32(255,255,255,255), + }; + for (int i = 0; i < 4; i++) + { + _whiteTexture.SetPixels32(color); + } } } private static float GetCameraZoom(Camera camera) From c267b9401c445351050097190020095f2458a729 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 09:16:14 +0800 Subject: [PATCH 10/17] fix text background --- Runtime/Gizmos/DebugX.text.cs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 69d5e5f..8a8266f 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -51,8 +51,6 @@ namespace DCFApixels } public void Render_UnityGizmos(Camera camera, GizmosList list) { - //return; -//#if UNITY_EDITOR if (Event.current.type != EventType.Repaint) { return; } Color dfColor = GUI.color; @@ -92,7 +90,9 @@ namespace DCFApixels GUI.color = c; //GUI.DrawTexture(rect, _whiteTexture); - //Graphics.DrawTexture(rect, EditorGUIUtility.whiteTexture); + var mat = DebugXAssets.Materials.Unlit; + mat.SetColor(ColorPropertyID, c); + Graphics.DrawTexture(rect, _whiteTexture, mat); //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); GUI.color = item.Color * GlobalColor; @@ -101,9 +101,8 @@ namespace DCFApixels GL.PopMatrix(); } } - //Handles.EndGUI(); GUI.color = dfColor; -//#endif + DebugXAssets.Materials.Unlit.SetColor(ColorPropertyID, Color.white); } @@ -157,7 +156,6 @@ namespace DCFApixels rect.y -= rect.height; break; } - return style.padding.Add(rect); } #endregion @@ -167,7 +165,6 @@ namespace DCFApixels { if (_labelStyle == null || _labelDummy == null || _whiteTexture == null) { - GUIStyleState GenerateGUIStyleState() { var result = new GUIStyleState(); @@ -175,24 +172,21 @@ namespace DCFApixels result.background = null; return result; } - GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin //GUISkin skin = GUI.skin; _labelStyle = new GUIStyle(skin.label) { richText = false, - padding = new RectOffset(0, 0, 3, 3), - margin = new RectOffset(4, 4, 4, 4), + padding = new RectOffset(1, 1, 1, 1), + margin = new RectOffset(0, 0, 0, 0), normal = GenerateGUIStyleState(), active = GenerateGUIStyleState(), hover = GenerateGUIStyleState(), focused = GenerateGUIStyleState(), }; - _labelDummy = new GUIContent(); - _whiteTexture = new Texture2D(2, 2); Color32[] color = new Color32[] { @@ -201,10 +195,8 @@ namespace DCFApixels new Color32(255,255,255,255), new Color32(255,255,255,255), }; - for (int i = 0; i < 4; i++) - { - _whiteTexture.SetPixels32(color); - } + _whiteTexture.SetPixels32(color); + _whiteTexture.Apply(); } } private static float GetCameraZoom(Camera camera) From 1aa6c967973712141d2e0bedc2df70b0b41f8096 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 09:24:37 +0800 Subject: [PATCH 11/17] fix text offset --- Runtime/Gizmos/DebugX.text.cs | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 8a8266f..3669ffe 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -57,7 +57,7 @@ namespace DCFApixels if (camera == null) { return; } InitStatic(); var zoom = GetCameraZoom(camera); - + var isSceneView = camera.name == "SceneCamera"; //Handles.BeginGUI(); foreach (ref readonly var item in list) { @@ -72,32 +72,21 @@ namespace DCFApixels if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) { 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); + GL.LoadPixelMatrix(0, Screen.width, Screen.height - (isSceneView ? 50 : 0), 0); - //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); Color c = item.Value.Settings.BackgroundColor * GlobalColor; GUI.color = c; - //GUI.DrawTexture(rect, _whiteTexture); - var mat = DebugXAssets.Materials.Unlit; mat.SetColor(ColorPropertyID, c); Graphics.DrawTexture(rect, _whiteTexture, mat); - //Graphics.DrawTexture(screenRect, EditorGUIUtility.whiteTexture, screenRect, 0, 0, 0, 0); GUI.color = item.Color * GlobalColor; style.Draw(rect, _labelDummy, false, false, false, false); + GL.PopMatrix(); } } From 6eb6e61d02f9d0f12277e7beeced908f1d313e38 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 09:24:44 +0800 Subject: [PATCH 12/17] update sample --- Samples/Other/Ground.mat | 4 ++-- Samples/Sample.unity | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Samples/Other/Ground.mat b/Samples/Other/Ground.mat index a397a41..1f5774b 100644 --- a/Samples/Other/Ground.mat +++ b/Samples/Other/Ground.mat @@ -135,14 +135,14 @@ Material: - _GlossinessSource: 0 - _GlossyReflections: 1 - _LightingEnabled: 1 - - _Metallic: 0 + - _Metallic: 0.046 - _Mode: 0 - _OcclusionStrength: 1 - _Parallax: 0.02 - _QueueOffset: 0 - _ReceiveShadows: 0 - _Shininess: 0 - - _Smoothness: 0 + - _Smoothness: 0.047 - _SmoothnessSource: 0 - _SmoothnessTextureChannel: 0 - _SoftParticlesEnabled: 0 diff --git a/Samples/Sample.unity b/Samples/Sample.unity index 3bfdc5a..dd247d0 100644 --- a/Samples/Sample.unity +++ b/Samples/Sample.unity @@ -1229,7 +1229,7 @@ Camera: m_ShutterSpeed: 0.005 m_Aperture: 16 m_FocusDistance: 10 - m_FocalLength: 50 + m_FocalLength: 20.78461 m_BladeCount: 5 m_Curvature: {x: 2, y: 11} m_BarrelClipping: 0.25 @@ -1244,7 +1244,7 @@ Camera: height: 1 near clip plane: 0.3 far clip plane: 1000 - field of view: 60 + field of view: 60.000004 orthographic: 0 orthographic size: 5 m_Depth: -1 @@ -1277,7 +1277,7 @@ Transform: m_Children: - {fileID: 1800693605} m_Father: {fileID: 0} - m_LocalEulerAnglesHint: {x: 60, y: 0, z: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &298618463 MonoBehaviour: m_ObjectHideFlags: 0 @@ -4135,9 +4135,9 @@ GameObject: m_Component: - component: {fileID: 1482731616} m_Layer: 0 - m_Name: Hello World Hello World Hello World + m_Name: Hello World m_TagString: Untagged - m_Icon: {fileID: 0} + m_Icon: {fileID: 7866945982896999795, guid: 0000000000000000d000000000000000, type: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 From c91c27c1cbc71b9ff114b705b37219724162e4ad Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 09:28:47 +0800 Subject: [PATCH 13/17] fix text --- Runtime/Gizmos/DebugX.text.cs | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 3669ffe..d74257b 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -57,8 +57,23 @@ namespace DCFApixels if (camera == null) { return; } InitStatic(); var zoom = GetCameraZoom(camera); - var isSceneView = camera.name == "SceneCamera"; - //Handles.BeginGUI(); + bool isSceneView = false; +#if UNITY_EDITOR + isSceneView = camera.name == "SceneCamera"; +#endif + + if (isSceneView) + { +#if UNITY_EDITOR + Handles.BeginGUI(); +#endif + } + else + { + GL.PushMatrix(); + //GL.LoadPixelMatrix(0, Screen.width, Screen.height - (isSceneView ? 50 : 0), 0); + GL.LoadPixelMatrix(0, Screen.width, Screen.height, 0); + } foreach (ref readonly var item in list) { _labelDummy.text = item.Value.Text; @@ -73,8 +88,7 @@ namespace DCFApixels { Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); - GL.PushMatrix(); - GL.LoadPixelMatrix(0, Screen.width, Screen.height - (isSceneView ? 50 : 0), 0); + Color c = item.Value.Settings.BackgroundColor * GlobalColor; @@ -87,11 +101,21 @@ namespace DCFApixels style.Draw(rect, _labelDummy, false, false, false, false); - GL.PopMatrix(); } } GUI.color = dfColor; DebugXAssets.Materials.Unlit.SetColor(ColorPropertyID, Color.white); + + if (isSceneView) + { +#if UNITY_EDITOR + Handles.EndGUI(); +#endif + } + else + { + GL.PopMatrix(); + } } From 94bf266d1824659dcf83668e0c67fcfc8cee9850 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 09:47:18 +0800 Subject: [PATCH 14/17] fix text render sorting --- Runtime/DebugX.cs | 71 +++++----------------- Runtime/Gizmos/DebugX.text.cs | 15 +---- Runtime/Internal/CommandBufferExecutors.cs | 6 ++ Runtime/Utils/IGizmo.cs | 4 +- 4 files changed, 25 insertions(+), 71 deletions(-) diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 215f83f..2a2c6e4 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -307,8 +307,9 @@ namespace DCFApixels { RenderContextController.StaicContextController.Prepare(); RenderContextController.StaicContextController.Render(cbExecutor); - - CallDrawGizmos(camera); + cbExecutor.Submit(); + RenderContextController.StaicContextController.PostRender(); + RenderContextController.StaicContextController.RunEnd(); } if (camera == null) { return; } @@ -316,54 +317,10 @@ namespace DCFApixels RenderContextController contextController = RenderContextController.GetController(new RenderContext(camera)); contextController.Prepare(); contextController.Render(cbExecutor); - + cbExecutor.Submit(); + contextController.PostRender(); + contextController.RunEnd(); } - -#if UNITY_EDITOR - - [DrawGizmo(GizmoType.NonSelected | GizmoType.Selected)] - private static void DrawGizmos(Camera obj, GizmoType gizmoType) - { - if (obj != Camera.main) { return; } - - //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; -#if Handles - Color handlesColor = Handles.color; - GL.MultMatrix(Handles.matrix); -#endif - - RenderContextController.StaicContextController.Render_UnityGizmos(); - - if (camera == null) { return; } - _currentCamera = camera; - RenderContextController.GetController(new RenderContext(camera)).Render_UnityGizmos(); - - GUI.color = guiColor; - GUI.contentColor = guiContextColor; - GUI.backgroundColor = guiBackgroundColor; - Gizmos.color = gizmosColor; -#if Handles - Handles.color = handlesColor; -#endif - } - #endregion @@ -586,13 +543,13 @@ namespace DCFApixels _buffers[i].Render(cbExecutor); } - RunEnd(); + //RunEnd(); } } [IN(LINE)] - public void Render_UnityGizmos() + public void PostRender() { #if UNITY_EDITOR using (_cameraMarker.Auto()) @@ -600,7 +557,7 @@ namespace DCFApixels { for (int i = 0, iMax = _buffers.Count; i < iMax; i++) { - _buffers[i].Render_UnityGizmos(); + _buffers[i].PostRender(); } //RunEnd(); @@ -631,7 +588,7 @@ namespace DCFApixels public abstract int UpdateTimer(float deltaTime); public abstract void Prepare(); public abstract void Render(ICommandBufferExecutor cbExecutor); - public abstract void Render_UnityGizmos(); + public abstract void PostRender(); public abstract int RunEnd(); public abstract void Clear(); } @@ -654,7 +611,7 @@ namespace DCFApixels //private readonly CommandBuffer _dynamicCommandBuffer; private readonly IGizmoRenderer _renderer; - private readonly IGizmoRenderer_UnityGizmos _rendererUnityGizmos; + private readonly IGizmoRenderer_PostRender _rendererUnityGizmos; private readonly bool _isStatic; #if DEV_MODE @@ -681,7 +638,7 @@ namespace DCFApixels _renderer = new DummyRenderer(); } _isStatic = _renderer.IsStaticRender; - _rendererUnityGizmos = _renderer as IGizmoRenderer_UnityGizmos; + _rendererUnityGizmos = _renderer as IGizmoRenderer_PostRender; All.Add(this); All.Sort((a, b) => a.ExecuteOrder - b.ExecuteOrder); @@ -800,7 +757,7 @@ namespace DCFApixels cbExecutor.Execute(_staticCommandBuffer); } } - public override void Render_UnityGizmos() + public override void PostRender() { if (_rendererUnityGizmos == null) { return; } //Debug.Log(_gizmos._count); @@ -812,7 +769,7 @@ namespace DCFApixels GizmosList list = GizmosList.From(_gizmos._items, _gizmos._count); try { - _rendererUnityGizmos.Render_UnityGizmos(GetCurrentCamera(), list); + _rendererUnityGizmos.PostRender(GetCurrentCamera(), list); } catch (Exception e) { throw new Exception($"[{_debugName}] [Render] ", e); } } diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index d74257b..22aade5 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -37,7 +37,7 @@ namespace DCFApixels public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } #region Renderer - private class Renderer : IGizmoRenderer + private class Renderer : IGizmoRenderer_PostRender { private static GUIStyle _labelStyle; private static GUIContent _labelDummy; @@ -45,11 +45,8 @@ namespace DCFApixels public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); public bool IsStaticRender => false; public void Prepare(Camera camera, GizmosList list) { } - public void Render(Camera camera, GizmosList list, CommandBuffer cb) - { - Render_UnityGizmos(camera, list); - } - public void Render_UnityGizmos(Camera camera, GizmosList list) + public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } + public void PostRender(Camera camera, GizmosList list) { if (Event.current.type != EventType.Repaint) { return; } Color dfColor = GUI.color; @@ -88,19 +85,13 @@ namespace DCFApixels { Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); - - - Color c = item.Value.Settings.BackgroundColor * GlobalColor; - GUI.color = c; var mat = DebugXAssets.Materials.Unlit; mat.SetColor(ColorPropertyID, c); Graphics.DrawTexture(rect, _whiteTexture, mat); GUI.color = item.Color * GlobalColor; style.Draw(rect, _labelDummy, false, false, false, false); - - } } GUI.color = dfColor; diff --git a/Runtime/Internal/CommandBufferExecutors.cs b/Runtime/Internal/CommandBufferExecutors.cs index af78f59..506d42f 100644 --- a/Runtime/Internal/CommandBufferExecutors.cs +++ b/Runtime/Internal/CommandBufferExecutors.cs @@ -7,6 +7,7 @@ namespace DCFApixels.DebugXCore.Internal internal interface ICommandBufferExecutor { void Execute(CommandBuffer cb); + void Submit(); } internal class CommandBufferExecutorSRP : ICommandBufferExecutor { @@ -24,6 +25,10 @@ namespace DCFApixels.DebugXCore.Internal { RenderContext.ExecuteCommandBuffer(cb); } + public void Submit() + { + RenderContext.Submit(); + } } internal class CommandBufferExecutorBRP : ICommandBufferExecutor { @@ -39,5 +44,6 @@ namespace DCFApixels.DebugXCore.Internal { Graphics.ExecuteCommandBuffer(cb); } + public void Submit() { } } } \ No newline at end of file diff --git a/Runtime/Utils/IGizmo.cs b/Runtime/Utils/IGizmo.cs index e1073c0..e692740 100644 --- a/Runtime/Utils/IGizmo.cs +++ b/Runtime/Utils/IGizmo.cs @@ -17,9 +17,9 @@ namespace DCFApixels.DebugXCore void Prepare(Camera camera, GizmosList list); void Render(Camera camera, GizmosList list, CommandBuffer cb); } - public interface IGizmoRenderer_UnityGizmos : IGizmoRenderer where T : IGizmo + public interface IGizmoRenderer_PostRender : IGizmoRenderer where T : IGizmo { - void Render_UnityGizmos(Camera camera, GizmosList list); + void PostRender(Camera camera, GizmosList list); } From 5da2eb16eec593dbf04573240a04ced5ad2c9901 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 10:19:44 +0800 Subject: [PATCH 15/17] polishing --- Runtime/Consts.cs | 2 +- Runtime/DebugX.cs | 23 +++++------------------ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/Runtime/Consts.cs b/Runtime/Consts.cs index 1833f87..9cf27c9 100644 --- a/Runtime/Consts.cs +++ b/Runtime/Consts.cs @@ -29,7 +29,7 @@ namespace DCFApixels - private enum PauseStateX + private enum DebugXPauseState { Unpaused = 0, PreUnpaused = 1, //нужно чтобы отщелкунть паузу с задержкой в один тик diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 2a2c6e4..02e3d3e 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -22,7 +22,7 @@ namespace DCFApixels using IN = System.Runtime.CompilerServices.MethodImplAttribute; public static unsafe partial class DebugX { - private static PauseStateX _pauseState = PauseStateX.Unpaused; + private static DebugXPauseState _pauseState = DebugXPauseState.Unpaused; private static bool _isCameraContext = false; private static double _lastUnityTime; @@ -31,18 +31,12 @@ namespace DCFApixels 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; } @@ -65,7 +59,7 @@ namespace DCFApixels #if UNITY_EDITOR private static void EditorApplication_pauseStateChanged(PauseState obj) { - _pauseState = obj == PauseState.Paused ? PauseStateX.Paused : PauseStateX.PreUnpaused; + _pauseState = obj == PauseState.Paused ? DebugXPauseState.Paused : DebugXPauseState.PreUnpaused; } #endif #endregion @@ -234,12 +228,10 @@ namespace DCFApixels private static void OnPreRender_BRP(Camera camera) { PreRender_General(camera); - //throw new NotImplementedException(); } private static void OnPostRender_BRP(Camera camera) { PostRender_General(CommandBufferExecutorBRP.GetInstance(), camera); - //throw new NotImplementedException(); } private static void PreUpdateCallback() @@ -250,7 +242,7 @@ namespace DCFApixels if (_lastUnityTime < Time.unscaledTimeAsDouble) { _timeTicks++; - if (_pauseState == PauseStateX.Unpaused) + if (_pauseState == DebugXPauseState.Unpaused) { _deltaTime = Time.unscaledDeltaTime * _timeScaleCache; } @@ -269,9 +261,9 @@ namespace DCFApixels } } _lastUnityTime = Time.unscaledTimeAsDouble; - if (_pauseState == PauseStateX.PreUnpaused) + if (_pauseState == DebugXPauseState.PreUnpaused) { - _pauseState = PauseStateX.Unpaused; + _pauseState = DebugXPauseState.Unpaused; } SetGameSceneContext(); } @@ -294,7 +286,6 @@ namespace DCFApixels _currentCamera = camera; } - private static void PostRender_General(ICommandBufferExecutor cbExecutor, Camera camera) { if (_lastEditorToRenderTicks != _editorTicks) @@ -542,8 +533,6 @@ namespace DCFApixels { _buffers[i].Render(cbExecutor); } - - //RunEnd(); } } @@ -559,8 +548,6 @@ namespace DCFApixels { _buffers[i].PostRender(); } - - //RunEnd(); } } From 30f93a7d4cc825e5d4627bf57a1237d0d21b3f4f Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:18:49 +0800 Subject: [PATCH 16/17] fix text background/ rework text world space scaling --- Runtime/Gizmos/DebugX.text.cs | 139 +++++++++--------- Runtime/Materials/TextBackground.mat | 135 +++++++++++++++++ Runtime/Materials/TextBackground.mat.meta | 8 + .../DCFApixels.DebugX/MaterialsList.prefab | 87 +++++++++++ Runtime/Shaders/TextBackground.shader | 59 ++++++++ Runtime/Shaders/TextBackground.shader.meta | 9 ++ Runtime/Utils/DebugXAssets.cs | 2 + Runtime/Utils/DebugXTextSettings.cs | 28 ++-- Runtime/Utils/DebugXUtility.cs | 2 +- Samples/Sample.unity | 2 +- Samples/Scripts/DebugXSample_Other.cs | 6 +- 11 files changed, 394 insertions(+), 83 deletions(-) create mode 100644 Runtime/Materials/TextBackground.mat create mode 100644 Runtime/Materials/TextBackground.mat.meta create mode 100644 Runtime/Shaders/TextBackground.shader create mode 100644 Runtime/Shaders/TextBackground.shader.meta diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs index 22aade5..7188307 100644 --- a/Runtime/Gizmos/DebugX.text.cs +++ b/Runtime/Gizmos/DebugX.text.cs @@ -14,24 +14,20 @@ namespace DCFApixels 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)); + [IN(LINE)] public DrawHandler Text(Vector3 position, object text) => Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default)); + [IN(LINE)] public DrawHandler Text(Vector3 position, object text, DebugXTextSettings settings) => Gizmo(new TextGizmo(position, text, settings)); 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(Vector3 position, object text, DebugXTextSettings settings, bool isWorldSpaceScale) + public TextGizmo(Vector3 position, object text, DebugXTextSettings settings) { Position = position; Text = text.ToString(); Settings = settings; - IsWorldSpaceScale = isWorldSpaceScale; } public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } @@ -39,7 +35,7 @@ namespace DCFApixels #region Renderer private class Renderer : IGizmoRenderer_PostRender { - private static GUIStyle _labelStyle; + private static GUIStyle _labelStyle; private static GUIContent _labelDummy; private static Texture2D _whiteTexture; public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); @@ -48,14 +44,15 @@ namespace DCFApixels public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } public void PostRender(Camera camera, GizmosList list) { + if (camera == null) { return; } if (Event.current.type != EventType.Repaint) { return; } Color dfColor = GUI.color; - - if (camera == null) { return; } InitStatic(); - var zoom = GetCameraZoom(camera); bool isSceneView = false; + var backgroundMaterial = DebugXAssets.Materials.TextBackground; + #if UNITY_EDITOR + //TODO костыльный вариант, нужно поискать более надежный способ определить рендер SceneView isSceneView = camera.name == "SceneCamera"; #endif @@ -68,7 +65,6 @@ namespace DCFApixels else { GL.PushMatrix(); - //GL.LoadPixelMatrix(0, Screen.width, Screen.height - (isSceneView ? 50 : 0), 0); GL.LoadPixelMatrix(0, Screen.width, Screen.height, 0); } foreach (ref readonly var item in list) @@ -76,26 +72,25 @@ namespace DCFApixels _labelDummy.text = item.Value.Text; GUIStyle style = _labelStyle; - style.fontSize = item.Value.IsWorldSpaceScale - ? Mathf.FloorToInt(item.Value.Settings.FontSize / zoom) - : item.Value.Settings.FontSize; + var zoom = GetCameraZoom(camera, item.Value.Position); + + style.fontSize = Mathf.FloorToInt(Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor)); style.alignment = item.Value.Settings.TextAnchor; if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) { Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); - Color c = item.Value.Settings.BackgroundColor * GlobalColor; - var mat = DebugXAssets.Materials.Unlit; - mat.SetColor(ColorPropertyID, c); - Graphics.DrawTexture(rect, _whiteTexture, mat); + Color backgroundColor = item.Value.Settings.BackgroundColor * GlobalColor; + backgroundMaterial.SetColor(ColorPropertyID, backgroundColor); + Graphics.DrawTexture(rect, _whiteTexture, backgroundMaterial); GUI.color = item.Color * GlobalColor; style.Draw(rect, _labelDummy, false, false, false, false); } } GUI.color = dfColor; - DebugXAssets.Materials.Unlit.SetColor(ColorPropertyID, Color.white); + backgroundMaterial.SetColor(ColorPropertyID, Color.white); if (isSceneView) { @@ -109,6 +104,46 @@ namespace DCFApixels } } + #region Init + private void InitStatic() + { + if (_labelStyle == null || _labelDummy == null || _whiteTexture == null) + { + GUIStyleState GenerateGUIStyleState() + { + var result = new GUIStyleState(); + result.textColor = Color.white; + result.background = null; + return result; + } + GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin + //GUISkin skin = GUI.skin; + _labelStyle = new GUIStyle(skin.label) + { + richText = false, + padding = new RectOffset(1, 1, 0, 0), + margin = new RectOffset(0, 0, 0, 0), + normal = GenerateGUIStyleState(), + active = GenerateGUIStyleState(), + hover = GenerateGUIStyleState(), + focused = GenerateGUIStyleState(), + }; + + _labelDummy = new GUIContent(); + + _whiteTexture = new Texture2D(2, 2); + Color32[] color = new Color32[] + { + Color.white, + Color.white, + Color.white, + Color.white, + }; + _whiteTexture.SetPixels32(color); + _whiteTexture.Apply(); + } + } + #endregion #region Utils public static Vector3 WorldToGUIPointWithDepth(Camera camera, Vector3 world) @@ -162,56 +197,25 @@ namespace DCFApixels } return style.padding.Add(rect); } - #endregion - - - private void InitStatic() + private static float GetCameraZoom(Camera camera, Vector3 position) { - if (_labelStyle == null || _labelDummy == null || _whiteTexture == null) - { - GUIStyleState GenerateGUIStyleState() - { - var result = new GUIStyleState(); - result.textColor = Color.white; - result.background = null; - return result; - } - GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin - //GUISkin skin = GUI.skin; - _labelStyle = new GUIStyle(skin.label) - { - richText = false, - padding = new RectOffset(1, 1, 1, 1), - margin = new RectOffset(0, 0, 0, 0), - normal = GenerateGUIStyleState(), - active = GenerateGUIStyleState(), - hover = GenerateGUIStyleState(), - focused = GenerateGUIStyleState(), - }; + position = Handles.matrix.MultiplyPoint(position); + Transform transform = camera.transform; + Vector3 position2 = transform.position; + float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f))); + Vector3 vector = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z))); + Vector3 vector2 = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z))); + float magnitude = (vector - vector2).magnitude; + return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint; - _labelDummy = new GUIContent(); - _whiteTexture = new Texture2D(2, 2); - Color32[] color = new Color32[] - { - new Color32(255,255,255,255), - new Color32(255,255,255,255), - new Color32(255,255,255,255), - new Color32(255,255,255,255), - }; - _whiteTexture.SetPixels32(color); - _whiteTexture.Apply(); - } - } - private static float GetCameraZoom(Camera camera) - { - const float DEFAULT_ZOOM = 1f; - - if (camera != null) - { - return camera.orthographicSize; - } - return DEFAULT_ZOOM; + //const float DEFAULT_ZOOM = 1f; + // + //if (camera != null) + //{ + // return camera.orthographicSize; + //} + //return DEFAULT_ZOOM; //var currentDrawingSceneView = SceneView.currentDrawingSceneView; // @@ -229,6 +233,7 @@ namespace DCFApixels // //return DEFAULT_ZOOM; } + #endregion } #endregion } diff --git a/Runtime/Materials/TextBackground.mat b/Runtime/Materials/TextBackground.mat new file mode 100644 index 0000000..32b9923 --- /dev/null +++ b/Runtime/Materials/TextBackground.mat @@ -0,0 +1,135 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 8 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: TextBackground + m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0} + m_Parent: {fileID: 0} + m_ModifiedSerializedProperties: 0 + m_ValidKeywords: [] + m_InvalidKeywords: [] + m_LightmapFlags: 4 + m_EnableInstancingVariants: 0 + m_DoubleSidedGI: 0 + m_CustomRenderQueue: -1 + stringTagMap: {} + disabledShaderPasses: + - MOTIONVECTORS + m_LockedProperties: + m_SavedProperties: + serializedVersion: 3 + m_TexEnvs: + - _BaseMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _BumpMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailAlbedoMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailMask: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _DetailNormalMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _EmissionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MainTex: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _MetallicGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _OcclusionMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _ParallaxMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - _SpecGlossMap: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_Lightmaps: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_LightmapsInd: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + - unity_ShadowMasks: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Ints: [] + m_Floats: + - _AddPrecomputedVelocity: 0 + - _AlphaClip: 0 + - _AlphaToMask: 0 + - _Blend: 0 + - _BlendModePreserveSpecular: 1 + - _BumpScale: 1 + - _ClearCoatMask: 0 + - _ClearCoatSmoothness: 0 + - _Cull: 2 + - _Cutoff: 0.5 + - _DetailAlbedoMapScale: 1 + - _DetailNormalMapScale: 1 + - _DstBlend: 0 + - _DstBlendAlpha: 0 + - _EnvironmentReflections: 1 + - _GlossMapScale: 0 + - _Glossiness: 0 + - _GlossyReflections: 0 + - _Metallic: 0 + - _OcclusionStrength: 1 + - _Parallax: 0.005 + - _QueueOffset: 0 + - _ReceiveShadows: 1 + - _Smoothness: 0.5 + - _SmoothnessTextureChannel: 0 + - _SpecularHighlights: 1 + - _SrcBlend: 1 + - _SrcBlendAlpha: 1 + - _Surface: 0 + - _WorkflowMode: 1 + - _ZWrite: 1 + m_Colors: + - _BaseColor: {r: 1, g: 1, b: 1, a: 1} + - _Color: {r: 1, g: 1, b: 1, a: 1} + - _EmissionColor: {r: 0, g: 0, b: 0, a: 1} + - _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1} + m_BuildTextureStacks: [] + m_AllowLocking: 1 +--- !u!114 &1974214919826471110 +MonoBehaviour: + m_ObjectHideFlags: 11 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d0353a89b1f911e48b9e16bdc9f2e058, type: 3} + m_Name: + m_EditorClassIdentifier: + version: 9 diff --git a/Runtime/Materials/TextBackground.mat.meta b/Runtime/Materials/TextBackground.mat.meta new file mode 100644 index 0000000..98922e1 --- /dev/null +++ b/Runtime/Materials/TextBackground.mat.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 75409b93d220f694aa75eee6f4bfd840 +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 2100000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Resources/DCFApixels.DebugX/MaterialsList.prefab b/Runtime/Resources/DCFApixels.DebugX/MaterialsList.prefab index 71cb551..624affb 100644 --- a/Runtime/Resources/DCFApixels.DebugX/MaterialsList.prefab +++ b/Runtime/Resources/DCFApixels.DebugX/MaterialsList.prefab @@ -86,6 +86,92 @@ MeshRenderer: m_SortingLayer: 0 m_SortingOrder: 0 m_AdditionalVertexStreams: {fileID: 0} +--- !u!1 &549390746132496352 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 7483746332942052062} + - component: {fileID: 6763744428275037148} + - component: {fileID: 387838602774759695} + m_Layer: 0 + m_Name: TextBackground + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &7483746332942052062 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 549390746132496352} + serializedVersion: 2 + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 12.5, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 2844384060761577604} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!33 &6763744428275037148 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 549390746132496352} + m_Mesh: {fileID: 4300000, guid: 873de0939b7f76947a258a8897199a8e, type: 2} +--- !u!23 &387838602774759695 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 549390746132496352} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RayTraceProcedural: 0 + m_RayTracingAccelStructBuildFlagsOverride: 0 + m_RayTracingAccelStructBuildFlags: 1 + m_SmallMeshCulling: 1 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 75409b93d220f694aa75eee6f4bfd840, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_AdditionalVertexStreams: {fileID: 0} --- !u!1 &1299980064020930364 GameObject: m_ObjectHideFlags: 0 @@ -292,6 +378,7 @@ Transform: - {fileID: 2770005348449356163} - {fileID: 5119875421667202613} - {fileID: 1046323005297189095} + - {fileID: 7483746332942052062} m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &6563773915565914195 diff --git a/Runtime/Shaders/TextBackground.shader b/Runtime/Shaders/TextBackground.shader new file mode 100644 index 0000000..6266f5d --- /dev/null +++ b/Runtime/Shaders/TextBackground.shader @@ -0,0 +1,59 @@ +Shader "DCFApixels/DebugX/TextBackground" +{ + Properties + { + _Color ("Color", Color) = (1,1,1,1) + } + SubShader + { + Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" } + Blend SrcAlpha OneMinusSrcAlpha + ZWrite Off Fog { Mode Off } + Lighting Off + ZTest Off + + Pass + { + CGPROGRAM + + #pragma vertex vert + #pragma fragment frag + #pragma multi_compile_instancing + #include "UnityCG.cginc" + + struct appdata_t + { + float4 vertex : POSITION; + float4 color : COLOR; + UNITY_VERTEX_INPUT_INSTANCE_ID + }; + + struct v2f + { + float4 vertex : SV_POSITION; + float4 color : COLOR; + }; + + UNITY_INSTANCING_BUFFER_START(Props) + UNITY_DEFINE_INSTANCED_PROP(float4, _Color) + UNITY_INSTANCING_BUFFER_END(Props) + + float4 _DebugX_GlobalColor; + + v2f vert (appdata_t v) + { + v2f o; + UNITY_SETUP_INSTANCE_ID(v); + o.vertex = UnityObjectToClipPos(v.vertex); + o.color = v.color * UNITY_ACCESS_INSTANCED_PROP(Props, _Color) * _DebugX_GlobalColor; + return o; + } + + float4 frag (v2f i) : SV_Target + { + return i.color * float4(1, 1, 1, 2); + } + ENDCG + } + } +} \ No newline at end of file diff --git a/Runtime/Shaders/TextBackground.shader.meta b/Runtime/Shaders/TextBackground.shader.meta new file mode 100644 index 0000000..b8042f8 --- /dev/null +++ b/Runtime/Shaders/TextBackground.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 1af3913ae49e790418a8d901f8982caf +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Utils/DebugXAssets.cs b/Runtime/Utils/DebugXAssets.cs index 1a4d4ef..b7f61f5 100644 --- a/Runtime/Utils/DebugXAssets.cs +++ b/Runtime/Utils/DebugXAssets.cs @@ -47,6 +47,8 @@ namespace DCFApixels.DebugXCore public readonly Material Billboard; public readonly Material Dot; public readonly Material Wire; + + internal readonly Material TextBackground; } } } \ No newline at end of file diff --git a/Runtime/Utils/DebugXTextSettings.cs b/Runtime/Utils/DebugXTextSettings.cs index 8bba295..fb5d9f8 100644 --- a/Runtime/Utils/DebugXTextSettings.cs +++ b/Runtime/Utils/DebugXTextSettings.cs @@ -2,40 +2,44 @@ namespace DCFApixels { - public struct DebugXTextSettings + public readonly 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 static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0); + public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor(1f); - public int FontSize; - public TextAnchor TextAnchor; - public Color BackgroundColor; + public readonly int FontSize; + public readonly TextAnchor TextAnchor; + public readonly Color BackgroundColor; + public readonly float WorldSpaceScaleFactor; public bool IsHasBackground { get { return BackgroundColor.a > 0; } } - public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor) + public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor, float worldSpaceScaleFactor) { FontSize = fontSize; TextAnchor = textAnchor; BackgroundColor = backgroundColor; + WorldSpaceScaleFactor = worldSpaceScaleFactor; } public DebugXTextSettings SetSize(int fontSize) { - FontSize = fontSize; - return this; + return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor); } public DebugXTextSettings SetAnchor(TextAnchor textAnchor) { - TextAnchor = textAnchor; - return this; + return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor); } public DebugXTextSettings SetBackground(Color backgroundColor) { - BackgroundColor = backgroundColor; - return this; + return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor); + } + public DebugXTextSettings SetWorldSpaceScaleFactor(float factor) + { + return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, factor); } } } \ No newline at end of file diff --git a/Runtime/Utils/DebugXUtility.cs b/Runtime/Utils/DebugXUtility.cs index 57f3d0b..c3f4bca 100644 --- a/Runtime/Utils/DebugXUtility.cs +++ b/Runtime/Utils/DebugXUtility.cs @@ -41,7 +41,7 @@ namespace DCFApixels.DebugXCore field.SetValue(obj, meshFilter.sharedMesh); } else - { + { Debug.LogWarning(field.Name + " not found."); } } diff --git a/Samples/Sample.unity b/Samples/Sample.unity index dd247d0..608f33e 100644 --- a/Samples/Sample.unity +++ b/Samples/Sample.unity @@ -4137,7 +4137,7 @@ GameObject: m_Layer: 0 m_Name: Hello World m_TagString: Untagged - m_Icon: {fileID: 7866945982896999795, guid: 0000000000000000d000000000000000, type: 0} + m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 diff --git a/Samples/Scripts/DebugXSample_Other.cs b/Samples/Scripts/DebugXSample_Other.cs index 71c06a1..866d0f0 100644 --- a/Samples/Scripts/DebugXSample_Other.cs +++ b/Samples/Scripts/DebugXSample_Other.cs @@ -8,7 +8,6 @@ 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() @@ -29,7 +28,10 @@ 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, DebugXTextSettings.Default.SetBackground(Color.black)); + Color backgroundColor = Color.white - GetColor(Points[i]); + //backgroundColor.a = 1; + backgroundColor.a = 0.5f; + i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.Default.SetBackground(backgroundColor)); i++; DebugX.Draw(GetColor(Points[i])).Dot(Points[i].position); i++; DebugX.Draw(GetColor(Points[i])).WireDot(Points[i].position); From e41c45d73f8887e84b9a71815e951c5c3922c7a4 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 28 Feb 2025 11:20:58 +0800 Subject: [PATCH 17/17] fix --- Runtime/Materials/TextBackground.mat | 2 +- Samples/Scripts/DebugXSample_Other.cs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Runtime/Materials/TextBackground.mat b/Runtime/Materials/TextBackground.mat index 32b9923..88444a5 100644 --- a/Runtime/Materials/TextBackground.mat +++ b/Runtime/Materials/TextBackground.mat @@ -8,7 +8,7 @@ Material: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_Name: TextBackground - m_Shader: {fileID: 10755, guid: 0000000000000000f000000000000000, type: 0} + m_Shader: {fileID: 4800000, guid: 1af3913ae49e790418a8d901f8982caf, type: 3} m_Parent: {fileID: 0} m_ModifiedSerializedProperties: 0 m_ValidKeywords: [] diff --git a/Samples/Scripts/DebugXSample_Other.cs b/Samples/Scripts/DebugXSample_Other.cs index 866d0f0..b5602dd 100644 --- a/Samples/Scripts/DebugXSample_Other.cs +++ b/Samples/Scripts/DebugXSample_Other.cs @@ -29,7 +29,6 @@ namespace DCFApixels.DebugXCore.Samples 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); Color backgroundColor = Color.white - GetColor(Points[i]); - //backgroundColor.a = 1; backgroundColor.a = 0.5f; i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.Default.SetBackground(backgroundColor));