From 5a6ac22b020cfe92e1f08027ad7d029eea4a3753 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:07:15 +0800 Subject: [PATCH] stash --- Runtime/DebugX.cs | 14 +- Runtime/Gizmos/DebugX.base.cs | 191 +++++++++++++++++++++++++++- Runtime/Gizmos/DebugX.primitives.cs | 82 +++++++----- 3 files changed, 254 insertions(+), 33 deletions(-) diff --git a/Runtime/DebugX.cs b/Runtime/DebugX.cs index 1204eab..bf022c5 100644 --- a/Runtime/DebugX.cs +++ b/Runtime/DebugX.cs @@ -202,7 +202,7 @@ namespace DCFApixels { return new DrawHandler(duration, color); } - public readonly partial struct DrawHandler + public readonly partial struct DrawHandler : IDrawHandler { public readonly Color Color; public readonly float Duration; @@ -218,6 +218,18 @@ namespace DCFApixels Duration = time; //ContextController = GetCurrenRenderContextController(); } + + Color IDrawHandler.Color + { + [IN(LINE)] + get => Color; + } + float IDrawHandler.Duration + { + [IN(LINE)] + get => Duration; + } + [IN(LINE)] public DrawHandler Setup(float duration, Color color) => new DrawHandler(duration, color); [IN(LINE)] public DrawHandler Setup(float duration) => new DrawHandler(duration, Color); [IN(LINE)] public DrawHandler Setup(Color color) => new DrawHandler(Duration, color); diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index de0acd6..c132b61 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -8,9 +8,196 @@ using UnityEngine.Rendering; namespace DCFApixels { + using static DCFApixels.DebugX; using static DebugXConsts; using IN = System.Runtime.CompilerServices.MethodImplAttribute; + public unsafe static partial class ExtsХ + { + #region InstancingMeshGizmo + [IN(LINE)] + public static MeshHandler Mesh(this T h, Vector3 position, Quaternion rotation, Vector3 size) + where T : struct, IDrawHandler + where TMesh : struct, IStaticMesh + where TMat : struct, IStaticMaterial + { + new DrawHandler(h.Duration, h.Color).Gizmo(new InstancingMeshGizmo(position, rotation, size)); + return new MeshHandler(h.Color, h.Duration, position, rotation, size); + } + [IN(LINE)] + public static MeshHandler Mesh(this T h, Vector3 position, Quaternion rotation, Vector3 size) + where T : struct, IDrawHandler + where TMesh : struct, IStaticMesh + { + new DrawHandler(h.Duration, h.Color).Gizmo(new InstancingMeshGizmo(position, rotation, size)); + return new MeshHandler(h.Color, h.Duration, position, rotation, size); + } + [IN(LINE)] + public static MeshHandler UnlitMesh(this T h, Vector3 position, Quaternion rotation, Vector3 size) + where T : struct, IDrawHandler + where TMesh : struct, IStaticMesh + { + new DrawHandler(h.Duration, h.Color).Gizmo(new InstancingMeshGizmo(position, rotation, size)); + return new MeshHandler(h.Color, h.Duration, position, rotation, size); + } + [IN(LINE)] + public static MeshHandler WireMesh(this T h, Vector3 position, Quaternion rotation, Vector3 size) + where T : struct, IDrawHandler + where TMesh : struct, IStaticMesh + { + new DrawHandler(h.Duration, h.Color).Gizmo(new InstancingMeshGizmo(position, rotation, size)); + return new MeshHandler(h.Color, h.Duration, position, rotation, size); + } + + private readonly struct InstancingMeshGizmoLayout + { + public readonly Quaternion Rotation; + public readonly Vector3 Position; + public readonly Vector3 Size; + public InstancingMeshGizmoLayout(Vector3 position, Quaternion rotation, Vector3 size) + { + Rotation = rotation; + Position = position; + Size = size; + } + } + private readonly struct InstancingMeshGizmo : IGizmo> + where TMesh : struct, IStaticMesh + where TMat : struct, IStaticMaterial + { + public readonly Quaternion Rotation; + public readonly Vector3 Position; + public readonly Vector3 Size; + [IN(LINE)] + public InstancingMeshGizmo(Vector3 position, Quaternion rotation, Vector3 size) + { + Rotation = rotation; + Position = position; + Size = size; + } + public IGizmoRenderer> RegisterNewRenderer() { return new Renderer(); } + private class Renderer : InstancingMeshRendererBase, IGizmoRenderer> + { + public Renderer() : base(default(TMesh), default(TMat)) { } + public void Prepare(Camera camera, GizmosList> list) + { + Prepare(list); + } + public void Render(Camera camera, GizmosList> list, CommandBuffer cb) + { + Render(cb); + } + } + } + #endregion + + #region InstancingMeshRendererBase + private class InstancingMeshRendererBase + { + private readonly struct GizmoData + { + public readonly Quaternion Rotation; + public readonly Vector3 Position; + public readonly Vector3 Size; + } + private struct PrepareJob : IJobParallelFor + { + [NativeDisableUnsafePtrRestriction] + public Gizmo* Items; + [NativeDisableUnsafePtrRestriction] + public Matrix4x4* ResultMatrices; + [NativeDisableUnsafePtrRestriction] + public Vector4* ResultColors; + public void Execute(int index) + { + ref readonly var item = ref Items[index]; + //if (item.IsSwaped == 0) { return; } + ResultMatrices[index] = Matrix4x4.TRS(item.Value.Position, item.Value.Rotation, item.Value.Size); + ResultColors[index] = item.Color; + } + } + + private readonly IStaticMesh _mesh; + private readonly IStaticMaterial _material; + + private readonly MaterialPropertyBlock _materialPropertyBlock; + + private int _buffersLength = 0; + private PinnedArray _matrices; + private PinnedArray _colors; + private PinnedArray> _gizmos; + + private JobHandle _jobHandle; + private int _prepareCount = 0; + + public InstancingMeshRendererBase(IStaticMesh mesh, IStaticMaterial material) + { + _mesh = mesh; + _material = material; + _materialPropertyBlock = new MaterialPropertyBlock(); + + _matrices = PinnedArray.Pin(DummyArray.Get()); + _colors = PinnedArray.Pin(DummyArray.Get()); + } + public virtual int ExecuteOrder => _material.GetExecuteOrder(); + public virtual bool IsStaticRender => true; + protected void Prepare(GizmosList rawList) + { + var list = rawList.As(); + _prepareCount = list.Count; + var items = list.Items; + var count = list.Count; + + if (_buffersLength < count) + { + _matrices.Dispose(); + _colors.Dispose(); + _matrices = PinnedArray.Pin(new Matrix4x4[DebugXUtility.NextPow2(count)]); + _colors = PinnedArray.Pin(new Vector4[DebugXUtility.NextPow2(count)]); + _buffersLength = count; + } + if (ReferenceEquals(_gizmos.Array, items) == false) + { + if (_gizmos.Array != null) + { + _gizmos.Dispose(); + } + _gizmos = PinnedArray>.Pin(items); + } + + var job = new PrepareJob + { + Items = _gizmos.Ptr, + ResultMatrices = _matrices.Ptr, + ResultColors = _colors.Ptr, + }; + _jobHandle = job.Schedule(count, 64); + } + protected void Render(CommandBuffer cb) + { + Material material = _material.GetMaterial(); + Mesh mesh = _mesh.GetMesh(); + _materialPropertyBlock.Clear(); + _jobHandle.Complete(); + if (IsSupportsComputeShaders) + { + _materialPropertyBlock.SetVectorArray(ColorPropertyID, _colors.Array); + cb.DrawMeshInstanced(mesh, 0, material, -1, _matrices.Array, _prepareCount, _materialPropertyBlock); + } + else + { + for (int i = 0; i < _prepareCount; i++) + { + _materialPropertyBlock.SetColor(ColorPropertyID, _colors.Ptr[i]); + cb.DrawMesh(mesh, _matrices.Ptr[i], material, 0, -1, _materialPropertyBlock); + } + } + } + } + #endregion + } + + public static unsafe partial class DebugX { public readonly partial struct DrawHandler @@ -64,7 +251,7 @@ namespace DCFApixels { return Gizmo(new MeshGizmo(mesh, position, rotation, size)); } - + private readonly struct MeshGizmoLayout { public readonly Mesh Mesh; @@ -95,7 +282,7 @@ namespace DCFApixels Size = size; } public IGizmoRenderer> RegisterNewRenderer() { return new Renderer(); } - + private class Renderer : MeshRendererBase, IGizmoRenderer> { public Renderer() : base(default(TMat)) { } diff --git a/Runtime/Gizmos/DebugX.primitives.cs b/Runtime/Gizmos/DebugX.primitives.cs index 83c7c37..09c0ebd 100644 --- a/Runtime/Gizmos/DebugX.primitives.cs +++ b/Runtime/Gizmos/DebugX.primitives.cs @@ -12,12 +12,14 @@ namespace DCFApixels public readonly struct MeshHandler : IDrawHandler, IDrawPositionHandler, IDrawRotationHandler, IDrawScaleHandler { + internal const System.Runtime.CompilerServices.MethodImplOptions LINE = System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining; + public readonly Color Color; public readonly float Duration; public readonly Vector3 Position; public readonly Quaternion Rotation; public readonly Vector3 Scale; - [IN(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] + [IN(LINE)] public MeshHandler(Color color, float duration, Vector3 position, Quaternion rotation, Vector3 scale) { Color = color; @@ -26,36 +28,56 @@ namespace DCFApixels Rotation = rotation; Scale = scale; } - Color IDrawHandler.Color => Color; - float IDrawHandler.Duration => Duration; - Vector3 IDrawPositionHandler.Position => Position; - Quaternion IDrawRotationHandler.Rotation => Rotation; - Vector3 IDrawScaleHandler.Scale => Scale; + Color IDrawHandler.Color + { + [IN(LINE)] + get => Color; + } + float IDrawHandler.Duration + { + [IN(LINE)] + get => Duration; + } + Vector3 IDrawPositionHandler.Position + { + [IN(LINE)] + get => Position; + } + Quaternion IDrawRotationHandler.Rotation + { + [IN(LINE)] + get => Rotation; + } + Vector3 IDrawScaleHandler.Scale + { + [IN(LINE)] + get => Scale; + } } - public unsafe static class ExtsХ + public unsafe static partial class ExtsХ { #region Cube - [IN(LINE)] public static MeshHandler Cube(this DrawHandler h, Vector3 position, Quaternion rotation, float size) => h.Cube(position, rotation, new Vector3(size, size, size)); - [IN(LINE)] public static MeshHandler Cube(this DrawHandler h, Vector3 position, Quaternion rotation, Vector3 size) + [IN(LINE)] public static MeshHandler Cube(this T h, Vector3 position, Quaternion rotation, float size) where T : struct, IDrawHandler => h.Cube(position, rotation, new Vector3(size, size, size)); + [IN(LINE)] public static MeshHandler Cube(this T h, Vector3 position, Quaternion rotation, Vector3 size) where T : struct, IDrawHandler { - h.Mesh(position, rotation, size); + h.Mesh(position, rotation, size); return new MeshHandler(h.Color, h.Duration, position, rotation, size); } #endregion #region WireCube - [IN(LINE)] public static MeshHandler WireCube(this DrawHandler h, Vector3 position, Quaternion rotation, float size) => h.WireCube(position, rotation, new Vector3(size, size, size)); - [IN(LINE)] public static MeshHandler WireCube(this DrawHandler h, Vector3 position, Quaternion rotation, Vector3 size) + [IN(LINE)] public static MeshHandler WireCube(this T h, Vector3 position, Quaternion rotation, float size) where T : struct, IDrawHandler => h.WireCube(position, rotation, new Vector3(size, size, size)); + [IN(LINE)] public static MeshHandler WireCube(this T h, Vector3 position, Quaternion rotation, Vector3 size) where T : struct, IDrawHandler { - h.Mesh(position, rotation, size); + h.Mesh(position, rotation, size); return new MeshHandler(h.Color, h.Duration, position, rotation, size); } #endregion #region CubePoints - [IN(LINE)] public static MeshHandler CubePoints(this DrawHandler h, Vector3 position, Quaternion rotation, float size) => h.CubePoints(position, rotation, new Vector3(size, size, size)); + [IN(LINE)] public static MeshHandler CubePoints(this T h, Vector3 position, Quaternion rotation, float size) where T : struct, IDrawHandler => h.CubePoints(position, rotation, new Vector3(size, size, size)); [IN(LINE)] - public static MeshHandler CubePoints(this DrawHandler h, Vector3 position, Quaternion rotation, Vector3 size) + public static MeshHandler CubePoints(this T h, Vector3 position, Quaternion rotation, Vector3 size) where T : struct, IDrawHandler { Vector3 halfSize = size / 2f; @@ -73,7 +95,7 @@ namespace DCFApixels for (int i = 0; i < 8; i++) { - h.Dot(rotation * vertices[i] + position); + new DrawHandler(h.Duration, h.Color).Dot(rotation * vertices[i] + position); } return new MeshHandler(h.Color, h.Duration, position, rotation, size); @@ -81,9 +103,9 @@ namespace DCFApixels #endregion #region CubeGrid - [IN(LINE)] public static MeshHandler CubeGrid(this DrawHandler h, Vector3 position, Quaternion rotation, float size, Vector3Int cells) => h.CubeGrid(position, rotation, new Vector3(size, size, size), cells); + [IN(LINE)] public static MeshHandler CubeGrid(this T h, Vector3 position, Quaternion rotation, float size, Vector3Int cells) where T : IDrawHandler => h.CubeGrid(position, rotation, new Vector3(size, size, size), cells); [IN(LINE)] - public unsafe static MeshHandler CubeGrid(this DrawHandler h, Vector3 position, Quaternion rotation, Vector3 size, Vector3Int cells) + public unsafe static MeshHandler CubeGrid(this T h, Vector3 position, Quaternion rotation, Vector3 size, Vector3Int cells) where T : IDrawHandler { Vector3 halfSize = size / 2f; @@ -108,28 +130,28 @@ namespace DCFApixels for (int i = 0; i <= cells.y; i++) { Vector3 pos = up * i; - h.Line(vertices[0] + pos, vertices[1] + pos); - h.Line(vertices[1] + pos, vertices[2] + pos); - h.Line(vertices[2] + pos, vertices[3] + pos); - h.Line(vertices[3] + pos, vertices[0] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[0] + pos, vertices[1] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[1] + pos, vertices[2] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[2] + pos, vertices[3] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[3] + pos, vertices[0] + pos); } Vector3 right = rotation * Vector3.right * (size.x / cells.x); for (int i = 0; i <= cells.x; i++) { Vector3 pos = right * i; - h.Line(vertices[0] + pos, vertices[3] + pos); - h.Line(vertices[3] + pos, vertices[7] + pos); - h.Line(vertices[4] + pos, vertices[0] + pos); - h.Line(vertices[7] + pos, vertices[4] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[0] + pos, vertices[3] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[3] + pos, vertices[7] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[4] + pos, vertices[0] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[7] + pos, vertices[4] + pos); } Vector3 forward = rotation * Vector3.forward * (size.z / cells.z); for (int i = 0; i <= cells.z; i++) { Vector3 pos = forward * i; - h.Line(vertices[4] + pos, vertices[5] + pos); - h.Line(vertices[5] + pos, vertices[1] + pos); - h.Line(vertices[1] + pos, vertices[0] + pos); - h.Line(vertices[0] + pos, vertices[4] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[4] + pos, vertices[5] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[5] + pos, vertices[1] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[1] + pos, vertices[0] + pos); + new DrawHandler(h.Duration, h.Color).Line(vertices[0] + pos, vertices[4] + pos); } return new MeshHandler(h.Color, h.Duration, position, rotation, size);