Vector Bilinear Interpolation (of a square grid)

Hi,

I’ve been trying to get kind of a straight answer on how to bilinearly interpolate vectors, but “googling” is only getting me so far. Or maybe I’m just missing something. Preferably I’d like to learn how to do it using Unity’s API, but if that’s not possible, I’m up for any other options.

The problem:

Lets say I have a grid cell (on a standard square grid of points – Cartesian coordinates I believe they are called). So lets say I have four points making up a cell: Their coordinates are (0,0,0), (1,0,0), (1,0,1) and (0,0,1) – so essentially a square cell with it’s bottom-left point at (0,0,0). So far, all I need is the x-z plane, I’m leaving out the y-axis, as it’s not needed at this time. So technically I could use Vector2’s as well – either Vector3 or Vector2 would be fine, but I just need to bilinearly interpolate.

At each of these points I have a vector, and each vector could be anything, any direction, with any magnitude. Make up whatever you wish. If, for example, I have a unit at a coordinate point within this square cell, how could I bilinearly interpolate the four vectors (at the four corners given above) – in order to calculate what the units “combination vector” should be? Again, the unit’s location would be contained IN the cell boundaries.

So far everything I’ve looked at gives how to linearly or bilinearly interpolate coordinate points, but NOT vectors. I’m trying to learn how to interpolate vectors themselves.

Thanks for any help, or let me know if you need more explanation. Thanks!

I’m not sure there’s anything built into the Vector types that make this any easier natively, but afaik bilinear interpolation is just the resultant of two linear functions. First between the two X components, then the Y’s at the resultant of X. I might be mistaken that it’s that simple?

edit: Right, it does get a little funny with Vectors, doesn’t it? I’ll be curious to see the keenest solution, and will try to spend some time hunting one myself.

C#

// Given a (u,v) coordinate that defines a 2D local position inside a planar quadrilateral, find the 
// absolute 3D (x,y,z) coordinate at that location.
//
//  0 <----u----> 1
//  a ----------- b    0
//  |             |   /|\
//  |             |    |
//  |             |    v
//  |  *(u,v)     |    |
//  |             |   \|/
//  d------------ c    1
//
// a, b, c, and d are the vertices of the quadrilateral. They are assumed to exist in the 
// same plane in 3D space, but this function will allow for some non-planar error.
// 
// Variables u and v are the two-dimensional local coordinates inside the quadrilateral.
// To find a point that is inside the quadrilateral, both u and v must be between 0 and 1 inclusive.  
// For example, if you send this function u=0, v=0, then it will return coordinate "a".  
// Similarly, coordinate u=1, v=1 will return vector "c". Any values between 0 and 1
// will return a coordinate that is bi-linearly interpolated between the four vertices.

public Vector3 QuadLerp(Vector3 a, Vector3 b, Vector3 c, Vector3 d, float u, float v)
{
   Vector3 abu = Vector3.Lerp(a, b, u);
   Vector3 dcu = Vector3.Lerp(d, c, u);
   return Vector3.Lerp(abu, dcu, v);
}
7 Likes

Awesome, thanks Brian. I havn’t implemented it yet, but hopefully it works – as it definitely looks like it should.

If I remember, I’ll post back whether or not it did. I don’t know where you got that function, but that’s great info. Thank you.

Anyone who’s ever tried implementing noise (simplex/ perline etc) or splines will be familiar with this kind of stacked lerp.
More fun to multiply it out by hand to
V = (1-v)((1-u)a+ub) + v((1-u)c+ud)
to
V = a-ua+ub-va+vua-vub+vc-vuc+vud

1 Like

Just wanted to say that the function is working (as far as I know). I just implemented it into my code, and stuff seems to be interpolating correctly :sunglasses:

Thanks again.

1 Like