From 5ebff2615a281785ac9526b9d22d48897bf60284 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 3 Mar 2025 19:29:49 +0800 Subject: [PATCH] stash --- Runtime/Gizmos/DebugX.base.cs | 379 ++++++++++++++-------------- Runtime/Gizmos/DebugX.primitives.cs | 20 +- 2 files changed, 209 insertions(+), 190 deletions(-) diff --git a/Runtime/Gizmos/DebugX.base.cs b/Runtime/Gizmos/DebugX.base.cs index c132b61..b9b35db 100644 --- a/Runtime/Gizmos/DebugX.base.cs +++ b/Runtime/Gizmos/DebugX.base.cs @@ -12,190 +12,190 @@ namespace DCFApixels 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 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 @@ -299,6 +299,17 @@ namespace DCFApixels #endregion #region InstancingMesh + [IN(LINE)] + public MeshHandler MeshX(Vector3 position, Quaternion rotation, Vector3 size) + where TMesh : struct, IStaticMesh + where TMat : struct, IStaticMaterial + { + //return Gizmo(new InstancingMeshGizmo(position, rotation, size)); + Gizmo(new InstancingMeshGizmo(position, rotation, size)); + return new MeshHandler(Color, Duration, position, rotation, size); + } + + [IN(LINE)] public DrawHandler Mesh(Vector3 position, Quaternion rotation, Vector3 size) where TMesh : struct, IStaticMesh diff --git a/Runtime/Gizmos/DebugX.primitives.cs b/Runtime/Gizmos/DebugX.primitives.cs index 09c0ebd..654a419 100644 --- a/Runtime/Gizmos/DebugX.primitives.cs +++ b/Runtime/Gizmos/DebugX.primitives.cs @@ -60,17 +60,25 @@ namespace DCFApixels [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); - return new MeshHandler(h.Color, h.Duration, position, rotation, size); + return new DrawHandler(h.Duration, h.Color).MeshX(position, rotation, size); } - #endregion + [IN(LINE)] + public static MeshHandler Cube(this T h) where T : struct, IDrawHandler, IDrawPositionHandler, IDrawRotationHandler, IDrawScaleHandler + { + return new DrawHandler(h.Duration, h.Color).MeshX(h.Position, h.Rotation, h.Scale); + } + #endregion - #region WireCube + #region WireCube [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); - return new MeshHandler(h.Color, h.Duration, position, rotation, size); + return new DrawHandler(h.Duration, h.Color).MeshX(position, rotation, size); + } + [IN(LINE)] + public static MeshHandler WireCube(this T h) where T : struct, IDrawHandler, IDrawPositionHandler, IDrawRotationHandler, IDrawScaleHandler + { + return new DrawHandler(h.Duration, h.Color).MeshX(h.Position, h.Rotation, h.Scale); } #endregion