Unity-DebugX/Runtime/Gizmos/DebugX.raycasts.cs

249 lines
9.3 KiB
C#
Raw Normal View History

2025-03-06 13:14:54 +08:00
using DCFApixels.DebugXCore.Internal;
2025-02-28 12:50:35 +08:00
using UnityEngine;
2025-02-22 17:25:54 +08:00
namespace DCFApixels
{
using IN = System.Runtime.CompilerServices.MethodImplAttribute;
public static partial class DebugX
{
private const float ShadowAlphaMultiplier = 0.3f;
public readonly partial struct DrawHandler
{
2025-03-12 21:49:38 +08:00
#if DEBUGX_ENABLE_PHYSICS3D
2025-03-03 15:56:04 +08:00
#region RaycastHit
[IN(LINE)]
public DrawHandler RaycastHit(RaycastHit hit)
{
if (hit.collider != null)
{
DotDiamond(hit.point);
RayArrow(hit.point, hit.normal);
}
return this;
}
#endregion
2025-02-22 17:25:54 +08:00
#region Raycast
[IN(LINE)] public DrawHandler Raycast(Ray ray, RaycastHit hit) => Raycast(ray.origin, ray.direction, hit);
[IN(LINE)]
public DrawHandler Raycast(Vector3 origin, Vector3 direction, RaycastHit hit)
{
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
Line(origin, origin + direction * hit.distance);
2025-03-03 15:56:04 +08:00
RaycastHit(hit);
2025-02-22 17:25:54 +08:00
}
return this;
}
#endregion
2025-02-23 08:41:28 +08:00
#region SphereCast
2025-02-22 17:25:54 +08:00
[IN(LINE)] public DrawHandler SphereCast(Ray ray, float radius, RaycastHit hit) => SphereCast(ray.origin, ray.direction, radius, hit);
[IN(LINE)]
public DrawHandler SphereCast(Vector3 origin, Vector3 direction, float radius, RaycastHit hit)
{
WireSphere(origin, radius);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
Vector3 end = origin + direction * hit.distance;
WidthOutLine(origin, end, radius * 2f);
2025-03-03 15:56:04 +08:00
Setup(Color.SetAlpha(ShadowAlphaMultiplier)).Line(origin, end);
2025-02-22 17:25:54 +08:00
WireSphere(end, radius);
2025-03-03 15:56:04 +08:00
RaycastHit(hit);
2025-02-22 17:25:54 +08:00
}
return this;
}
#endregion
2025-02-23 08:41:28 +08:00
#region BoxCast
2025-02-22 17:25:54 +08:00
[IN(LINE)] public DrawHandler BoxCast(Ray ray, Quaternion rotation, Vector3 size, RaycastHit hit) => BoxCast(ray.origin, ray.direction, rotation, size, hit);
[IN(LINE)]
public DrawHandler BoxCast(Vector3 origin, Vector3 direction, Quaternion rotation, Vector3 size, RaycastHit hit)
{
WireCube(origin, rotation, size * 2f);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
Vector3 end = origin + direction * hit.distance;
WidthOutLine(origin, end, size.x * 2f);
2025-03-03 15:56:04 +08:00
Setup(Color.SetAlpha(ShadowAlphaMultiplier)).Line(origin, end);
2025-02-22 17:25:54 +08:00
WireCube(end, rotation, size * 2f);
2025-03-03 15:56:04 +08:00
RaycastHit(hit);
2025-02-22 17:25:54 +08:00
}
return this;
}
#endregion
2025-02-23 08:41:28 +08:00
#region CapsuleCast
2025-02-22 17:25:54 +08:00
[IN(LINE)]
public DrawHandler CapsuleCast(Vector3 point1, Vector3 point2, Vector3 direction, float radius, RaycastHit hit)
{
Vector3 center = (point1 + point2) * 0.5f;
Quaternion rotation = Quaternion.LookRotation(point2 - point1, Vector3.up);
rotation = rotation * Quaternion.Euler(90, 0, 0);
CapsuleCast(center, direction, rotation, radius, Vector3.Distance(point1, point2) + radius * 2f, hit);
return this;
}
[IN(LINE)]
public DrawHandler CapsuleCast(Ray ray, Vector3 size, Quaternion rotation, float radius, float height, RaycastHit hit)
{
CapsuleCast(ray.origin, ray.direction, rotation, radius, height, hit);
return this;
}
[IN(LINE)]
public DrawHandler CapsuleCast(Vector3 origin, Vector3 direction, Quaternion rotation, float radius, float height, RaycastHit hit)
{
WireCapsule(origin, rotation, radius, height);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
Vector3 end = origin + direction * hit.distance;
WidthOutLine(origin, end, radius * 2f);
2025-03-03 15:56:04 +08:00
Setup(Color.SetAlpha(ShadowAlphaMultiplier)).Line(origin, end);
2025-02-22 17:25:54 +08:00
WireCapsule(end, rotation, radius, height);
2025-03-03 15:56:04 +08:00
RaycastHit(hit);
2025-02-22 17:25:54 +08:00
}
return this;
}
#endregion
2025-03-12 21:49:38 +08:00
#endif
2025-02-23 10:48:25 +08:00
2025-03-12 21:49:38 +08:00
#if DEBUGX_ENABLE_PHYSICS2D
#region RaycastHit2D
[IN(LINE)]
public DrawHandler RaycastHit(RaycastHit2D hit)
{
if (hit.collider != null)
{
DotDiamond(hit.point);
RayArrow(hit.point, hit.normal);
}
return this;
}
[IN(LINE)]
private void RaycastHit_Internal(float offsetZ, RaycastHit2D hit)
{
DotDiamond(new Vector3(hit.point.x, hit.point.y, offsetZ));
RayArrow(new Vector3(hit.point.x, hit.point.y, offsetZ), hit.normal);
}
#endregion
2025-02-23 10:48:25 +08:00
#region Raycast2D
[IN(LINE)] public DrawHandler Raycast2D(Ray ray, RaycastHit2D hit) => Raycast2D(ray.origin, ray.direction, hit);
[IN(LINE)]
2025-02-23 20:03:13 +08:00
public DrawHandler Raycast2D(Vector3 origin, Vector2 direction, RaycastHit2D hit)
2025-02-23 10:48:25 +08:00
{
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
2025-02-23 20:03:13 +08:00
Line(origin, origin + (Vector3)direction * hit.distance);
2025-02-23 10:48:25 +08:00
2025-03-05 10:59:16 +08:00
RaycastHit_Internal(origin.z, hit);
2025-02-23 10:48:25 +08:00
}
return this;
}
#endregion
#region CircleCast2D
private static readonly Vector3 Normal2D = Vector3.forward;
[IN(LINE)] public DrawHandler CircleCast2D(Ray ray, float radius, RaycastHit2D hit) => CircleCast2D(ray.origin, ray.direction, radius, hit);
[IN(LINE)]
2025-02-23 20:03:13 +08:00
public DrawHandler CircleCast2D(Vector3 origin, Vector2 direction, float radius, RaycastHit2D hit)
2025-02-23 10:48:25 +08:00
{
WireCircle(origin, Normal2D, radius);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
2025-02-23 20:03:13 +08:00
Vector3 end = origin + (Vector3)direction * hit.distance;
2025-02-23 10:48:25 +08:00
2025-03-03 15:56:04 +08:00
Line(origin, end);
2025-02-23 10:48:25 +08:00
WireCircle(end, Normal2D, radius);
2025-03-05 10:59:16 +08:00
RaycastHit_Internal(origin.z, hit);
2025-02-23 10:48:25 +08:00
}
return this;
}
#endregion
#region BoxCast2D
[IN(LINE)] public DrawHandler BoxCast2D(Ray ray, float angle, Vector3 size, RaycastHit2D hit) => BoxCast2D(ray.origin, ray.direction, angle, size, hit);
[IN(LINE)]
2025-02-23 20:03:13 +08:00
public DrawHandler BoxCast2D(Vector3 origin, Vector2 direction, float angle, Vector3 size, RaycastHit2D hit)
2025-02-23 10:48:25 +08:00
{
size *= 0.5f;
Quaternion rotation = Quaternion.Euler(0, 0, angle);
WireQuad(origin, rotation, size * 2f);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
2025-02-23 20:03:13 +08:00
Vector3 end = origin + (Vector3)direction * hit.distance;
2025-02-23 10:48:25 +08:00
2025-03-03 15:56:04 +08:00
Line(origin, end);
2025-02-23 10:48:25 +08:00
WireQuad(end, rotation, size * 2f);
2025-03-05 10:59:16 +08:00
RaycastHit_Internal(origin.z, hit);
2025-02-23 10:48:25 +08:00
}
return this;
}
#endregion
#region CapsuleCast2D
[IN(LINE)] public DrawHandler CapsuleCast2D(Ray ray, float angle, Vector2 size, CapsuleDirection2D capsuleDirection, RaycastHit2D hit) => CapsuleCast2D(ray.origin, ray.direction, angle, size, capsuleDirection, hit);
[IN(LINE)]
2025-02-23 20:03:13 +08:00
public DrawHandler CapsuleCast2D(Vector3 origin, Vector2 direction, float angle, Vector2 size, CapsuleDirection2D capsuleDirection, RaycastHit2D hit)
2025-02-23 10:48:25 +08:00
{
var rotation = Quaternion.Euler(0, 0, angle);
var height = (capsuleDirection == CapsuleDirection2D.Vertical ? size.y : size.x);
var radius = (capsuleDirection == CapsuleDirection2D.Vertical ? size.x : size.y) * 0.5f;
WireFlatCapsule(origin, rotation, radius, height);
if (hit.collider == null)
{
RayFade(origin, direction * 3f);
}
else
{
2025-02-23 20:03:13 +08:00
Vector3 end = origin + (Vector3)direction * hit.distance;
2025-02-24 10:59:58 +08:00
2025-03-03 15:56:04 +08:00
Line(origin, end);
2025-02-23 10:48:25 +08:00
WireFlatCapsule(end, rotation, radius, height);
2025-02-24 10:59:58 +08:00
2025-03-05 10:59:16 +08:00
RaycastHit_Internal(origin.z, hit);
2025-02-23 10:48:25 +08:00
}
return this;
}
#endregion
2025-03-12 21:49:38 +08:00
#endif
2025-02-22 17:25:54 +08:00
}
}
2025-03-12 21:49:38 +08:00
}