update text gizmo

This commit is contained in:
Mikhail 2025-12-01 17:39:16 +08:00
parent c583d28079
commit e23fcbc74b
6 changed files with 80 additions and 37 deletions

View File

@ -1,6 +1,7 @@
#if DISABLE_DEBUG #if DISABLE_DEBUG
#undef DEBUG #undef DEBUG
#endif #endif
using System;
using UnityEngine; using UnityEngine;
namespace DCFApixels namespace DCFApixels
@ -8,80 +9,72 @@ namespace DCFApixels
/// <summary> /// <summary>
/// All additional settings for text rendering are stored here. /// All additional settings for text rendering are stored here.
/// </summary> /// </summary>
public readonly struct DebugXTextSettings public struct DebugXTextSettings
{ {
public const TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.MiddleLeft; public const TextAnchor DEFAULT_TEXT_ANCHOR = TextAnchor.MiddleLeft;
public const int DEFAULT_FONT_SIZE = 16; public const int DEFAULT_FONT_SIZE = 16;
public const float SCREEN_SPACE_SCALE_BLEND = 0f;
public const float WORLD_SPACE_SCALE_BLEND = 1f;
public const float SCREEN_SPACE_SCALE_FACTOR = 0f; public static readonly DebugXTextSettings ScreenSpace = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, Color.clear, 0);
public const float WORLD_SPACE_SCALE_FACTOR = 1f; public static readonly DebugXTextSettings WorldSpace = ScreenSpace.InWorldSpace();
public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0); /// <summary> Font size. Default is <see cref="DEFAULT_FONT_SIZE" />. </summary>
public static readonly DebugXTextSettings WorldSpaceScale = Default.SetWorldSpaceScaleFactor(); public int FontSize;
/// <summary> Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />. </summary>
/// <summary> public TextAnchor TextAnchor;
/// Font size. Default is <see cref="DEFAULT_FONT_SIZE" />. public Color BackgroundColor;
/// </summary> public float WorldSpaceBlendMultiplier;
public readonly int FontSize;
/// <summary>
/// Text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary>
public readonly TextAnchor TextAnchor;
public readonly Color BackgroundColor;
public readonly float WorldSpaceScaleFactor;
// ReSharper disable once UnusedMember.Global // ReSharper disable once UnusedMember.Global
public bool IsHasBackground => BackgroundColor.a > 0; public bool IsHasBackground { get { return BackgroundColor.a > 0; } }
public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor, float worldSpaceBlendMultiplier)
public DebugXTextSettings(int fontSize, TextAnchor textAnchor, Color backgroundColor, float worldSpaceScaleFactor)
{ {
FontSize = fontSize; FontSize = fontSize;
TextAnchor = textAnchor; TextAnchor = textAnchor;
BackgroundColor = backgroundColor; BackgroundColor = backgroundColor;
WorldSpaceScaleFactor = worldSpaceScaleFactor; WorldSpaceBlendMultiplier = worldSpaceBlendMultiplier;
} }
/// <summary> /// <summary>
/// Set font size. Default is <see cref="DEFAULT_FONT_SIZE" />. /// Set font size. Default is <see cref="DEFAULT_FONT_SIZE" />.
/// </summary> /// </summary>
public DebugXTextSettings SetSize(int fontSize) public DebugXTextSettings Size(int fontSize)
{ {
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
} }
/// <summary> /// <summary>
/// Sets text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />. /// Sets text alignment. Default is <see cref="DEFAULT_TEXT_ANCHOR" />.
/// </summary> /// </summary>
public DebugXTextSettings SetAnchor(TextAnchor textAnchor) public DebugXTextSettings Anchor(TextAnchor textAnchor)
{ {
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
} }
/// <summary> /// <summary>
/// Sets background image color behind text. Ignored if transparent. /// Sets background image color behind text. Ignored if transparent.
/// </summary> /// </summary>
public DebugXTextSettings SetBackground(Color backgroundColor) public DebugXTextSettings Background(Color backgroundColor)
{ {
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceScaleFactor); return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceBlendMultiplier);
} }
/// <summary> /// <summary>
/// Synchronizes the text scale in screen space. The text will remain the same size on the screen. /// Synchronizes the text scale in screen space. The text will remain the same size on the screen.
/// </summary> /// </summary>
// ReSharper disable once UnusedMember.Global // ReSharper disable once UnusedMember.Global
public DebugXTextSettings SetScreenSpaceScaleFactor() public DebugXTextSettings InScreenSpace()
{ {
return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_FACTOR); return ScreenWorldSpaceBlend(SCREEN_SPACE_SCALE_BLEND);
} }
/// <summary> /// <summary>
/// Synchronizes the text scale in world space. The text will remain the same size on the scene. /// Synchronizes the text scale in world space. The text will remain the same size on the scene.
/// </summary> /// </summary>
public DebugXTextSettings SetWorldSpaceScaleFactor() public DebugXTextSettings InWorldSpace()
{ {
return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_FACTOR); return ScreenWorldSpaceBlend(WORLD_SPACE_SCALE_BLEND);
} }
/// <summary> /// <summary>
@ -93,9 +86,59 @@ namespace DCFApixels
/// 1 - world space<br /> /// 1 - world space<br />
/// Values in between [0.00 - 1.00] blend these spaces together. /// Values in between [0.00 - 1.00] blend these spaces together.
/// </param> /// </param>
public DebugXTextSettings ScreenWorldSpaceBlend(float t)
{
return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, t);
}
#region Obsolete
[Obsolete("Use " + nameof(WorldSpaceBlendMultiplier))]
public float WorldSpaceScaleFactor
{
get => WorldSpaceBlendMultiplier;
set => WorldSpaceBlendMultiplier = value;
}
[Obsolete("Use " + nameof(ScreenSpace))]
public static readonly DebugXTextSettings Default = new DebugXTextSettings(DEFAULT_FONT_SIZE, DEFAULT_TEXT_ANCHOR, default, 0);
[Obsolete("Use " + nameof(WorldSpace))]
public static readonly DebugXTextSettings WorldSpaceScale = Default.InWorldSpace();
[Obsolete("Use " + nameof(SCREEN_SPACE_SCALE_BLEND))]
public const float SCREEN_SPACE_SCALE_FACTOR = 0f;
[Obsolete("Use " + nameof(WORLD_SPACE_SCALE_BLEND))]
public const float WORLD_SPACE_SCALE_FACTOR = 1f;
[Obsolete("Use " + nameof(Size))]
public DebugXTextSettings SetSize(int fontSize)
{
return new DebugXTextSettings(fontSize, TextAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(Anchor))]
public DebugXTextSettings SetAnchor(TextAnchor textAnchor)
{
return new DebugXTextSettings(FontSize, textAnchor, BackgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(Background))]
public DebugXTextSettings SetBackground(Color backgroundColor)
{
return new DebugXTextSettings(FontSize, TextAnchor, backgroundColor, WorldSpaceBlendMultiplier);
}
[Obsolete("Use " + nameof(InScreenSpace))]
public DebugXTextSettings SetScreenSpaceScaleFactor()
{
return SetCustomSpaceScaleFactor(SCREEN_SPACE_SCALE_BLEND);
}
[Obsolete("Use " + nameof(InWorldSpace))]
public DebugXTextSettings SetWorldSpaceScaleFactor()
{
return SetCustomSpaceScaleFactor(WORLD_SPACE_SCALE_BLEND);
}
[Obsolete("Use " + nameof(ScreenWorldSpaceBlend))]
public DebugXTextSettings SetCustomSpaceScaleFactor(float factor) public DebugXTextSettings SetCustomSpaceScaleFactor(float factor)
{ {
return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, factor); return new DebugXTextSettings(FontSize, TextAnchor, BackgroundColor, factor);
} }
#endregion
} }
} }

