feat: add DebugUtility.DrawFunction

This commit is contained in:
Oliver Booth 2023-04-13 01:48:58 +01:00
parent c5ea6cca58
commit 3f08f5270f
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
4 changed files with 119 additions and 2 deletions

View File

@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`. - X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
- X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`. - X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`.
- X10D.Unity: Added `DebugUtility.DrawFunction`, and `DebugUtility.DrawUnjoinedPolyhedron` on which it relies.
### Changed ### Changed

View File

@ -1,4 +1,5 @@
using UnityEngine; using System;
using UnityEngine;
using X10D.Drawing; using X10D.Drawing;
using X10D.Unity.Drawing; using X10D.Unity.Drawing;
using Color = UnityEngine.Color; using Color = UnityEngine.Color;
@ -34,6 +35,9 @@ namespace X10D.Unity.Tests
var sphere = new Sphere(System.Numerics.Vector3.Zero, 0.5f); var sphere = new Sphere(System.Numerics.Vector3.Zero, 0.5f);
DebugUtility.DrawSphere(sphere, 25, new Vector2(0.0f, -1.5f), Color.white); DebugUtility.DrawSphere(sphere, 25, new Vector2(0.0f, -1.5f), Color.white);
DebugUtility.DrawFunction(x => MathF.Sin(x + UnityEngine.Time.time % (2 * MathF.PI)), -10, 10, 0.1f, Vector3.up * 4,
Color.yellow, 0.0f, false);
DebugUtility.Assert(true); DebugUtility.Assert(true);
} }
} }

View File

@ -0,0 +1,69 @@
using UnityEngine;
using X10D.Drawing;
using X10D.Numerics;
using X10D.Unity.Numerics;
using Quaternion = System.Numerics.Quaternion;
namespace X10D.Unity;
public static partial class DebugUtility
{
/// <summary>
/// Draws a function plot.
/// </summary>
/// <param name="function">The function to plot.</param>
/// <param name="xMin">The minimum X value.</param>
/// <param name="xMax">The maximum X value.</param>
public static void DrawFunction(Func<float, float> function, float xMin, float xMax)
{
DrawFunction(function, xMin, xMax, 0.1f, Vector3.zero, Color.white, 0.0f, false);
}
/// <summary>
/// Draws a function plot.
/// </summary>
/// <param name="function">The function to plot.</param>
/// <param name="xMin">The minimum X value.</param>
/// <param name="xMax">The maximum X value.</param>
/// <param name="step">The X increment.</param>
/// <param name="offset">The drawing offset of the circle.</param>
/// <param name="color">The color of the circle.</param>
/// <param name="duration">
/// The duration of the circle's visibility, in seconds. If 0 is passed, the circle is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="true" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="true" /> will have the circle be obscured by objects closer to the camera.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="function" /> is <see langword="null" />.</exception>
public static void DrawFunction(Func<float, float> function, float xMin, float xMax, float step, in Vector3 offset,
in Color color, float duration,
bool depthTest)
{
if (function is null)
{
throw new ArgumentNullException(nameof(function));
}
DrawUnjoinedPolyhedron(CreateFunction(function, xMin, xMax, step, Vector3.zero), offset, color, duration, depthTest);
}
private static Polyhedron CreateFunction(Func<float, float> function, float xMin, float xMax, float step, in Vector3 axis)
{
var points = new List<System.Numerics.Vector3>();
for (float x = xMin; x < xMax; x += step)
{
float y = function(x);
var vector = new System.Numerics.Vector3(x, y, 0);
if (axis != Vector3.zero)
{
vector = Quaternion.CreateFromAxisAngle(axis.ToSystemVector(), MathF.PI / 2.0f).Multiply(vector);
}
points.Add(vector);
}
return new Polyhedron(points);
}
}

View File

@ -1,4 +1,4 @@
using UnityEngine; using UnityEngine;
using X10D.Drawing; using X10D.Drawing;
using X10D.Unity.Numerics; using X10D.Unity.Numerics;
@ -126,4 +126,47 @@ public static partial class DebugUtility
DrawLine(start, end, color, duration, depthTest); DrawLine(start, end, color, duration, depthTest);
} }
} }
/// <summary>
/// Draws a polyhedron.
/// </summary>
/// <param name="polyhedron">The polyhedron to draw.</param>
/// <param name="offset">The drawing offset of the polyhedron.</param>
/// <param name="color">The color to use for drawing.</param>
/// <param name="duration">
/// The duration of the polyhedron's visibility, in seconds. If 0 is passed, the polyhedron is visible for a single frame.
/// </param>
/// <param name="depthTest">
/// <see langword="true" /> if depth test should be applied; otherwise, <see langword="false" />. Passing
/// <see langword="true" /> will have the box be obscured by objects closer to the camera.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="polyhedron" /> is <see langword="null" />.</exception>
public static void DrawUnjoinedPolyhedron(Polyhedron polyhedron, in Vector3 offset, in Color color, float duration,
bool depthTest)
{
if (polyhedron is null)
{
throw new ArgumentNullException(nameof(polyhedron));
}
IReadOnlyList<System.Numerics.Vector3> points = polyhedron.Vertices;
if (points.Count < 2)
{
return;
}
for (var i = 0; i < points.Count; i++)
{
if (i >= points.Count - 2)
{
break;
}
int j = i + 1;
Vector3 start = points[i].ToUnityVector() + offset;
Vector3 end = points[j].ToUnityVector() + offset;
DrawLine(start, end, color, duration, depthTest);
}
}
} }