summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-23 10:08:29 +0800
committerchai <215380520@qq.com>2024-05-23 10:08:29 +0800
commit8722a9920c1f6119bf6e769cba270e63097f8e25 (patch)
tree2eaf9865de7fb1404546de4a4296553d8f68cc3b /Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs
parent3ba4020b69e5971bb0df7ee08b31d10ea4d01937 (diff)
+ astar project
Diffstat (limited to 'Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs')
-rw-r--r--Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs122
1 files changed, 122 insertions, 0 deletions
diff --git a/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs
new file mode 100644
index 0000000..8212c1d
--- /dev/null
+++ b/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Drawing/DrawingUtilities.cs
@@ -0,0 +1,122 @@
+using UnityEngine;
+using Unity.Mathematics;
+using Unity.Collections;
+using System.Collections.Generic;
+
+namespace Pathfinding.Drawing {
+ /// <summary>Various high-level utilities that are useful when drawing things</summary>
+ public static class DrawingUtilities {
+ private static List<Component> componentBuffer = new List<Component>();
+
+ /// <summary>
+ /// Bounding box of a GameObject.
+ /// Sometimes you want to quickly draw the bounding box of an object. This is not always trivial as the object may have any number of children with colliders and renderers.
+ /// You can use this method to calculate the bounding box easily.
+ ///
+ /// The bounding box is calculated based on the colliders and renderers on this object and all its children.
+ ///
+ /// [Open online documentation to see images]
+ /// <code>
+ /// Draw.WireBox(DrawingUtilities.BoundsFrom(transform), Color.black);
+ /// </code>
+ ///
+ /// See: <see cref="BoundsFrom(Transform)"/>
+ /// </summary>
+ public static Bounds BoundsFrom (GameObject gameObject) {
+ return BoundsFrom(gameObject.transform);
+ }
+
+ /// <summary>
+ /// Bounding box of a Transform.
+ /// Sometimes you want to quickly draw the bounding box of an object. This is not always trivial as the object may have any number of children with colliders and renderers.
+ /// You can use this method to calculate the bounding box easily.
+ ///
+ /// The bounding box is calculated based on the colliders and renderers on this object and all its children.
+ ///
+ /// [Open online documentation to see images]
+ /// <code>
+ /// Draw.WireBox(DrawingUtilities.BoundsFrom(transform), Color.black);
+ /// </code>
+ ///
+ /// See: <see cref="BoundsFrom(GameObject)"/>
+ /// </summary>
+ public static Bounds BoundsFrom (Transform transform) {
+ transform.gameObject.GetComponents(componentBuffer);
+ Bounds bounds = new Bounds(transform.position, Vector3.zero);
+ for (int i = 0; i < componentBuffer.Count; i++) {
+ var component = componentBuffer[i];
+ if (component is Collider coll) bounds.Encapsulate(coll.bounds);
+ else if (component is Collider2D coll2D) bounds.Encapsulate(coll2D.bounds);
+ else if (component is MeshRenderer rend) bounds.Encapsulate(rend.bounds);
+ else if (component is SpriteRenderer rendSprite) bounds.Encapsulate(rendSprite.bounds);
+ }
+ componentBuffer.Clear();
+ var children = transform.childCount;
+ for (int i = 0; i < children; i++) bounds.Encapsulate(BoundsFrom(transform.GetChild(i)));
+ return bounds;
+ }
+
+ /// <summary>
+ /// Bounding box which contains all points in the list.
+ /// <code>
+ /// List<Vector3> points = new List<Vector3> { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 1) };
+ /// Draw.WireBox(DrawingUtilities.BoundsFrom(points), Color.black);
+ /// </code>
+ ///
+ /// See: <see cref="BoundsFrom(Vector3"/>[])
+ /// See: <see cref="BoundsFrom(NativeArray<float3>)"/>
+ /// </summary>
+ public static Bounds BoundsFrom (List<Vector3> points) {
+ if (points.Count == 0) throw new System.ArgumentException("At least 1 point is required");
+ Vector3 mn = points[0];
+ Vector3 mx = points[0];
+ for (int i = 0; i < points.Count; i++) {
+ mn = Vector3.Min(mn, points[i]);
+ mx = Vector3.Max(mx, points[i]);
+ }
+ return new Bounds((mx + mn) * 0.5f, (mx - mn) * 0.5f);
+ }
+
+ /// <summary>
+ /// Bounding box which contains all points in the array.
+ /// <code>
+ /// List<Vector3> points = new List<Vector3> { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 1) };
+ /// Draw.WireBox(DrawingUtilities.BoundsFrom(points), Color.black);
+ /// </code>
+ ///
+ /// See: <see cref="BoundsFrom(List<Vector3>)"/>
+ /// See: <see cref="BoundsFrom(NativeArray<float3>)"/>
+ /// </summary>
+ public static Bounds BoundsFrom (Vector3[] points) {
+ if (points.Length == 0) throw new System.ArgumentException("At least 1 point is required");
+ Vector3 mn = points[0];
+ Vector3 mx = points[0];
+ for (int i = 0; i < points.Length; i++) {
+ mn = Vector3.Min(mn, points[i]);
+ mx = Vector3.Max(mx, points[i]);
+ }
+ return new Bounds((mx + mn) * 0.5f, (mx - mn) * 0.5f);
+ }
+
+ /// <summary>
+ /// Bounding box which contains all points in the array.
+ /// <code>
+ /// List<Vector3> points = new List<Vector3> { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 1) };
+ /// Draw.WireBox(DrawingUtilities.BoundsFrom(points), Color.black);
+ /// </code>
+ ///
+ /// See: <see cref="BoundsFrom(List<Vector3>)"/>
+ /// See: <see cref="BoundsFrom(Vector3"/>[])
+ /// </summary>
+ public static Bounds BoundsFrom (NativeArray<float3> points) {
+ if (points.Length == 0) throw new System.ArgumentException("At least 1 point is required");
+ float3 mn = points[0];
+ float3 mx = points[0];
+ for (int i = 0; i < points.Length; i++) {
+ mn = math.min(mn, points[i]);
+ mx = math.max(mx, points[i]);
+ }
+ return new Bounds((mx + mn) * 0.5f, (mx - mn) * 0.5f);
+ }
+ }
+}