diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index d0b093a..68749dc 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -12,9 +12,9 @@ using UnityEngine.LowLevel; using UnityEngine.PlayerLoop; using Unity.Collections.LowLevel.Unsafe; using DCFApixels.DebugXCore.Internal; +using System.Linq; #if UNITY_EDITOR using UnityEditor; -using System.Linq; #endif namespace DCFApixels @@ -53,7 +53,7 @@ namespace DCFApixels eventToClean = null; foreach (var delegateItem in validDelegates) { - eventToClean += (OnDrawGizmoHandler)delegateItem; + eventToClean += (OnDrawGizmoHandler)delegateItem; } } } @@ -63,7 +63,7 @@ namespace DCFApixels CleanupEvent(ref _onDrawGizmo); } internal static void InvokeOnDrawGizmo(Camera camera) - { + { _onDrawGizmo(camera); } } diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index 6f88e85..ab37c78 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -1,5 +1,4 @@ -//#undef DEBUG -using DCFApixels.DebugXCore; +using DCFApixels.DebugXCore; using DCFApixels.DebugXCore.Internal; using System.Runtime.InteropServices; using Unity.Collections.LowLevel.Unsafe; @@ -278,9 +277,11 @@ namespace DCFApixels _material = material; _matrices = PinnedArray.Pin(DummyArray.Get()); _colors = PinnedArray.Pin(DummyArray.Get()); - +#if UNITY_EDITOR AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload; +#endif } +#if UNITY_EDITOR private void AssemblyReloadEvents_beforeAssemblyReload() { AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload; @@ -289,6 +290,7 @@ namespace DCFApixels _colors.Dispose(); _gizmos.Dispose(); } +#endif public void Prepare(GizmosList rawList) { var list = rawList.As(); @@ -391,9 +393,11 @@ namespace DCFApixels _materialPropertyBlock = new MaterialPropertyBlock(); _drawDatas = PinnedArray.Pin(DummyArray.Get()); _enableInstancing = IsSupportsComputeShaders && _material.GetMaterial().enableInstancing; - +#if UNITY_EDITOR AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload; +#endif } +#if UNITY_EDITOR private void AssemblyReloadEvents_beforeAssemblyReload() { AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload; @@ -403,7 +407,7 @@ namespace DCFApixels _drawDatas.Dispose(); _gizmos.Dispose(); } - +#endif public virtual int ExecuteOrder => _material.GetExecuteOrder(); public virtual bool IsStaticRender => true; protected void Prepare(GizmosList rawList) @@ -412,8 +416,8 @@ namespace DCFApixels _prepareCount = list.Count; var items = list.Items; var count = list.Count; - - if (_buffersLength < count) + + if (_buffersLength < count) { int capacity = DebugXUtility.NextPow2(count); _drawDatas.Dispose(); @@ -520,9 +524,11 @@ namespace DCFApixels _materialPropertyBlock = new MaterialPropertyBlock(); _drawDatas = PinnedArray.Pin(DummyArray.Get()); _enableInstancing = IsSupportsComputeShaders && _material.GetMaterial().enableInstancing; - +#if UNITY_EDITOR AssemblyReloadEvents.beforeAssemblyReload += AssemblyReloadEvents_beforeAssemblyReload; +#endif } +#if UNITY_EDITOR private void AssemblyReloadEvents_beforeAssemblyReload() { AssemblyReloadEvents.beforeAssemblyReload -= AssemblyReloadEvents_beforeAssemblyReload; @@ -532,6 +538,7 @@ namespace DCFApixels _drawDatas.Dispose(); _gizmos.Dispose(); } +#endif public virtual int ExecuteOrder => _material.GetExecuteOrder() - 1; public virtual bool IsStaticRender => true; public void Prepare(GizmosList rawList) diff --git a/Runtime/Gizmos/DebugX.dots.cs b/Runtime/Gizmos/DebugX.dots.cs index f73e35e..edb6470 100644 --- a/Runtime/Gizmos/DebugX.dots.cs +++ b/Runtime/Gizmos/DebugX.dots.cs @@ -1,8 +1,5 @@ -//#undef DEBUG -using DCFApixels.DebugXCore; -using System; +using DCFApixels.DebugXCore; using UnityEngine; -using UnityEngine.Rendering; namespace DCFApixels { diff --git a/Runtime/Gizmos/DebugX.lines.cs b/Runtime/Gizmos/DebugX.lines.cs index f3f02ad..78d835e 100644 --- a/Runtime/Gizmos/DebugX.lines.cs +++ b/Runtime/Gizmos/DebugX.lines.cs @@ -1,5 +1,4 @@ -//#undef DEBUG -using DCFApixels.DebugXCore; +using DCFApixels.DebugXCore; using System; using System.Collections.Generic; using UnityEngine; @@ -27,7 +26,7 @@ namespace DCFApixels } [IN(LINE)] public DrawHandler Lines(List points) => Lines(points, 0, points.Count); [IN(LINE)] public DrawHandler Lines(List points, int length) => Lines(points, 0, length); - [IN(LINE)] + [IN(LINE)] public DrawHandler Lines(List points, int startIndex, int length) { for (int i = startIndex, iMax = startIndex + (length & ~1); i < iMax;) @@ -69,7 +68,7 @@ namespace DCFApixels [IN(LINE)] public DrawHandler LineStrip(List points, int startIndex, int length) { - for (int i = startIndex, iMax = startIndex + length; i < iMax;) + for (int i = startIndex, iMax = startIndex + length - 1; i < iMax;) { Line(points[i], points[++i]); } @@ -147,7 +146,7 @@ namespace DCFApixels [IN(LINE)] public DrawHandler LineStrip(List points, int startIndex, int length) { - for (int i = startIndex, iMax = startIndex + length; i < iMax;) + for (int i = startIndex, iMax = startIndex + length - 1; i < iMax;) { Line(points[i], points[++i]); } diff --git a/Runtime/Gizmos/DebugX.primitives.cs b/Runtime/Gizmos/DebugX.primitives.cs index 891a912..f70ac70 100644 --- a/Runtime/Gizmos/DebugX.primitives.cs +++ b/Runtime/Gizmos/DebugX.primitives.cs @@ -1,5 +1,4 @@ -//#undef DEBUG -using DCFApixels.DebugXCore; +using DCFApixels.DebugXCore; using System; using UnityEngine; using UnityEngine.Rendering; @@ -409,7 +408,7 @@ namespace DCFApixels //} Vector3 halfSize = size / 2f; - + Vector3* vertices = stackalloc Vector3[] { new Vector3(-halfSize.x, -halfSize.y, -halfSize.z), // 0 @@ -421,12 +420,12 @@ namespace DCFApixels new Vector3(halfSize.x, halfSize.y, halfSize.z), // 6 new Vector3(-halfSize.x, halfSize.y, halfSize.z), // 7 }; - + for (int i = 0; i < 8; i++) { vertices[i] = rotation * vertices[i] + position; } - + Vector3 up = rotation * Vector3.up * (size.y / cells.y); for (int i = 0; i <= cells.y; i++) { @@ -454,7 +453,7 @@ namespace DCFApixels Line(vertices[1] + pos, vertices[0] + pos); Line(vertices[0] + pos, vertices[4] + pos); } - + return this; } #endregion diff --git a/Runtime/Gizmos/DebugX.raycasts.cs b/Runtime/Gizmos/DebugX.raycasts.cs index b505cea..c240dd4 100644 --- a/Runtime/Gizmos/DebugX.raycasts.cs +++ b/Runtime/Gizmos/DebugX.raycasts.cs @@ -1,5 +1,4 @@ -//#undef DEBUG -using DCFApixels.DebugXCore.Internal; +using DCFApixels.DebugXCore.Internal; using UnityEngine; namespace DCFApixels diff --git a/Runtime/Gizmos/DebugX.text.cs b/Runtime/Gizmos/DebugX.text.cs deleted file mode 100644 index 315b4f2..0000000 --- a/Runtime/Gizmos/DebugX.text.cs +++ /dev/null @@ -1,244 +0,0 @@ -using DCFApixels.DebugXCore; -using UnityEngine; -using UnityEngine.Rendering; -#if UNITY_EDITOR -using UnityEditor; -#endif - -namespace DCFApixels -{ - using IN = System.Runtime.CompilerServices.MethodImplAttribute; - - public static partial class DebugX - { - public readonly partial struct DrawHandler - { - #region Text - [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 - { - public readonly Vector3 Position; - public readonly string Text; - public readonly DebugXTextSettings Settings; - [IN(LINE)] - public TextGizmo(Vector3 position, object text, DebugXTextSettings settings) - { - Position = position; - Text = text.ToString(); - Settings = settings; - } - - public IGizmoRenderer RegisterNewRenderer() { return new Renderer(); } - - #region Renderer - private class Renderer : IGizmoRenderer_PostRender - { - private static GUIStyle _labelStyle; - private static GUIContent _labelDummy; - private static Texture2D _whiteTexture; - public int ExecuteOrder => default(UnlitMat).GetExecuteOrder(); - public bool IsStaticRender => false; - public void Prepare(Camera camera, GizmosList list) { } - public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } - public void PostRender(Camera camera, GizmosList list) - { - if (camera == null) { return; } - if (Event.current.type != EventType.Repaint) { return; } - Color dfColor = GUI.color; - InitStatic(); - bool isSceneView = false; - var backgroundMaterial = DebugXAssets.Materials.TextBackground; - -#if UNITY_EDITOR - //TODO костыльный вариант, нужно поискать более надежный способ определить рендер SceneView - isSceneView = camera.name == "SceneCamera"; -#endif - - if (isSceneView) - { -#if UNITY_EDITOR - Handles.BeginGUI(); -#endif - } - else - { - GL.PushMatrix(); - GL.LoadPixelMatrix(0, Screen.width, Screen.height, 0); - } - foreach (ref readonly var item in list) - { - _labelDummy.text = item.Value.Text; - 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)); - - 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 * GlobalColor; - Graphics.DrawTexture(rect, _whiteTexture, new Rect(0, 0, 1, 1), 0, 0, 0, 0, backgroundColor, backgroundMaterial, -1); - GUI.color = item.Color * GlobalColor; - style.Draw(rect, _labelDummy, false, false, false, false); - } - } - GUI.color = dfColor; - backgroundMaterial.SetColor(ColorPropertyID, Color.white); - - if (isSceneView) - { -#if UNITY_EDITOR - Handles.EndGUI(); -#endif - } - else - { - GL.PopMatrix(); - } - } - - #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) - { -#if UNITY_EDITOR - world = Handles.matrix.MultiplyPoint(world); -#endif - Vector3 vector = camera.WorldToScreenPoint(world); - vector.y = camera.pixelHeight - vector.y; - Vector2 vector2 = (Vector2)(vector); -#if UNITY_EDITOR - vector2 = EditorGUIUtility.PixelsToPoints(vector); -#endif - return new Vector3(vector2.x, vector2.y, vector.z); - } - public static Rect WorldPointToSizedRect(Camera camera, Vector3 position, GUIContent content, GUIStyle style) - { - 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); - } - private static float GetCameraZoom(Camera camera, Vector3 position) - { -#if UNITY_EDITOR - position = Handles.matrix.MultiplyPoint(position); -#endif - 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; - return 80f / Mathf.Max(magnitude, 0.0001f); - - - //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; - } - #endregion - } - #endregion - } - - #endregion - } - } -} \ No newline at end of file diff --git a/Runtime/Gizmos/DebugX.text.cs.meta b/Runtime/Gizmos/DebugX.text.cs.meta deleted file mode 100644 index baa2bda..0000000 --- a/Runtime/Gizmos/DebugX.text.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: c465dc7f5c78c9549a063ecfc5e8326d \ No newline at end of file diff --git a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs index 272a8d4..36235cf 100644 --- a/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs +++ b/Runtime/Gizmos/Text/TextDrawHandlerExtensions.cs @@ -17,7 +17,7 @@ namespace DCFApixels [IN(LINE)] public static DrawHandler Text(this DrawHandler h, Vector3 position, object text, DebugXTextSettings settings) { - if(settings.FontSize <= float.Epsilon) + if (settings.FontSize <= float.Epsilon) { #if DEBUG if (_singleWarningToggle) diff --git a/Runtime/Gizmos/Text/TextGizmo.cs b/Runtime/Gizmos/Text/TextGizmo.cs index 6c70b72..903e2a5 100644 --- a/Runtime/Gizmos/Text/TextGizmo.cs +++ b/Runtime/Gizmos/Text/TextGizmo.cs @@ -1,8 +1,10 @@ using DCFApixels.DebugXCore; using System.Runtime.CompilerServices; -using UnityEditor; using UnityEngine; using UnityEngine.Rendering; +//#if UNITY_EDITOR +using UnityEditor; +//#endif namespace DCFApixels { @@ -75,13 +77,13 @@ namespace DCFApixels if (item.Value.Settings.IsHasBackground) { Color backgroundColor = item.Value.Settings.BackgroundColor * DebugX.GlobalColor; - if(fontSize < 1) + 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; + Color color = item.Color * DebugX.GlobalColor; if (fontSize < 1) { color.a *= fontSize; @@ -202,14 +204,20 @@ namespace DCFApixels } private static float GetCameraZoom(Camera camera, Vector3 position) { +#if UNITY_EDITOR position = Handles.matrix.MultiplyPoint(position); +#endif 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; +#if UNITY_EDITOR return 80f / Mathf.Max(magnitude, 0.0001f) * EditorGUIUtility.pixelsPerPoint; +#else + return 80f / Mathf.Max(magnitude, 0.0001f); +#endif } #endregion } diff --git a/Runtime/Internal/PinnedArray.cs b/Runtime/Internal/PinnedArray.cs index 411c347..33c6511 100644 --- a/Runtime/Internal/PinnedArray.cs +++ b/Runtime/Internal/PinnedArray.cs @@ -1,5 +1,4 @@ #undef DEBUG - using System; using Unity.Collections.LowLevel.Unsafe; diff --git a/package.json b/package.json index e7cc763..58281ee 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "displayName": "DebugX", "description": "", "unity": "2021.3", - "version": "0.8.5", + "version": "0.8.7", "repository": { "type": "git", "url": "https://github.com/DCFApixels/DebugX.git"