I am trying to do procedural terrain generation and in order to make the elevation of four biomes blend seamlessly I need to do bilinear interpolation because linear interpolation only works with two biomes blending seamlessly.
Alrighty, it seems that the bilinear interpolation process isn’t too difficult. This article does a really good job of explaining the steps (I’d highly recommend reading it to understand how it works), and the steps can then be converted into a Lua function:
function interpolate(x,y,alpha) --simple linear interpolation
local difference=y-x
local progress=alpha*difference
local result=progress+x
return result
end
function getBilinearValue(value00,value10,value01,value11,xProgress,yProgress)
local top=interpolate(value00,value10,xProgress) --get progress across line A
local bottom=interpolate(value01,value11,yProgress) --get line B progress
local middle=interpolate(top,bottom,yProgress) --get progress of line going
return middle --between point A and point B
end
--an example:
local value00=78
local value10=954
local value01=856
local value11=24
local xProgress=0.35
local yProgress=0.35
local bilinearValue=getBilinearValue(value00,value10,value01,value11,xProgress,yProgress)
print(bilinearValue)
The function I wrote takes six parameters. The first four are the values of the corners, and the last two are floats representing how far between the top left and bottom right the point is.