fix text background/ rework text world space scaling

This commit is contained in:
DCFApixels 2025-02-28 11:18:49 +08:00
parent 5da2eb16ee
commit 30f93a7d4c
11 changed files with 394 additions and 83 deletions

View File

@ -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<TextGizmo>
{
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<TextGizmo> RegisterNewRenderer() { return new Renderer(); }
@ -39,7 +35,7 @@ namespace DCFApixels
#region Renderer
private class Renderer : IGizmoRenderer_PostRender<TextGizmo>
{
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<TextGizmo> list, CommandBuffer cb) { }
public void PostRender(Camera camera, GizmosList<TextGizmo> 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
}

View File

@ -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

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 75409b93d220f694aa75eee6f4bfd840
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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

View File

@ -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
}
}
}

View File

@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 1af3913ae49e790418a8d901f8982caf
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

View File

@ -47,6 +47,8 @@ namespace DCFApixels.DebugXCore
public readonly Material Billboard;
public readonly Material Dot;
public readonly Material Wire;
internal readonly Material TextBackground;
}
}
}

View File

@ -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);
}
}
}

View File

@ -41,7 +41,7 @@ namespace DCFApixels.DebugXCore
field.SetValue(obj, meshFilter.sharedMesh);
}
else
{
{
Debug.LogWarning(field.Name + " not found.");
}
}

View File

@ -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

View File

@ -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<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.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);