我需要在计算着色器中将一个方向向量旋转到另一个具有最大角度的方向向量,就像Vector3.RotateTowards(from,to,maxAngle,0)函数所做的那样。这需要在计算着色器内部发生,因为出于性能原因,我无法将所需的值发送到GPU。对如何实现这一点有什么建议吗?
发布于 2020-06-11 16:56:12
这是从this post on the Unity forums by 和this shadertoy entry by 的组合改编而来的。我还没有测试过这一点,而且我做HLSL/cg编码已经有一段时间了,如果有bugs-特别是语法错误,请让我知道。
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)));
}
https://stackoverflow.com/questions/62267444
复制相似问题