This commit is contained in:
DCFApixels 2025-03-03 19:07:15 +08:00
parent 196edffae8
commit 5a6ac22b02
3 changed files with 254 additions and 33 deletions

View File

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

View File

@ -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<T, TMesh, TMat>(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<TMesh, TMat>(position, rotation, size));
return new MeshHandler(h.Color, h.Duration, position, rotation, size);
}
[IN(LINE)]
public static MeshHandler Mesh<T, TMesh>(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<TMesh, LitMat>(position, rotation, size));
return new MeshHandler(h.Color, h.Duration, position, rotation, size);
}
[IN(LINE)]
public static MeshHandler UnlitMesh<T, TMesh>(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<TMesh, UnlitMat>(position, rotation, size));
return new MeshHandler(h.Color, h.Duration, position, rotation, size);
}
[IN(LINE)]
public static MeshHandler WireMesh<T, TMesh>(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<TMesh, WireMat>(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<TMesh, TMat> : IGizmo<InstancingMeshGizmo<TMesh, TMat>>
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<InstancingMeshGizmo<TMesh, TMat>> RegisterNewRenderer() { return new Renderer(); }
private class Renderer : InstancingMeshRendererBase, IGizmoRenderer<InstancingMeshGizmo<TMesh, TMat>>
{
public Renderer() : base(default(TMesh), default(TMat)) { }
public void Prepare(Camera camera, GizmosList<InstancingMeshGizmo<TMesh, TMat>> list)
{
Prepare(list);
}
public void Render(Camera camera, GizmosList<InstancingMeshGizmo<TMesh, TMat>> 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<GizmoData>* 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<Matrix4x4> _matrices;
private PinnedArray<Vector4> _colors;
private PinnedArray<Gizmo<GizmoData>> _gizmos;
private JobHandle _jobHandle;
private int _prepareCount = 0;
public InstancingMeshRendererBase(IStaticMesh mesh, IStaticMaterial material)
{
_mesh = mesh;
_material = material;
_materialPropertyBlock = new MaterialPropertyBlock();
_matrices = PinnedArray<Matrix4x4>.Pin(DummyArray<Matrix4x4>.Get());
_colors = PinnedArray<Vector4>.Pin(DummyArray<Vector4>.Get());
}
public virtual int ExecuteOrder => _material.GetExecuteOrder();
public virtual bool IsStaticRender => true;
protected void Prepare(GizmosList rawList)
{
var list = rawList.As<GizmoData>();
_prepareCount = list.Count;
var items = list.Items;
var count = list.Count;
if (_buffersLength < count)
{
_matrices.Dispose();
_colors.Dispose();
_matrices = PinnedArray<Matrix4x4>.Pin(new Matrix4x4[DebugXUtility.NextPow2(count)]);
_colors = PinnedArray<Vector4>.Pin(new Vector4[DebugXUtility.NextPow2(count)]);
_buffersLength = count;
}
if (ReferenceEquals(_gizmos.Array, items) == false)
{
if (_gizmos.Array != null)
{
_gizmos.Dispose();
}
_gizmos = PinnedArray<Gizmo<GizmoData>>.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

View File

@ -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;
}
public unsafe static class ExtsХ
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 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<T>(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<T>(this T h, Vector3 position, Quaternion rotation, Vector3 size) where T : struct, IDrawHandler
{
h.Mesh<CubeMesh, LitMat>(position, rotation, size);
h.Mesh<T, CubeMesh, LitMat>(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<T>(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<T>(this T h, Vector3 position, Quaternion rotation, Vector3 size) where T : struct, IDrawHandler
{
h.Mesh<WireCubeMesh, GeometryUnlitMat>(position, rotation, size);
h.Mesh<T, WireCubeMesh, GeometryUnlitMat>(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<T>(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<T>(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<T>(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<T>(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);