From 8743b77dfa7520f77c4d62083d5e41550884df9c Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 24 Feb 2025 13:16:30 +0800 Subject: [PATCH] polishing --- Runtime/Consts.cs | 8 +++- Runtime/DebugX.cs | 31 ++++++-------- Runtime/DebugX.globals.cs.meta | 2 + Runtime/Gizmos/DebugX.base.cs | 14 +++---- Runtime/Gizmos/DebugX.lines.cs | 3 +- Runtime/Gizmos/DebugX.primitives.cs | 3 +- Runtime/Shaders/HandlesWire.shader | 2 +- Runtime/Utils/DebugXUtility.cs | 57 +++++++++++++++++++++++-- Runtime/Utils/StaticDataLoader.cs | 58 -------------------------- Runtime/Utils/StaticDataLoader.cs.meta | 2 - Samples/DebugX.Samples.asmdef | 16 +++++++ Samples/DebugX.Samples.asmdef.meta | 7 ++++ 12 files changed, 107 insertions(+), 96 deletions(-) create mode 100644 Runtime/DebugX.globals.cs.meta delete mode 100644 Runtime/Utils/StaticDataLoader.cs delete mode 100644 Runtime/Utils/StaticDataLoader.cs.meta create mode 100644 Samples/DebugX.Samples.asmdef create mode 100644 Samples/DebugX.Samples.asmdef.meta diff --git a/Runtime/Consts.cs b/Runtime/Consts.cs index 1df22f0..ab8686e 100644 --- a/Runtime/Consts.cs +++ b/Runtime/Consts.cs @@ -22,8 +22,14 @@ namespace DCFApixels private readonly static bool IsSupportsComputeShaders = SystemInfo.supportsComputeShaders; public const float IMMEDIATE_DURATION = -1; - } + private enum PauseStateX + { + Unpaused = 0, + PreUnpaused = 1, //нужно чтобы отщелкунть паузу с задержкой в один тик + Paused = 2, + } + } public enum DebugXLine { Default, diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 9760607..3392e24 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -12,7 +12,6 @@ using UnityEngine.LowLevel; using UnityEngine.PlayerLoop; using Unity.Collections.LowLevel.Unsafe; using DCFApixels.DebugXCore.Internal; -using static DCFApixels.DebugXCore.DebugXUtility; #if UNITY_EDITOR using UnityEditor; #endif @@ -47,12 +46,6 @@ namespace DCFApixels { _isCameraContext = false; } - private enum PauseStateX - { - Unpaused = 0, - PreUnpaused = 1, // - Paused = 2, - } #if UNITY_EDITOR private static void EditorApplication_pauseStateChanged(PauseState obj) { @@ -66,8 +59,8 @@ namespace DCFApixels { InitGlobals(); - Meshes = StaticDataLoader.Load(new MeshesList(), $"DCFApixels.DebugX/MeshesList"); - Materials = StaticDataLoader.Load(new MaterialsList(), $"DCFApixels.DebugX/MaterialsList"); + Meshes = DebugXUtility.LoadStaticData(new MeshesList(), $"DCFApixels.DebugX/MeshesList"); + Materials = DebugXUtility.LoadStaticData(new MaterialsList(), $"DCFApixels.DebugX/MaterialsList"); IsSRP = GraphicsSettings.currentRenderPipeline != null; @@ -308,7 +301,7 @@ namespace DCFApixels _lastEditorTicks = _editorTicks; } - if (IsGizmosRender()) + if (DebugXUtility.IsGizmosRender()) { RenderContextController.StaicContextController.Prepare(); RenderContextController.StaicContextController.Render(cbExecutor); @@ -512,7 +505,7 @@ namespace DCFApixels } private void TypeCode_OnAddNewID(int count) { - Array.Resize(ref _buffersMap, NextPow2(count)); + Array.Resize(ref _buffersMap, DebugXUtility.NextPow2(count)); } @@ -642,6 +635,7 @@ namespace DCFApixels public void Prepare(Camera camera, GizmosList list) { } public void Render(Camera camera, GizmosList list, CommandBuffer cb) { } } + private static readonly bool _isUnmanaged = UnsafeUtility.IsUnmanaged(typeof(T)); private StructList> _gizmos = new StructList>(32); private readonly string _debugName; @@ -655,17 +649,20 @@ namespace DCFApixels private readonly bool _isStatic; #if DEV_MODE - private static readonly Unity.Profiling.ProfilerMarker _timerMarker = new Unity.Profiling.ProfilerMarker($"{GetGenericTypeName_Internal(typeof(T), 3, false)}.{nameof(UpdateTimer)}"); - private static readonly Unity.Profiling.ProfilerMarker _prepareMarker = new Unity.Profiling.ProfilerMarker($"{GetGenericTypeName_Internal(typeof(T), 3, false)}.{nameof(Prepare)}"); - private static readonly Unity.Profiling.ProfilerMarker _renderMarker = new Unity.Profiling.ProfilerMarker($"{GetGenericTypeName_Internal(typeof(T), 3, false)}.{nameof(Render)}"); + private static readonly Unity.Profiling.ProfilerMarker _timerMarker = new Unity.Profiling.ProfilerMarker($"{DebugXUtility.GetGenericTypeName(typeof(T), 3, false)}.{nameof(UpdateTimer)}"); + private static readonly Unity.Profiling.ProfilerMarker _prepareMarker = new Unity.Profiling.ProfilerMarker($"{DebugXUtility.GetGenericTypeName(typeof(T), 3, false)}.{nameof(Prepare)}"); + private static readonly Unity.Profiling.ProfilerMarker _renderMarker = new Unity.Profiling.ProfilerMarker($"{DebugXUtility.GetGenericTypeName(typeof(T), 3, false)}.{nameof(Render)}"); #endif + public override int ExecuteOrder { get { return _renderer.ExecuteOrder; } } + + public GizmosBuffer(RenderContext context, CommandBuffer cb) { Type type = typeof(T); _context = context; _staticCommandBuffer = cb; - _debugName = GetGenericTypeName_Internal(type, 3, false); + _debugName = DebugXUtility.GetGenericTypeName(type, 3, false); //_dynamicCommandBuffer = new CommandBuffer(); //_dynamicCommandBuffer.name = _debugName; @@ -696,10 +693,6 @@ namespace DCFApixels } } - public override int ExecuteOrder { get { return _renderer.ExecuteOrder; } } - - - private static readonly bool _isUnmanaged = UnsafeUtility.IsUnmanaged(typeof(T)); public sealed override int UpdateTimer(float deltaTime) { _staticCommandBuffer.Clear(); diff --git a/Runtime/DebugX.globals.cs.meta b/Runtime/DebugX.globals.cs.meta new file mode 100644 index 0000000..ff658da --- /dev/null +++ b/Runtime/DebugX.globals.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: b42093f02e7c98c4395413fe24c0784b \ No newline at end of file diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index 9d952c2..4a9f30a 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -4,9 +4,7 @@ using Unity.Collections.LowLevel.Unsafe; using Unity.Jobs; using UnityEngine; using UnityEngine.Rendering; -using static DCFApixels.DebugXCore.DebugXUtility; using DCFApixels.DebugXCore.Internal; - #if UNITY_EDITOR using UnityEditor; #endif @@ -346,8 +344,8 @@ namespace DCFApixels _matrices.Dispose(); _colors.Dispose(); } - _matrices = PinnedArray.Pin(new Matrix4x4[NextPow2(count)]); - _colors = PinnedArray.Pin(new Color[NextPow2(count)]).As(); + _matrices = PinnedArray.Pin(new Matrix4x4[DebugXUtility.NextPow2(count)]); + _colors = PinnedArray.Pin(new Color[DebugXUtility.NextPow2(count)]).As(); _buffersLength = count; } if (ReferenceEquals(_gizmos.Array, items) == false) @@ -446,8 +444,8 @@ namespace DCFApixels { _matrices.Dispose(); _colors.Dispose(); - _matrices = PinnedArray.Pin(new Matrix4x4[NextPow2(count)]); - _colors = PinnedArray.Pin(new Vector4[NextPow2(count)]); + _matrices = PinnedArray.Pin(new Matrix4x4[DebugXUtility.NextPow2(count)]); + _colors = PinnedArray.Pin(new Vector4[DebugXUtility.NextPow2(count)]); _buffersLength = count; } if (ReferenceEquals(_gizmos.Array, items) == false) @@ -554,8 +552,8 @@ namespace DCFApixels _matrices.Dispose(); _colors.Dispose(); } - _matrices = PinnedArray.Pin(new Matrix4x4[NextPow2(count)]); - _colors = PinnedArray.Pin(new Vector4[NextPow2(count)]); + _matrices = PinnedArray.Pin(new Matrix4x4[DebugXUtility.NextPow2(count)]); + _colors = PinnedArray.Pin(new Vector4[DebugXUtility.NextPow2(count)]); _buffersLength = count; } if (ReferenceEquals(_gizmos.Array, items) == false) diff --git a/Runtime/Gizmos/DebugX.lines.cs b/Runtime/Gizmos/DebugX.lines.cs index 493233d..e04573b 100644 --- a/Runtime/Gizmos/DebugX.lines.cs +++ b/Runtime/Gizmos/DebugX.lines.cs @@ -2,7 +2,6 @@ using DCFApixels.DebugXCore; using UnityEngine; using UnityEngine.Rendering; -using static DCFApixels.DebugXCore.DebugXUtility; namespace DCFApixels { @@ -40,7 +39,7 @@ namespace DCFApixels const float MinDistance = 2.5f; Vector3 direction = end - start; - float distance = FastSqrt(direction.sqrMagnitude); + float distance = DebugXUtility.FastSqrt(direction.sqrMagnitude); Quaternion rotation = direction == default ? Quaternion.identity : Quaternion.LookRotation(direction); var arrowSize = 0.5f; diff --git a/Runtime/Gizmos/DebugX.primitives.cs b/Runtime/Gizmos/DebugX.primitives.cs index 4e0ee98..d3e6210 100644 --- a/Runtime/Gizmos/DebugX.primitives.cs +++ b/Runtime/Gizmos/DebugX.primitives.cs @@ -3,7 +3,6 @@ using DCFApixels.DebugXCore; using System; using UnityEngine; using UnityEngine.Rendering; -using static DCFApixels.DebugXCore.DebugXUtility; namespace DCFApixels { @@ -88,7 +87,7 @@ namespace DCFApixels float x = sqrRadius * sqrRadius / sqrMagnitude; if (x / sqrRadius < 1f) { - float resultSize = FastSqrt(sqrRadius - x); + float resultSize = DebugXUtility.FastSqrt(sqrRadius - x); _buffer[i] = new Gizmo( new InstancingMeshGizmoLayout( diff --git a/Runtime/Shaders/HandlesWire.shader b/Runtime/Shaders/HandlesWire.shader index b1585dc..cb4dce9 100644 --- a/Runtime/Shaders/HandlesWire.shader +++ b/Runtime/Shaders/HandlesWire.shader @@ -67,7 +67,7 @@ Shader "DCFApixels/DebugX/Handles Wire" o.color = input[i].color; lineStream.Append(o); - int next = (i + 1) % 3; + uint next = (i + 1) % 3; o.vertex = input[next].vertex; o.color = input[next].color; lineStream.Append(o); diff --git a/Runtime/Utils/DebugXUtility.cs b/Runtime/Utils/DebugXUtility.cs index 6b9242b..36d72d7 100644 --- a/Runtime/Utils/DebugXUtility.cs +++ b/Runtime/Utils/DebugXUtility.cs @@ -1,6 +1,8 @@ using System; using UnityEngine; -using static DCFApixels.DebugX; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; #if UNITY_EDITOR using UnityEditor; #endif @@ -10,7 +12,56 @@ namespace DCFApixels.DebugXCore using IN = System.Runtime.CompilerServices.MethodImplAttribute; public static class DebugXUtility { - public static string GetGenericTypeName_Internal(Type type, int maxDepth, bool isFull) + private const MethodImplOptions LINE = MethodImplOptions.AggressiveInlining; + public static T LoadStaticData(T instance, string path) + { + object obj = instance; + var type = obj.GetType(); + var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); + var prefab = Resources.Load(path); + + if (prefab == null) + { + Debug.LogError($"{path} not found."); + return (T)obj; + } + + if (fields.Count() <= 0) + { + Debug.LogError($"{typeof(T).Name} no fields."); + return (T)obj; + } + + foreach (var field in fields.Where(o => o.FieldType == typeof(Mesh))) + { + var child = prefab.transform.Find(field.Name); + var meshFilter = child.GetComponent(); + if (meshFilter != null) + { + field.SetValue(obj, meshFilter.sharedMesh); + } + else + { + Debug.LogWarning(field.Name + " not found."); + } + } + foreach (var field in fields.Where(o => o.FieldType == typeof(Material))) + { + var child = prefab.transform.Find(field.Name); + var meshFilter = child.GetComponent(); + if (meshFilter != null) + { + field.SetValue(obj, meshFilter.sharedMaterial); + } + else + { + Debug.LogWarning(field.Name + " not found."); + } + } + + return (T)obj; + } + public static string GetGenericTypeName(Type type, int maxDepth, bool isFull) { #if DEBUG || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает string typeName = isFull ? type.FullName : type.Name; @@ -29,7 +80,7 @@ namespace DCFApixels.DebugXCore for (int i = 0; i < typeParameters.Length; ++i) { //чтобы строка не была слишком длинной, используются сокращенные имена для типов аргументов - string paramTypeName = GetGenericTypeName_Internal(typeParameters[i], maxDepth - 1, false); + string paramTypeName = GetGenericTypeName(typeParameters[i], maxDepth - 1, false); genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}"); } return $"{typeName}<{genericParams}>"; diff --git a/Runtime/Utils/StaticDataLoader.cs b/Runtime/Utils/StaticDataLoader.cs deleted file mode 100644 index 5c60816..0000000 --- a/Runtime/Utils/StaticDataLoader.cs +++ /dev/null @@ -1,58 +0,0 @@ -using System.Linq; -using System.Reflection; -using UnityEngine; - -namespace DCFApixels.DebugXCore -{ - public static class StaticDataLoader - { - public static T Load(T instance, string path) - { - object obj = instance; - var type = obj.GetType(); - var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); - var prefab = Resources.Load(path); - - if (prefab == null) - { - Debug.LogError($"{path} not found"); - return (T)obj; - } - - if (fields.Count() <= 0) - { - Debug.LogError($"{typeof(T).Name} no fields"); - return (T)obj; - } - - foreach (var field in fields.Where(o => o.FieldType == typeof(Mesh))) - { - var child = prefab.transform.Find(field.Name); - var meshFilter = child.GetComponent(); - if (meshFilter != null) - { - field.SetValue(obj, meshFilter.sharedMesh); - } - else - { - Debug.LogWarning(field.Name + " not found"); - } - } - foreach (var field in fields.Where(o => o.FieldType == typeof(Material))) - { - var child = prefab.transform.Find(field.Name); - var meshFilter = child.GetComponent(); - if (meshFilter != null) - { - field.SetValue(obj, meshFilter.sharedMaterial); - } - else - { - Debug.LogWarning(field.Name + " not found"); - } - } - - return (T)obj; - } - } -} \ No newline at end of file diff --git a/Runtime/Utils/StaticDataLoader.cs.meta b/Runtime/Utils/StaticDataLoader.cs.meta deleted file mode 100644 index 2274ab2..0000000 --- a/Runtime/Utils/StaticDataLoader.cs.meta +++ /dev/null @@ -1,2 +0,0 @@ -fileFormatVersion: 2 -guid: 2482dd3495375b040ab937f0af691464 \ No newline at end of file diff --git a/Samples/DebugX.Samples.asmdef b/Samples/DebugX.Samples.asmdef new file mode 100644 index 0000000..7f215e5 --- /dev/null +++ b/Samples/DebugX.Samples.asmdef @@ -0,0 +1,16 @@ +{ + "name": "DCFApixels.DebugX.Samples", + "rootNamespace": "DCFApixels", + "references": [ + "GUID:1139531e2a3a6bd4c8ad2bc68ae00719" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": true, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Samples/DebugX.Samples.asmdef.meta b/Samples/DebugX.Samples.asmdef.meta new file mode 100644 index 0000000..c8369ac --- /dev/null +++ b/Samples/DebugX.Samples.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ad77f906ceb59f143976326b11159337 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: