2025-02-26 07:31:16 +08:00
|
|
|
|
using DCFApixels.DebugXCore;
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
using UnityEditor;
|
|
|
|
|
#endif
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Rendering;
|
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
namespace DCFApixels
|
|
|
|
|
{
|
2025-02-26 07:31:16 +08:00
|
|
|
|
using IN = System.Runtime.CompilerServices.MethodImplAttribute;
|
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
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));
|
|
|
|
|
|
|
|
|
|
private readonly struct TextGizmo : IGizmo<TextGizmo>
|
|
|
|
|
{
|
|
|
|
|
public readonly Vector3 Position;
|
|
|
|
|
public readonly string Text;
|
|
|
|
|
public readonly DebugXTextSettings Settings;
|
|
|
|
|
public readonly bool IsWorldSpaceScale;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
[IN(LINE)]
|
2025-02-26 23:39:50 +08:00
|
|
|
|
public TextGizmo(Vector3 position, object text, DebugXTextSettings settings, bool isWorldSpaceScale)
|
|
|
|
|
{
|
|
|
|
|
Position = position;
|
|
|
|
|
Text = text.ToString();
|
|
|
|
|
Settings = settings;
|
|
|
|
|
IsWorldSpaceScale = isWorldSpaceScale;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IGizmoRenderer<TextGizmo> RegisterNewRenderer() { return new Renderer(); }
|
|
|
|
|
|
|
|
|
|
#region Renderer
|
2025-02-27 21:19:54 +08:00
|
|
|
|
private class Renderer : IGizmoRenderer<TextGizmo>
|
2025-02-26 07:31:16 +08:00
|
|
|
|
{
|
2025-02-26 23:39:50 +08:00
|
|
|
|
private static GUIStyle _labelStyle;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
private static GUIContent _labelDummy;
|
2025-02-27 21:19:54 +08:00
|
|
|
|
private static Texture2D _whiteTexture;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
public int ExecuteOrder => default(UnlitMat).GetExecuteOrder();
|
|
|
|
|
public bool IsStaticRender => false;
|
|
|
|
|
public void Prepare(Camera camera, GizmosList<TextGizmo> list) { }
|
2025-02-27 21:19:54 +08:00
|
|
|
|
public void Render(Camera camera, GizmosList<TextGizmo> list, CommandBuffer cb)
|
|
|
|
|
{
|
|
|
|
|
Render_UnityGizmos(camera, list);
|
|
|
|
|
}
|
2025-02-26 07:31:16 +08:00
|
|
|
|
public void Render_UnityGizmos(Camera camera, GizmosList<TextGizmo> list)
|
|
|
|
|
{
|
2025-02-27 20:01:22 +08:00
|
|
|
|
if (Event.current.type != EventType.Repaint) { return; }
|
2025-02-27 21:19:54 +08:00
|
|
|
|
Color dfColor = GUI.color;
|
2025-02-27 20:01:22 +08:00
|
|
|
|
|
|
|
|
|
if (camera == null) { return; }
|
2025-02-26 23:39:50 +08:00
|
|
|
|
InitStatic();
|
|
|
|
|
var zoom = GetCameraZoom(camera);
|
2025-02-28 09:24:37 +08:00
|
|
|
|
var isSceneView = camera.name == "SceneCamera";
|
2025-02-27 21:19:54 +08:00
|
|
|
|
//Handles.BeginGUI();
|
2025-02-26 07:31:16 +08:00
|
|
|
|
foreach (ref readonly var item in list)
|
|
|
|
|
{
|
2025-02-26 23:39:50 +08:00
|
|
|
|
_labelDummy.text = item.Value.Text;
|
2025-02-27 16:46:55 +08:00
|
|
|
|
GUIStyle style = _labelStyle;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
style.fontSize = item.Value.IsWorldSpaceScale
|
|
|
|
|
? Mathf.FloorToInt(item.Value.Settings.FontSize / zoom)
|
|
|
|
|
: item.Value.Settings.FontSize;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
style.alignment = item.Value.Settings.TextAnchor;
|
2025-02-27 20:01:22 +08:00
|
|
|
|
if (!(WorldToGUIPointWithDepth(camera, item.Value.Position).z < 0f))
|
2025-02-26 07:31:16 +08:00
|
|
|
|
{
|
2025-02-27 20:01:22 +08:00
|
|
|
|
Rect rect = WorldPointToSizedRect(camera, item.Value.Position, _labelDummy, _labelStyle);
|
|
|
|
|
|
|
|
|
|
GL.PushMatrix();
|
2025-02-28 09:24:37 +08:00
|
|
|
|
GL.LoadPixelMatrix(0, Screen.width, Screen.height - (isSceneView ? 50 : 0), 0);
|
2025-02-27 20:01:22 +08:00
|
|
|
|
|
|
|
|
|
|
2025-02-27 16:46:55 +08:00
|
|
|
|
Color c = item.Value.Settings.BackgroundColor * GlobalColor;
|
|
|
|
|
GUI.color = c;
|
2025-02-28 09:16:14 +08:00
|
|
|
|
var mat = DebugXAssets.Materials.Unlit;
|
|
|
|
|
mat.SetColor(ColorPropertyID, c);
|
|
|
|
|
Graphics.DrawTexture(rect, _whiteTexture, mat);
|
2025-02-27 16:46:55 +08:00
|
|
|
|
|
|
|
|
|
GUI.color = item.Color * GlobalColor;
|
2025-02-27 20:01:22 +08:00
|
|
|
|
style.Draw(rect, _labelDummy, false, false, false, false);
|
|
|
|
|
|
2025-02-28 09:24:37 +08:00
|
|
|
|
|
2025-02-27 20:01:22 +08:00
|
|
|
|
GL.PopMatrix();
|
2025-02-26 07:31:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-27 21:19:54 +08:00
|
|
|
|
GUI.color = dfColor;
|
2025-02-28 09:16:14 +08:00
|
|
|
|
DebugXAssets.Materials.Unlit.SetColor(ColorPropertyID, Color.white);
|
2025-02-26 23:39:50 +08:00
|
|
|
|
}
|
2025-02-27 20:01:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Utils
|
|
|
|
|
public static Vector3 WorldToGUIPointWithDepth(Camera camera, Vector3 world)
|
|
|
|
|
{
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
world = Handles.matrix.MultiplyPoint(world);
|
2025-02-27 21:19:54 +08:00
|
|
|
|
#endif
|
2025-02-27 20:01:22 +08:00
|
|
|
|
Vector3 vector = camera.WorldToScreenPoint(world);
|
|
|
|
|
vector.y = camera.pixelHeight - vector.y;
|
2025-02-27 21:19:54 +08:00
|
|
|
|
Vector2 vector2 = (Vector2)(vector);
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
vector2 = EditorGUIUtility.PixelsToPoints(vector);
|
2025-02-27 20:01:22 +08:00
|
|
|
|
#endif
|
2025-02-27 21:19:54 +08:00
|
|
|
|
return new Vector3(vector2.x, vector2.y, vector.z);
|
2025-02-27 20:01:22 +08:00
|
|
|
|
}
|
|
|
|
|
public static Rect WorldPointToSizedRect(Camera camera, Vector3 position, GUIContent content, GUIStyle style)
|
2025-02-26 23:39:50 +08:00
|
|
|
|
{
|
2025-02-27 20:01:22 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void InitStatic()
|
|
|
|
|
{
|
2025-02-27 21:19:54 +08:00
|
|
|
|
if (_labelStyle == null || _labelDummy == null || _whiteTexture == null)
|
2025-02-26 23:39:50 +08:00
|
|
|
|
{
|
2025-02-27 21:19:54 +08:00
|
|
|
|
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)
|
2025-02-26 23:39:50 +08:00
|
|
|
|
{
|
|
|
|
|
richText = false,
|
2025-02-28 09:16:14 +08:00
|
|
|
|
padding = new RectOffset(1, 1, 1, 1),
|
|
|
|
|
margin = new RectOffset(0, 0, 0, 0),
|
2025-02-27 21:19:54 +08:00
|
|
|
|
normal = GenerateGUIStyleState(),
|
|
|
|
|
active = GenerateGUIStyleState(),
|
|
|
|
|
hover = GenerateGUIStyleState(),
|
|
|
|
|
focused = GenerateGUIStyleState(),
|
2025-02-26 23:39:50 +08:00
|
|
|
|
};
|
2025-02-27 21:19:54 +08:00
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
_labelDummy = new GUIContent();
|
2025-02-27 21:19:54 +08:00
|
|
|
|
|
|
|
|
|
_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),
|
|
|
|
|
};
|
2025-02-28 09:16:14 +08:00
|
|
|
|
_whiteTexture.SetPixels32(color);
|
|
|
|
|
_whiteTexture.Apply();
|
2025-02-26 08:00:42 +08:00
|
|
|
|
}
|
2025-02-26 23:39:50 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
2025-02-26 07:31:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-26 23:39:50 +08:00
|
|
|
|
#endregion
|
2025-02-26 07:31:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|