using UnityEngine; using BrewMonster.Scripts; namespace BrewMonster { /// /// Utility functions for hook position/rotation calculation /// 挂点位置/旋转计算的工具函数 /// public static class HookUtils { /// /// Get world position from hook Transform with offset /// 从挂点变换获取世界位置(带偏移) /// /// Hook Transform / 挂点变换 /// Offset vector / 偏移向量 /// If true, offset is relative to hook's local space; if false, absolute / 如果为true,偏移相对于挂点局部空间;如果为false,绝对偏移 /// Model transform for absolute offset calculation (optional) / 用于绝对偏移计算的模型变换(可选) /// World position / 世界位置 public static Vector3 GetHookWorldPosition(Transform hookTransform, Vector3 offset, bool bRelative, Transform modelTransform = null) { if (hookTransform == null) return Vector3.zero; if (bRelative) { // Relative offset: transform offset from hook's local space to world space // 相对偏移:将偏移从挂点的局部空间变换到世界空间 // C++ equivalent: pHook->GetAbsoluteTM() * pOffset return hookTransform.TransformPoint(offset); } else { // Absolute offset: transform offset in model space, then translate to hook position // 绝对偏移:在模型空间中变换偏移,然后平移到挂点位置 // C++ equivalent: // vPos = pSkin->GetAbsoluteTM() * pOffset; // vPos = vPos - pSkin->GetAbsoluteTM().GetRow(3) + pHook->GetAbsoluteTM().GetRow(3); // Get model transform (skeleton root or provided) // 获取模型变换(骨架根或提供的) if (modelTransform == null) { // Fallback: find SkeletonBuilder in hierarchy and use root bone // 回退:在层次结构中查找SkeletonBuilder并使用根骨骼 SkeletonBuilder skeleton = hookTransform.GetComponentInParent(); modelTransform = skeleton != null ? skeleton.rootBone : hookTransform.root; } // Transform offset in model's world space // 在模型的世界空间中变换偏移 Vector3 offsetWorld = modelTransform.TransformPoint(offset); // Translate to hook position // 平移到挂点位置 return offsetWorld - modelTransform.position + hookTransform.position; } } /// /// Get world rotation from hook Transform /// 从挂点变换获取世界旋转 /// /// Hook Transform / 挂点变换 /// World rotation / 世界旋转 public static Quaternion GetHookWorldRotation(Transform hookTransform) { if (hookTransform == null) return Quaternion.identity; return hookTransform.rotation; } /// /// Get world direction from hook Transform /// 从挂点变换获取世界方向 /// /// Hook Transform / 挂点变换 /// Forward direction in world space / 世界空间的前方向 public static Vector3 GetHookWorldForward(Transform hookTransform) { if (hookTransform == null) return Vector3.forward; return hookTransform.forward; } } }