3D (Trilinear) Interpolation

I’m currently trying to optimize my terrain generation, but I’m having issues with interpolation. I’m using both 2D and 3D fractal noise, and while my 2D (bilinear) interpolation of the 2D noise definitely helped, my attempt at interpolating the 3D noise somehow ended up making the time almost worse.

I was using this image as a reference:

trilinearfig

This is my code to try and replicate that:

function interpolate1D(startPoint,startValue,endPoint,endValue,input)
	if startPoint == endPoint then
		return startValue
	end
	local t = (input-startPoint)/(endPoint-startPoint)
	local interpolatedValue = startValue+t*(endValue-startValue)
	t=nil
	return interpolatedValue
end
function interpolate3D(points,position)
	local x0 = math.floor(position.X/4)*4
	local x1 = math.ceil(position.X/4)*4
	local y0 = math.floor(position.Y/4)*4
	local y1 = math.ceil(position.Y/4)*4
	local z0 = math.floor(position.Z/4)*4
	local z1 = math.ceil(position.Z/4)*4

	local c011,c111 = points[x0][y1][z1],points[x1][y1][z1]
	local c010,c110 = points[x0][y1][z0],points[x1][y1][z0]
	local c001,c101 = points[x0][y0][z1],points[x1][y0][z1]
	local c000,c100 = points[x0][y0][z0],points[x1][y0][z0]

	local a = interpolate1D(x0,c010,x1,c110,position.X)
	local b = interpolate1D(x0,c000,x1,c100,position.X)
	local c = interpolate1D(x0,c011,x1,c111,position.X)
	local d = interpolate1D(x0,c001,x1,c101,position.X)

	local e = interpolate1D(y0,a,y1,b,position.Y)
	local f = interpolate1D(y0,c,y1,d,position.Y)

	local g = interpolate1D(z0,e,z1,f,position.Z)

	x0,x1,y0,y1,z0,z1,c011,c111,c010,c110,c001,c101,c000,c100,a,b,c,d,e,f = nil

	return g
end

I’m wondering if anyone has any idea why this might be slower than the more complex 3D fractal noise?

Currently it takes a Roblox server around 6-8 seconds to generate a 8x8 chunk area (each chunk being 16x16 blocks on the x and z axis).

In Studio, it only takes 3-5 seconds. I know Studio’s test mode is done locally, and it’s also doing the client in addition to the server. Though obviously I care more about it running faster on Roblox’s servers.