update text/fix size bug/fix code style

This commit is contained in:
DCFApixels 2025-03-05 18:50:22 +08:00
parent 9b9a52552f
commit c0a2f4e744
4 changed files with 74 additions and 36 deletions

8
Runtime/Gizmos/Text.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9709faba0386b5244a8ab4c9c7ce49ec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,17 +1,34 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using UnityEngine; using UnityEngine;
namespace DCFApixels { namespace DCFApixels
using IN = MethodImplAttribute; {
using DrawHandler = DebugX.DrawHandler; using DrawHandler = DebugX.DrawHandler;
using IN = MethodImplAttribute;
public static class TextDrawHandlerExtensions { public static class TextDrawHandlerExtensions
{
private const MethodImplOptions LINE = DebugX.LINE; private const MethodImplOptions LINE = DebugX.LINE;
#if DEBUG
[IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text) => private static bool _singleWarningToggle = true;
drawHandler.Gizmo(new TextGizmo(position, text, DebugXTextSettings.Default)); #endif
[IN(LINE)]
[IN(LINE)] public static DrawHandler Text(this DrawHandler drawHandler, Vector3 position, object text, DebugXTextSettings settings) => public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.Default);
drawHandler.Gizmo(new TextGizmo(position, text, settings)); [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));
}
} }
} }

View File

@ -4,11 +4,12 @@ using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.Rendering; using UnityEngine.Rendering;
namespace DCFApixels { namespace DCFApixels
{
using IN = MethodImplAttribute; using IN = MethodImplAttribute;
internal readonly struct TextGizmo : IGizmo<TextGizmo>
internal readonly struct TextGizmo : IGizmo<TextGizmo> { {
private const MethodImplOptions LINE = DebugX.LINE; private const MethodImplOptions LINE = MethodImplOptions.AggressiveInlining;
public readonly Vector3 Position; public readonly Vector3 Position;
public readonly string Text; public readonly string Text;
public readonly DebugXTextSettings Settings; public readonly DebugXTextSettings Settings;
@ -64,16 +65,28 @@ namespace DCFApixels {
GUIStyle style = _labelStyle; GUIStyle style = _labelStyle;
var zoom = GetCameraZoom(camera, item.Value.Position); var zoom = GetCameraZoom(camera, item.Value.Position);
float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceScaleFactor);
style.fontSize = Mathf.FloorToInt(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; style.alignment = item.Value.Settings.TextAnchor;
if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f)) if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f))
{ {
Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle); Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle);
if (item.Value.Settings.IsHasBackground)
{
Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor; 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); Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1);
GUI.color = item.Color * DebugX.GlobalColor; }
Color color= item.Color * DebugX.GlobalColor;
if (fontSize < 1)
{
color.a *= fontSize;
}
GUI.color = color;
style.Draw(rect, _labelDummy, false, false, false, false); style.Draw(rect, _labelDummy, false, false, false, false);
} }
} }
@ -190,12 +203,12 @@ namespace DCFApixels {
private static float GetCameraZoom(Camera camera, Vector3 position) private static float GetCameraZoom(Camera camera, Vector3 position)
{ {
position = Handles.matrix.MultiplyPoint(position); position = Handles.matrix.MultiplyPoint(position);
Transform transform = camera.transform; Transform cameraTransform = camera.transform;
Vector3 position2 = transform.position; Vector3 cameraPos = cameraTransform.position;
float z = Vector3.Dot(position - position2, transform.TransformDirection(new Vector3(0f, 0f, 1f))); float z = Vector3.Dot(position - cameraPos, cameraTransform.TransformDirection(new Vector3(0f, 0f, 1f)));
Vector3 vector = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(0f, 0f, z))); Vector3 pos1 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(0f, 0f, z)));
Vector3 vector2 = camera.WorldToScreenPoint(position2 + transform.TransformDirection(new Vector3(1f, 0f, z))); Vector3 pos2 = camera.WorldToScreenPoint(cameraPos + cameraTransform.TransformDirection(new Vector3(1f, 0f, z)));
float magnitude = (vector - vector2).magnitude; float magnitude = (pos1 - pos2).magnitude;
return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint; return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint;
} }
#endregion #endregion