From df49cc5e1eec60cb62148e9f9653eb153c860bff Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 18 Jul 2025 17:27:48 +0900 Subject: [PATCH] Update Assets/UniGLTF/Runtime/SpringBoneJobs/Anglelimit/Anglelimit.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../SpringBoneJobs/Anglelimit/Anglelimit.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Assets/UniGLTF/Runtime/SpringBoneJobs/Anglelimit/Anglelimit.cs b/Assets/UniGLTF/Runtime/SpringBoneJobs/Anglelimit/Anglelimit.cs index 353a8547b..b91e3bd77 100644 --- a/Assets/UniGLTF/Runtime/SpringBoneJobs/Anglelimit/Anglelimit.cs +++ b/Assets/UniGLTF/Runtime/SpringBoneJobs/Anglelimit/Anglelimit.cs @@ -66,17 +66,24 @@ namespace UniGLTF.SpringBoneJobs // https://discussions.unity.com/t/unity-mathematics-equivalent-to-quaternion-fromtorotation/237459 public static quaternion fromToQuaternion(in float3 from, in float3 to) { + var fromNorm = math.normalize(from); + var toNorm = math.normalize(to); + var dot = math.dot(fromNorm, toNorm); + + // Handle the case where from and to are parallel but opposite + if (math.abs(dot + 1f) < 1e-6f) // dot is approximately -1 + { + // Find a perpendicular axis + var perpAxis = math.abs(fromNorm.x) > math.abs(fromNorm.z) + ? new float3(-fromNorm.y, fromNorm.x, 0f) + : new float3(0f, -fromNorm.z, fromNorm.y); + return quaternion.AxisAngle(math.normalize(perpAxis), math.PI); + } + + // General case return quaternion.AxisAngle( - angle: math.acos( - math.clamp( - math.dot( - math.normalize(from), - math.normalize(to) - ), - -1f, - 1f) - ), - axis: math.normalize(math.cross(from, to)) + angle: math.acos(math.clamp(dot, -1f, 1f)), + axis: math.normalize(math.cross(fromNorm, toNorm)) ); }