What does the alpha in 'Lerp' mean?

Hi there, I’m trying to lerp between two numbers, but the issue is, is that it only works for Vector3s and Color3s, etc. To solve this, I have used a custom function of Lerping, but I do not understand the ‘Alpha’ number in Lerp. E.x

local function lerp(a, b, t)
	return a + (b-a)*t
end

lerp(0,1,0.5) -- the third number is what I'm asking about.

What is alpha and how does it work in this case?

It’s a value between 0 and 1 which is the percentage in which to interpolate from a to b. If the value is 0.5 as it is in your example, it will be 50% between a and b. Or in your example, 0.5.

2 Likes

So if alpha was 0.2, it goes in increments of 0.2? I’m sorry if I do not understand, but are what some examples?

Not necessarily increments, the problem with using 0 and 1 as a and b is that it creates this notion that it works in increments. It isn’t an increment, it’s the percent which to interpolate between a and b. Think of it kinda like a progress bar. A progress bar interpolates between 0 and 100 (usually). We’re doing the same thing, except we’re changing the 0 and 100 to whatever we want. If a progress bar is at 50%, the alpha value would be 0.5, this is because 50 is 50% from 0 to 100. Here’s a table with random examples:

a b alpha result
604 1394 0.6 1078.0
953 1130 0.5 1041.5
260 495 0.2 307.0
46 110 0.7 90.8
584 1344 0.1 660.0
205 453 0.7 378.6
411 861 0.7 726.0
325 872 0.4 543.8
623 875 0.7 799.4
3 161 0.0 3.0
763 1346 0.4 996.2
478 1127 0.1 542.9
564 722 0.1 579.8
39 875 0.6 540.6
946 948 0.1 946.2
697 1558 0.9 1471.9
124 409 0.5 266.5
412 1091 0.7 887.3
666 1392 0.5 1029.0
40 388 0.8 318.4
77 325 0.0 77.0
667 737 0.3 688.0
985 1307 0.9 1274.8
694 855 0.1 710.1
222 698 0.1 269.6

In these examples, the result is always alpha*100% from a to b. So in our last example, 269.6 is 10% from 222 to 698

3 Likes

I see now. What if I wanted to lerp between 2 numbers constantly? Say I’m using playbackloudness in this scenario and I wanted to lerp between 2 numbers based off that?

PlaybackLoudness uses 0-1000. So if you were to lerp with an alpha of 0.5, it’d be 500. If that’s all you need, lerping between 0-n is just alpha*n. Can you explain a bit more about what you’re trying to accomplish?

1 Like

So what I’m trying to accomplish here, is to lerp between two fov’s with the alpha based off playbackloudness. The thing is, is that if I were to have alpha based of playbackloudness, and have min and max from 0-10, it returns 0. Even when this is used in a renderstepped function. I have managed to lerp using color3’s, but not with single values as they are more confusing to me.

E.x:

rs.RenderStepped:Connect(function()
	lerp(0,1,sound.PlaybackLoudness)
	print(lerp) -- returns 0
end)

Note that the lerp function is here is also made outside.

The only reason lerp(0, 1, alpha) would return 0 is if alpha was 0. Have you ensured sound.PlaybackLoudness isn’t 0? If you wanted a value from 0-10 based off of playbackloudness you should use something along the lines of lerp(0, 10, sound.PlaybackLoudness/1000) this is because PlaybackLoudness has a max of 1000 as defined here. If you divide by the max value that will give you a value between 0 and 1, which is what lerp expects.

1 Like

This is really interesting, I’m not getting values but instead weird numbers?
image

local function lerp(a, b, t)
	return a + (b-a)*t
end


local loudness = sound.PlaybackLoudness

rs.RenderStepped:Connect(function()
	lerp(0,10,sound.PlaybackLoudness/1000)
	print(lerp) -- returns 0
end)

You’re printing the function which is a memory address.

Try

rs.RenderStepped:Connect(function()
	print(lerp(0,10,sound.PlaybackLoudness/1000))
end)
1 Like

I see now, mb. I’m getting values around 1.5. Is this is the initial number I could use with my FOV, if I decreased the divisor?

I’m still a little confused at what you’re trying to do with the fov. Are you trying to make a loud sound increase/decrease the player’s FOV?

1 Like

So what I’m exactly trying to do, is to lerp between two numbers, which control the CurrentCamera’s fov, 70 and 90. Based off the loudness, if the music louder the number obviously increases between 70 and 90, and down is vice versa.

So if you want numbers between 70 and 90, why not lerp between 70 and 90 rather than 0 and 10? i.e.

workspace.CurrentCamera.FieldOfView = lerp(70,90,sound.PlaybackLoudness/1000)
1 Like

I see. How could I also clamp the lerp function

Would I do it like this?

rs.RenderStepped:Connect(function()
	local change = lerp(70,90,sound.PlaybackLoudness/1000)
	camera.FieldOfView = math.clamp(change, 70, 90)
end)

Lerp is clamped between a and b provided your alpha value is between 0 and 1. However, if you want to guarantee it to be clamped if your alpha value ever goes beyond 0 and 1 you have a few options. You can clamp the result:

math.clamp(lerp(70,90,sound.PlaybackLoudness/1000), 70, 90))

You can change the lerp function in a couple ways:
1:

local function lerp(a, b, t)
	return math.clamp(a + (b-a)*t, a, b)
end

2:

local function lerp(a, b, t)
	return a + (b-a)*math.clamp(t, 0, 1)
end

But provided your alpha value is always between 0 and 1, lerp will always return a value clamped between a and b regardless.

1 Like

Ohhh, I understand now! This is my current script, thank you very much for your help!

local rs  = game:GetService('RunService')
local sound  = workspace:WaitForChild('Sound')
local camera = workspace.CurrentCamera

local function lerp(a, b, t)
	return a + (b-a)*math.clamp(t, 0, 1)
end

local on = true

local loudness = sound.PlaybackLoudness

rs.RenderStepped:Connect(function()
	if on then
		local change = lerp(70,75,sound.PlaybackLoudness/1000)
		camera.FieldOfView = change
	end
end)