HLSL: Unitys Vector3.RotateTowards(...)



我需要在计算着色器中以最大角度将方向向量向另一个方向向量旋转,就像 Vector3.RotateTowards(from, to, maxAngle, 0( 函数一样。这需要在计算着色器内部进行,因为出于性能原因,我无法从 GPU 发送所需的值或向 GPU 发送所需的值。关于如何实现这一点的任何建议?

本文改编自vc1001在 Unity 论坛上的这篇文章和demofox的这个着色器条目的组合。我还没有测试过这个,自从我完成 HLSL/cg 编码以来已经有一段时间了,如果有错误,sop lease 让我知道——尤其是语法错误。

float3 slerp(float3 current, float3 target, float maxAngle)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = dot(current, target);     
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
dot = clamp(dot, -1, 1);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float delta = acos(dot);
float theta = min(1.0f, maxAngle / delta);
float3 relativeVec = normalize(target - current*dot); // Orthonormal basis
float3 slerped = ((start*cos(theta)) + (relativeVec*sin(theta)));
}

最新更新