Hopefully this is a simple one, I just can't get my mind around the formula and there might even be some builtin function for it. I want to create a sort of 1D gradient in the code: A dictionary of value maps which translate the value of a variable into a blend of linearly interpolated ranges. If anyone's curious it's for the density of my chunk terrain generator to allow defining how sharp the noise should be at different height values, like say between a position.y of -50 and +50 I want a density transition from 0.25 to 0.75.

For example: Let's say I have the following dictionary of maps:

density = {
    "-20" = "1"
    "-10" = "0.5"
    "0" = "0"
    "10" = "1"
    "20" = "0.5"
}

When the value of x is translated, the result will be whatever in-between of its value lies in that dictionary. For instance: If x is 5 result is 0.5, if x is 15 result is 0.75, if x is -15 result is 0.75. What's the best way to calculate this translation?

I want to create a sort of 1D gradient in the code

Godot has a Gradient resource which you can use for this purpose :) It stores Colors, but nothing prevents you from only reading the red channel (for instance).

You could also use the Curve resource if you need more control over tangents.

inverse_lerp() and lerp_range() global scope methods are also useful, but for blending between multiple points, nothing beats Gradient.

Thanks. Seems the best solution is using the curve object provided by default, which is pretty much a gradient but fully mathematical which is everything I need:

https://docs.godotengine.org/en/latest/classes/class_curve.html

Only problem is the curve points only seem to range between 0 and 1 even if their values can be any value, but I can work around that with multipliers and a separate range mapping system.

6 months later