| 1 | package net.minecraft.client.animation;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.model.geom.ModelPart;
|
| 6 | import net.minecraft.util.Mth;
|
| 7 | import org.joml.Vector3f;
|
| 8 | import org.joml.Vector3fc;
|
| 9 |
|
| 10 | @Environment(EnvType.CLIENT)
|
| 11 | public record AnimationChannel(AnimationChannel.Target target, Keyframe... keyframes) {
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public interface Interpolation {
|
| 14 | Vector3f apply(final Vector3f vector, final float alpha, final Keyframe[] keyframes, final int prev, final int next, final float targetScale);
|
| 15 | }
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public static class Interpolations {
|
| 19 | public static final AnimationChannel.Interpolation LINEAR = (vector, alpha, keyframes, prev, next, targetScale) -> {
|
| 20 | Vector3fc point0 = keyframes[prev].postTarget();
|
| 21 | Vector3fc point1 = keyframes[next].preTarget();
|
| 22 | return point0.lerp(point1, alpha, vector).mul(targetScale);
|
| 23 | };
|
| 24 | public static final AnimationChannel.Interpolation CATMULLROM = (vector, alpha, keyframes, prev, next, targetScale) -> {
|
| 25 | Vector3fc point0 = keyframes[Math.max(0, prev - 1)].postTarget();
|
| 26 | Vector3fc point1 = keyframes[prev].postTarget();
|
| 27 | Vector3fc point2 = keyframes[next].postTarget();
|
| 28 | Vector3fc point3 = keyframes[Math.min(keyframes.length - 1, next + 1)].postTarget();
|
| 29 | vector.set(
|
| 30 | Mth.catmullrom(alpha, point0.x(), point1.x(), point2.x(), point3.x()) * targetScale,
|
| 31 | Mth.catmullrom(alpha, point0.y(), point1.y(), point2.y(), point3.y()) * targetScale,
|
| 32 | Mth.catmullrom(alpha, point0.z(), point1.z(), point2.z(), point3.z()) * targetScale
|
| 33 | );
|
| 34 | return vector;
|
| 35 | };
|
| 36 | }
|
| 37 |
|
| 38 | @Environment(EnvType.CLIENT)
|
| 39 | public interface Target {
|
| 40 | void apply(final ModelPart animationBone, final Vector3f target);
|
| 41 | }
|
| 42 |
|
| 43 | @Environment(EnvType.CLIENT)
|
| 44 | public static class Targets {
|
| 45 | public static final AnimationChannel.Target POSITION = ModelPart::offsetPos;
|
| 46 | public static final AnimationChannel.Target ROTATION = ModelPart::offsetRotation;
|
| 47 | public static final AnimationChannel.Target SCALE = ModelPart::offsetScale;
|
| 48 | }
|
| 49 | }
|
| 50 |
|