| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 |
|
| 9 | public interface EasingType {
|
| 10 | ExtraCodecs.LateBoundIdMapper<String, EasingType> SIMPLE_REGISTRY = new ExtraCodecs.LateBoundIdMapper<>();
|
| 11 | Codec<EasingType> CODEC = Codec.either(SIMPLE_REGISTRY.codec(Codec.STRING), EasingType.CubicBezier.CODEC)
|
| 12 | .xmap(Either::unwrap, easing -> easing instanceof EasingType.CubicBezier bezier ? Either.right(bezier) : Either.left(easing));
|
| 13 | EasingType CONSTANT = registerSimple("constant", x -> 0.0F);
|
| 14 | EasingType LINEAR = registerSimple("linear", x -> x);
|
| 15 | EasingType IN_BACK = registerSimple("in_back", Ease::inBack);
|
| 16 | EasingType IN_BOUNCE = registerSimple("in_bounce", Ease::inBounce);
|
| 17 | EasingType IN_CIRC = registerSimple("in_circ", Ease::inCirc);
|
| 18 | EasingType IN_CUBIC = registerSimple("in_cubic", Ease::inCubic);
|
| 19 | EasingType IN_ELASTIC = registerSimple("in_elastic", Ease::inElastic);
|
| 20 | EasingType IN_EXPO = registerSimple("in_expo", Ease::inExpo);
|
| 21 | EasingType IN_QUAD = registerSimple("in_quad", Ease::inQuad);
|
| 22 | EasingType IN_QUART = registerSimple("in_quart", Ease::inQuart);
|
| 23 | EasingType IN_QUINT = registerSimple("in_quint", Ease::inQuint);
|
| 24 | EasingType IN_SINE = registerSimple("in_sine", Ease::inSine);
|
| 25 | EasingType IN_OUT_BACK = registerSimple("in_out_back", Ease::inOutBack);
|
| 26 | EasingType IN_OUT_BOUNCE = registerSimple("in_out_bounce", Ease::inOutBounce);
|
| 27 | EasingType IN_OUT_CIRC = registerSimple("in_out_circ", Ease::inOutCirc);
|
| 28 | EasingType IN_OUT_CUBIC = registerSimple("in_out_cubic", Ease::inOutCubic);
|
| 29 | EasingType IN_OUT_ELASTIC = registerSimple("in_out_elastic", Ease::inOutElastic);
|
| 30 | EasingType IN_OUT_EXPO = registerSimple("in_out_expo", Ease::inOutExpo);
|
| 31 | EasingType IN_OUT_QUAD = registerSimple("in_out_quad", Ease::inOutQuad);
|
| 32 | EasingType IN_OUT_QUART = registerSimple("in_out_quart", Ease::inOutQuart);
|
| 33 | EasingType IN_OUT_QUINT = registerSimple("in_out_quint", Ease::inOutQuint);
|
| 34 | EasingType IN_OUT_SINE = registerSimple("in_out_sine", Ease::inOutSine);
|
| 35 | EasingType OUT_BACK = registerSimple("out_back", Ease::outBack);
|
| 36 | EasingType OUT_BOUNCE = registerSimple("out_bounce", Ease::outBounce);
|
| 37 | EasingType OUT_CIRC = registerSimple("out_circ", Ease::outCirc);
|
| 38 | EasingType OUT_CUBIC = registerSimple("out_cubic", Ease::outCubic);
|
| 39 | EasingType OUT_ELASTIC = registerSimple("out_elastic", Ease::outElastic);
|
| 40 | EasingType OUT_EXPO = registerSimple("out_expo", Ease::outExpo);
|
| 41 | EasingType OUT_QUAD = registerSimple("out_quad", Ease::outQuad);
|
| 42 | EasingType OUT_QUART = registerSimple("out_quart", Ease::outQuart);
|
| 43 | EasingType OUT_QUINT = registerSimple("out_quint", Ease::outQuint);
|
| 44 | EasingType OUT_SINE = registerSimple("out_sine", Ease::outSine);
|
| 45 |
|
| 46 | static EasingType registerSimple(final String id, final EasingType easing) {
|
| 47 | SIMPLE_REGISTRY.put(id, easing);
|
| 48 | return easing;
|
| 49 | }
|
| 50 |
|
| 51 | static EasingType cubicBezier(final float x1, final float y1, final float x2, final float y2) {
|
| 52 | return new EasingType.CubicBezier(new EasingType.CubicBezierControls(x1, y1, x2, y2));
|
| 53 | }
|
| 54 |
|
| 55 | static EasingType symmetricCubicBezier(final float x1, final float y1) {
|
| 56 | return cubicBezier(x1, y1, 1.0F - x1, 1.0F - y1);
|
| 57 | }
|
| 58 |
|
| 59 | float apply(float x);
|
| 60 |
|
| 61 | final class CubicBezier implements EasingType {
|
| 62 | public static final Codec<EasingType.CubicBezier> CODEC = RecordCodecBuilder.create(
|
| 63 | i -> i.group(EasingType.CubicBezierControls.CODEC.fieldOf("cubic_bezier").forGetter(b -> b.controls)).apply(i, EasingType.CubicBezier::new)
|
| 64 | );
|
| 65 | private static final int NEWTON_RAPHSON_ITERATIONS = 4;
|
| 66 | private static final float MAX_STEP = 0.25F;
|
| 67 | private final EasingType.CubicBezierControls controls;
|
| 68 | private final EasingType.CubicBezier.CubicCurve xCurve;
|
| 69 | private final EasingType.CubicBezier.CubicCurve yCurve;
|
| 70 |
|
| 71 | public CubicBezier(final EasingType.CubicBezierControls controls) {
|
| 72 | this.controls = controls;
|
| 73 | this.xCurve = curveFromControls(controls.x1, controls.x2);
|
| 74 | this.yCurve = curveFromControls(controls.y1, controls.y2);
|
| 75 | }
|
| 76 |
|
| 77 | private static EasingType.CubicBezier.CubicCurve curveFromControls(final float v1, final float v2) {
|
| 78 | return new EasingType.CubicBezier.CubicCurve(3.0F * v1 - 3.0F * v2 + 1.0F, -6.0F * v1 + 3.0F * v2, 3.0F * v1);
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public float apply(final float x) {
|
| 83 | return this.yCurve.sample(this.solveT(x));
|
| 84 | }
|
| 85 |
|
| 86 | private float solveT(final float x) {
|
| 87 | float t = x;
|
| 88 |
|
| 89 | for (int i = 0; i < 4; i++) {
|
| 90 | float error = this.xCurve.sample(t) - x;
|
| 91 | if (Math.abs(error) < 1.0E-5F) {
|
| 92 | return t;
|
| 93 | }
|
| 94 |
|
| 95 | float gradient = this.xCurve.sampleGradient(t);
|
| 96 | if (gradient < 1.0E-5F) {
|
| 97 | break;
|
| 98 | }
|
| 99 |
|
| 100 | t -= Mth.clamp(error / gradient, -0.25F, 0.25F);
|
| 101 | }
|
| 102 |
|
| 103 | return this.solveTBisect(x, t);
|
| 104 | }
|
| 105 |
|
| 106 | private float solveTBisect(final float x, final float initialT) {
|
| 107 | float t0 = 0.0F;
|
| 108 | float t1 = 1.0F;
|
| 109 |
|
| 110 | float t;
|
| 111 | for (t = initialT; t0 < t1; t = (t1 + t0) / 2.0F) {
|
| 112 | float error = this.xCurve.sample(t) - x;
|
| 113 | if (Math.abs(error) < 1.0E-5F) {
|
| 114 | return t;
|
| 115 | }
|
| 116 |
|
| 117 | if (error < 0.0F) {
|
| 118 | t0 = t;
|
| 119 | } else {
|
| 120 | t1 = t;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | return t;
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | public boolean equals(final Object obj) {
|
| 129 | return obj instanceof EasingType.CubicBezier bezier && this.controls.equals(bezier.controls);
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public int hashCode() {
|
| 134 | return this.controls.hashCode();
|
| 135 | }
|
| 136 |
|
| 137 | @Override
|
| 138 | public String toString() {
|
| 139 | return "CubicBezier(" + this.controls.x1 + ", " + this.controls.y1 + ", " + this.controls.x2 + ", " + this.controls.y2 + ")";
|
| 140 | }
|
| 141 |
|
| 142 | private record CubicCurve(float a, float b, float c) {
|
| 143 | public float sample(final float t) {
|
| 144 | return ((this.a * t + this.b) * t + this.c) * t;
|
| 145 | }
|
| 146 |
|
| 147 | public float sampleGradient(final float t) {
|
| 148 | return (3.0F * this.a * t + 2.0F * this.b) * t + this.c;
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | record CubicBezierControls(float x1, float y1, float x2, float y2) {
|
| 154 | public static final Codec<EasingType.CubicBezierControls> CODEC = Codec.FLOAT
|
| 155 | .listOf(4, 4)
|
| 156 | .xmap(
|
| 157 | floats -> new EasingType.CubicBezierControls(floats.get(0), floats.get(1), floats.get(2), floats.get(3)),
|
| 158 | controls -> List.of(controls.x1, controls.y1, controls.x2, controls.y2)
|
| 159 | )
|
| 160 | .validate(EasingType.CubicBezierControls::validate);
|
| 161 |
|
| 162 | private DataResult<EasingType.CubicBezierControls> validate() {
|
| 163 | if (this.x1 < 0.0F || this.x1 > 1.0F) {
|
| 164 | return DataResult.error(() -> "x1 must be in range [0; 1]");
|
| 165 | } else {
|
| 166 | return !(this.x2 < 0.0F) && !(this.x2 > 1.0F) ? DataResult.success(this) : DataResult.error(() -> "x2 must be in range [0; 1]");
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 |
|