| 1 | package net.minecraft.world.attribute;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.util.ARGB;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import org.joml.Vector3fc;
|
| 9 | import org.joml.Vector4fc;
|
| 10 |
|
| 11 | public interface LerpFunction<T> {
|
| 12 | LerpFunction<?> CONSTANT = ofStep(1.0F);
|
| 13 |
|
| 14 | static LerpFunction<Float> ofFloat() {
|
| 15 | return Mth::lerp;
|
| 16 | }
|
| 17 |
|
| 18 | static LerpFunction<Integer> ofInteger() {
|
| 19 | return Mth::lerpInt;
|
| 20 | }
|
| 21 |
|
| 22 | static LerpFunction<Float> ofDegrees(final float maxDelta) {
|
| 23 | return (alpha, from, to) -> {
|
| 24 | float delta = Mth.wrapDegrees(to - from);
|
| 25 | return Math.abs(delta) >= maxDelta ? to : from + alpha * delta;
|
| 26 | };
|
| 27 | }
|
| 28 |
|
| 29 | static <T> LerpFunction<T> ofConstant() {
|
| 30 | return (LerpFunction<T>)CONSTANT;
|
| 31 | }
|
| 32 |
|
| 33 | static <T> LerpFunction<T> ofStep(final float threshold) {
|
| 34 | return (alpha, from, to) -> alpha >= threshold ? to : from;
|
| 35 | }
|
| 36 |
|
| 37 | static LerpFunction<Integer> ofColor() {
|
| 38 | return ARGB::srgbLerp;
|
| 39 | }
|
| 40 |
|
| 41 | static LerpFunction<Vector3fc> ofColorVec3() {
|
| 42 | return ARGB::srgbLerp;
|
| 43 | }
|
| 44 |
|
| 45 | static LerpFunction<Vector4fc> ofColorVec4() {
|
| 46 | return ARGB::srgbLerp;
|
| 47 | }
|
| 48 |
|
| 49 | static <T> LerpFunction<List<T>> ofListCrossFade(final LerpFunction.AlphaScaler<T> scaler) {
|
| 50 | return (alpha, from, to) -> {
|
| 51 | if (alpha == 0.0F) {
|
| 52 | return from;
|
| 53 | }
|
| 54 |
|
| 55 | if (alpha == 1.0F) {
|
| 56 | return to;
|
| 57 | }
|
| 58 |
|
| 59 | Builder<T> builder = ImmutableList.builderWithExpectedSize(from.size() + to.size());
|
| 60 |
|
| 61 | for (T element : from) {
|
| 62 | builder.add(scaler.apply(element, 1.0F - alpha));
|
| 63 | }
|
| 64 |
|
| 65 | for (T element : to) {
|
| 66 | builder.add(scaler.apply(element, alpha));
|
| 67 | }
|
| 68 |
|
| 69 | return builder.build();
|
| 70 | };
|
| 71 | }
|
| 72 |
|
| 73 | T apply(float alpha, T from, T to);
|
| 74 |
|
| 75 | @FunctionalInterface
|
| 76 | interface AlphaScaler<T> {
|
| 77 | T apply(T item, float alpha);
|
| 78 | }
|
| 79 | }
|
| 80 |
|