View File

@ -16,7 +16,7 @@ namespace DCFApixels
private static bool _singleWarningToggle = true; private static bool _singleWarningToggle = true;
#endif #endif
[IN(LINE)] [IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.Default); public static DrawHandler Text(this DrawHandler h, Vector3 position, object text) => h.Text(position, text, DebugXTextSettings.ScreenSpace);
[IN(LINE)] [IN(LINE)]
public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings) public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings)
{ {

View File

@ -77,7 +77,7 @@ 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); float fontSize = Mathf.Lerp(item.Value.Settings.FontSize, item.Value.Settings.FontSize / zoom, item.Value.Settings.WorldSpaceBlendMultiplier);
style.fontSize = Mathf.Max(1, Mathf.FloorToInt(fontSize)); style.fontSize = Mathf.Max(1, Mathf.FloorToInt(fontSize));
style.alignment = item.Value.Settings.TextAnchor; style.alignment = item.Value.Settings.TextAnchor;

View File

@ -30,7 +30,7 @@ namespace DCFApixels.DebugXCore.Samples
i++; DebugX.Draw(GetColor(Points[i])).BillboardCross(Points[i].position, Points[i].localScale.x); i++; DebugX.Draw(GetColor(Points[i])).BillboardCross(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])).BillboardCircle(Points[i].position, Points[i].localScale.x * RADIUS_M);
i++; DebugX.Draw(GetColor(Points[i])).WireMesh<SphereMesh>(Points[i].position, Points[i].rotation, Points[i].localScale * RADIUS_M); i++; DebugX.Draw(GetColor(Points[i])).WireMesh<SphereMesh>(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.WorldSpaceScale.SetSize(26).SetBackground(TextBackgroundColor)); i++; DebugX.Draw(GetColor(Points[i])).Text(Points[i].position, Points[i].name, DebugXTextSettings.WorldSpace.SetSize(26).SetBackground(TextBackgroundColor));
i++; DebugX.Draw(GetColor(Points[i])).Dot(Points[i].position); i++; DebugX.Draw(GetColor(Points[i])).Dot(Points[i].position);
i++; DebugX.Draw(GetColor(Points[i])).WireDot(Points[i].position); i++; DebugX.Draw(GetColor(Points[i])).WireDot(Points[i].position);

