mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-04-18 21:17:17 -05:00
64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using UnityEngine;
|
|
|
|
|
|
namespace SphereTriangle
|
|
{
|
|
public class TriangleCapsuleGizmo : MonoBehaviour
|
|
{
|
|
public Transform B;
|
|
public Transform C;
|
|
public SphereCapsuleCollider Capsule;
|
|
|
|
void Reset()
|
|
{
|
|
for (int i = 0; i < transform.childCount; ++i)
|
|
{
|
|
switch (i)
|
|
{
|
|
case 0:
|
|
if (this.B == null) B = transform.GetChild(i);
|
|
break;
|
|
|
|
case 1:
|
|
if (this.C == null) C = transform.GetChild(i);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
[SerializeField]
|
|
TriangleCapsuleCollisionSolver _solver = new();
|
|
|
|
public void OnDrawGizmos()
|
|
{
|
|
if (B == null) return;
|
|
if (C == null) return;
|
|
var t = new Triangle(transform.position, B.position, C.position);
|
|
|
|
if (Capsule?.Tail == null) return;
|
|
var capsule = new LineSegment(Capsule.transform.position, Capsule.Tail.position);
|
|
|
|
_solver.BeginFrame();
|
|
var result = _solver.Collide(t, Capsule, capsule, Capsule.Radius);
|
|
var type = result.TryGetClosest(out var l);
|
|
if (!type.HasValue)
|
|
{
|
|
Gizmos.color = Color.gray;
|
|
t.DrawGizmos();
|
|
return;
|
|
}
|
|
|
|
_solver.DrawGizmos(t, 1.0f, Capsule.Radius);
|
|
|
|
Gizmos.color = Color.magenta;
|
|
Gizmos.DrawLine(l.Start, l.End);
|
|
Gizmos.DrawSphere(l.End, 0.01f);
|
|
Gizmos.DrawWireSphere(l.Start, 0.02f);
|
|
|
|
var delta = l.Vector.normalized * (Capsule.Radius - l.Length);
|
|
Gizmos.color = Color.green;
|
|
Gizmos.DrawLine(l.End, l.End + delta);
|
|
Gizmos.DrawSphere(l.End + delta, 0.01f);
|
|
}
|
|
}
|
|
} |