Unity-DebugX/Samples/Scripts/DebugXSample_Raycasts3D.cs

70 lines
2.5 KiB
C#
Raw Normal View History

2025-02-22 23:02:05 +08:00
using UnityEngine;
2025-02-24 10:59:58 +08:00
namespace DCFApixels.DebugXCore.Samples
2025-02-22 23:02:05 +08:00
{
2025-02-23 20:03:13 +08:00
[SelectionBase]
2025-02-23 10:48:25 +08:00
public class DebugXSample_Raycasts3D : MonoBehaviour
2025-02-22 23:02:05 +08:00
{
public Gradient Gradient;
public float GradientMultiplier = 5;
public Transform[] Points;
2025-03-12 21:49:38 +08:00
2025-02-23 20:03:13 +08:00
#if UNITY_EDITOR
2025-02-22 23:02:05 +08:00
private void OnDrawGizmos()
2025-02-23 20:03:13 +08:00
{
Draw();
}
#else
private void Update()
{
Draw();
}
#endif
private void Draw()
2025-02-22 23:02:05 +08:00
{
2025-03-12 21:49:38 +08:00
#if DEBUGX_ENABLE_PHYSICS3D
2025-02-22 23:02:05 +08:00
int i = 0;
const float RADIUS_M = 0.5f;
Transform point;
Ray ray;
RaycastHit hit;
point = Points[i++];
ray = new Ray(point.position, point.forward);
Physics.Raycast(ray, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).Raycast(ray, hit);
point = Points[i++];
ray = new Ray(point.position, point.forward);
Physics.SphereCast(ray.origin, point.localScale.x * RADIUS_M, ray.direction, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).SphereCast(ray, point.localScale.x * RADIUS_M, hit);
point = Points[i++];
ray = new Ray(point.position, point.forward);
Physics.BoxCast(ray.origin, point.localScale * RADIUS_M, ray.direction, out hit, point.rotation, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).BoxCast(ray, point.rotation, point.localScale * RADIUS_M, hit);
point = Points[i++];
ray = new Ray(point.position, point.forward);
Vector3 point1 = ray.origin + point.up * point.localScale.y * RADIUS_M * 0.5f;
Vector3 point2 = ray.origin + point.up * point.localScale.y * RADIUS_M * -0.5f;
Physics.CapsuleCast(point1, point2, point.localScale.x * RADIUS_M, ray.direction, out hit, float.PositiveInfinity, int.MaxValue, QueryTriggerInteraction.UseGlobal);
DebugX.Draw(GetColor(point)).CapsuleCast(point1, point2, ray.direction, point.localScale.x * RADIUS_M, hit);
2025-03-12 21:49:38 +08:00
#endif
2025-02-22 23:02:05 +08:00
}
private Color GetColor(Transform pos1)
{
Vector3 pos = pos1.localPosition;
pos /= GradientMultiplier == 0 ? 1 : GradientMultiplier;
pos += Vector3.one * 0.5f;
float t = pos.x + pos.y + pos.z;
t /= 3f;
return Gradient.Evaluate(Mathf.Clamp01(t));
}
}
}