Unity3D 使用两个向量和介于两者之间的点计算插值级别



我有三个vector3变量;开始,结束和中间的某个点。我知道在正常的线性插值中,您会给出起点和终点以及一个"t"变量,例如"0.6",它返回沿路径该点的位置。

我想做的是传递起点和终点以及一个位置并返回"t"值,因此例如,我可能会在刚好超过一半的位置传递并返回"0.6"。

我知道这有点奇怪,我可能在解释它方面做得很差,但任何帮助将不胜感激。

public float ReverseLerp(Vector3 start, Vector3 end, Vector3 point)
{
float tx = (1f - 0f) / (end.x - start.x) * (point.x - end.x) + 1f;
float ty = (1f - 0f) / (end.y - start.y) * (point.y - end.y) + 1f;
float tz = (1f - 0f) / (end.z - start.z) * (point.z - end.z) + 1f;
float totalClamp = tx + ty + tz;
float weight = (1f - 0f) / (3f - 0f) * (totalClamp - 3f) + 1f;
weight = ((weight - 0f) / (1f - 0f)) * (1f - 0f) + 0f;
weight = Round(weight, 2);
return weight;
}

如果你能假设第三个点在前两个点之间

float MagA = (Position2 - Position1).Magnitude;
float MagB = (LerpedVector - Position1).Magnitude;
float fraction =  MagB/MagA;

相关内容

最新更新