diff --git a/Runtime/Gizmos/Text.meta b/Runtime/Gizmos/Text.meta new file mode 100644 index 0000000..22f0f23 --- /dev/null +++ b/Runtime/Gizmos/Text.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9709faba0386b5244a8ab4c9c7ce49ec +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/Gizmos/Text/DebugXTextSettings.cs b/Runtime/Gizmos/Text/DebugXTextSettings.cs index 22a45c4..ba9a3d1 100644 --- a/Runtime/Gizmos/Text/DebugXTextSettings.cs +++ b/Runtime/Gizmos/Text/DebugXTextSettings.cs @@ -12,7 +12,7 @@ namespace DCFApixels public const float SCREEN_SPACE_SCALE_FACTOR = 0f; public const float WORLD_SPACE_SCALE_FACTOR = 1f; - + public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0); public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor(); @@ -20,15 +20,15 @@ namespace DCFApixels /// Font size. Default is . /// public readonly int FontSize; - + /// /// Text alignment. Default is . /// public readonly TextAnchor TextAnchor; - + public readonly Color BackgroundColor; public readonly float WorldSpaceScaleFactor; - + // ReSharper disable once UnusedMember.Global public bool IsHasBackground => BackgroundColor.a > 0; @@ -47,7 +47,7 @@ namespace DCFApixels { return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor); } - + /// /// Sets text alignment. Default is . /// @@ -55,7 +55,7 @@ namespace DCFApixels { return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor); } - + /// /// Sets background image color behind text. Ignored if transparent. /// @@ -63,7 +63,7 @@ namespace DCFApixels { return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor); } - + /// /// Synchronizes the text scale in screen space. The text will remain the same size on the screen. /// @@ -72,7 +72,7 @@ namespace DCFApixels { return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_FACTOR); } - + /// /// Synchronizes the text scale in world space. The text will remain the same size on the scene. /// @@ -80,7 +80,7 @@ namespace DCFApixels { return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_FACTOR); } - + /// /// Allows you to control the text scale depending on the camera zoom. /// diff --git a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs index 910e542..272a8d4 100644 --- a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs +++ b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs @@ -1,17 +1,34 @@ using System.Runtime.CompilerServices; using UnityEngine; -namespace DCFApixels { - using IN = MethodImplAttribute; +namespace DCFApixels +{ using DrawHandler = DebugX.DrawHandler; - - public static class TextDrawHandlerExtensions { - private const MethodImplOptions LINE = DebugX.LINE; - - [IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text) => - drawHandler.Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default)); + using IN = MethodImplAttribute; - [IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text, DebugXTextSettings settings) => - drawHandler.Gizmo(new TextGizmo(position, text, settings)); + public static class TextDrawHandlerExtensions + { + private const MethodImplOptions LINE = DebugX.LINE; +#if DEBUG + private static bool _singleWarningToggle = true; +#endif + [IN(LINE)] + public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.Default); + [IN(LINE)] + public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings) + { + if(settings.FontSize <= float.Epsilon) + { +#if DEBUG + if (_singleWarningToggle) + { + Debug.LogWarning("Text rendering requires FontSize > 0, otherwise the text will be invisible. To avoid invalid parameters, use DebugXTextSettings.Default instead of manual instantiation."); + _singleWarningToggle = false; + } +#endif + settings = settings.SetSize(DebugXTextSettings.DEFAULT_FONT_SIZE); + } + return h.Gizmo(new TextGizmo(position, text, settings)); + } } } \ No newline at end of file diff --git a/Runtime/Gizmos/Text/TextGizmo.cs b/Runtime/Gizmos/Text/TextGizmo.cs index 5b27b50..6c70b72 100644 --- a/Runtime/Gizmos/Text/TextGizmo.cs +++ b/Runtime/Gizmos/Text/TextGizmo.cs @@ -4,11 +4,12 @@ using UnityEditor; using UnityEngine; using UnityEngine.Rendering; -namespace DCFApixels { +namespace DCFApixels +{ using IN = MethodImplAttribute; - - internal readonly struct TextGizmo : IGizmo { - private const MethodImplOptions LINE = DebugX.LINE; + internal readonly struct TextGizmo : IGizmo + { + private const MethodImplOptions LINE = MethodImplOptions.AggressiveInlining; public readonly Vector3 Position; public readonly string Text; public readonly DebugXTextSettings Settings; @@ -64,16 +65,28 @@ namespace DCFApixels { GUIStyle style = _labelStyle; 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)); + float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor); + style.fontSize = Mathf.Max(1, Mathf.FloorToInt(fontSize)); style.alignment = item.Value.Settings.TextAnchor; if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) { Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); - Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor; - Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1); - GUI.color = item.Color * DebugX.GlobalColor; + if (item.Value.Settings.IsHasBackground) + { + Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor; + if(fontSize < 1) + { + backgroundColor.a *= fontSize; + } + Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1); + } + Color color= item.Color * DebugX.GlobalColor; + if (fontSize < 1) + { + color.a *= fontSize; + } + GUI.color = color; style.Draw(rect, _labelDummy, false, false, false, false); } } @@ -104,10 +117,10 @@ namespace DCFApixels { result.background = null; return result; } - + // If calling GUI.skin directly - Unity will throw exceptions saying that GUI.skin can be called only from OnSceneGUI context. GUISkin skin = (GUISkin)typeof(GUI).GetField("s_Skin", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(null); //GUI.s_Skin - + _labelStyle = new GUIStyle(skin.label) { richText = false, @@ -190,12 +203,12 @@ namespace DCFApixels { private static float GetCameraZoom(Camera camera, Vector3 position) { 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; + Transform cameraTransform = camera.transform; + Vector3 cameraPos = cameraTransform.position; + float z = Vector3.Dot(position - cameraPos, cameraTransform.TransformDirection(new Vector3(0f, 0f, 1f))); + Vector3 pos1 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(0f, 0f, z))); + Vector3 pos2 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(1f, 0f, z))); + float magnitude = (pos1 - pos2).magnitude; return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint; } #endregion