我一直在尝试在 C# 中的高度图(或高度场)类中实现双线性插值,但并不真正了解 Wiki 上的数学。这是 heightfield 类的实现。我有第二个函数,Mathf.BilinearInterpolation(...),它会抛出 NotImplementedException。
/// <summary>
/// A flattened 2D array which contains the surface elevation data.
/// </summary>
public float[] Elevation { get; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public float ElevationAtVertex(int x, int y)
{
return Elevation[x + y + (y * Length)];
}
public float ElevationAtPoint(float x, float y)
{
float f1 = ElevationAtVertex(Mathf.Floor(x), Mathf.Floor(y));
float f2 = ElevationAtVertex(Mathf.Floor(x), Mathf.Ceiling(y));
float f3 = ElevationAtVertex(Mathf.Ceiling(x), Mathf.Floor(y));
float f4 = ElevationAtVertex(Mathf.Ceiling(x), Mathf.Ceiling(y));
return Mathf.BilinearInterpolation(f1, f2, f3, f4);
}