Files
2026-03-03 15:26:21 +07:00

90 lines
4.1 KiB
C#

using UnityEngine;
using BrewMonster.Scripts;
namespace BrewMonster
{
/// <summary>
/// Utility functions for hook position/rotation calculation
/// 挂点位置/旋转计算的工具函数
/// </summary>
public static class HookUtils
{
/// <summary>
/// Get world position from hook Transform with offset
/// 从挂点变换获取世界位置(带偏移)
/// </summary>
/// <param name="hookTransform">Hook Transform / 挂点变换</param>
/// <param name="offset">Offset vector / 偏移向量</param>
/// <param name="bRelative">If true, offset is relative to hook's local space; if false, absolute / 如果为true,偏移相对于挂点局部空间;如果为false,绝对偏移</param>
/// <param name="modelTransform">Model transform for absolute offset calculation (optional) / 用于绝对偏移计算的模型变换(可选)</param>
/// <returns>World position / 世界位置</returns>
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<SkeletonBuilder>();
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;
}
}
/// <summary>
/// Get world rotation from hook Transform
/// 从挂点变换获取世界旋转
/// </summary>
/// <param name="hookTransform">Hook Transform / 挂点变换</param>
/// <returns>World rotation / 世界旋转</returns>
public static Quaternion GetHookWorldRotation(Transform hookTransform)
{
if (hookTransform == null)
return Quaternion.identity;
return hookTransform.rotation;
}
/// <summary>
/// Get world direction from hook Transform
/// 从挂点变换获取世界方向
/// </summary>
/// <param name="hookTransform">Hook Transform / 挂点变换</param>
/// <returns>Forward direction in world space / 世界空间的前方向</returns>
public static Vector3 GetHookWorldForward(Transform hookTransform)
{
if (hookTransform == null)
return Vector3.forward;
return hookTransform.forward;
}
}
}