View File

@ -56,7 +56,7 @@ namespace DCFApixels.DebugXCore.Samples
hit = Physics2D.CapsuleCast(ray.origin, point.localScale, CapsuleDirection2D.Vertical, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue); hit = Physics2D.CapsuleCast(ray.origin, point.localScale, CapsuleDirection2D.Vertical, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
DebugX.Draw(GetColor(point)).CapsuleCast2D(ray, point.eulerAngles.z, point.localScale, CapsuleDirection2D.Vertical, hit); DebugX.Draw(GetColor(point)).CapsuleCast2D(ray, point.eulerAngles.z, point.localScale, CapsuleDirection2D.Vertical, hit);
#else #else
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter)); DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpace.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
#endif #endif
if (Application.isPlaying && RotatedTransform) if (Application.isPlaying && RotatedTransform)

View File

@ -58,7 +58,7 @@ namespace DCFApixels.DebugXCore.Samples
Physics.CapsuleCast(point1, point2, point.localScale.x * RADIUS_M, ray.direction, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal); Physics.CapsuleCast(point1, point2, point.localScale.x * RADIUS_M, ray.direction, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).CapsuleCast(point1, point2, ray.direction, point.localScale.x * RADIUS_M, hit); DebugX.Draw(GetColor(point)).CapsuleCast(point1, point2, ray.direction, point.localScale.x * RADIUS_M, hit);
#else #else
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS3D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter)); DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS3D\" define", DebugXTextSettings.WorldSpace.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
#endif #endif
if (Application.isPlaying && RotatedTransform) if (Application.isPlaying && RotatedTransform)