HLSL:Unitys Vector3.RotateTowards(…)

问题描述 投票:0回答:1

我需要在计算着色器中将方向向量向另一个具有最大角度的方向旋转,就像Vector3.RotateTowards(from,to,maxAngle,0)函数一样。这需要在计算着色器内部发生,因为出于性能方面的考虑,我无法从GPU发送所需的值。关于如何实施此建议?

unity3d vector rotation hlsl angle
1个回答
0
投票

这是从this post on the Unity forums by vc1001this shadertoy entry by 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)));
}
© www.soinside.com 2019 - 2024. All rights reserved.