| 1 | package net.minecraft.client.animation;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import java.util.Map;
|
| 6 | import java.util.function.Function;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.model.geom.ModelPart;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.world.entity.AnimationState;
|
| 12 | import org.joml.Vector3f;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class KeyframeAnimation {
|
| 16 | private final AnimationDefinition definition;
|
| 17 | private final List<KeyframeAnimation.Entry> entries;
|
| 18 |
|
| 19 | private KeyframeAnimation(final AnimationDefinition definition, final List<KeyframeAnimation.Entry> entries) {
|
| 20 | this.definition = definition;
|
| 21 | this.entries = entries;
|
| 22 | }
|
| 23 |
|
| 24 | static KeyframeAnimation bake(final ModelPart root, final AnimationDefinition definition) {
|
| 25 | List<KeyframeAnimation.Entry> entries = new ArrayList<>();
|
| 26 | Function<String, ModelPart> partLookup = root.createPartLookup();
|
| 27 |
|
| 28 | for (Map.Entry<String, List<AnimationChannel>> entry : definition.boneAnimations().entrySet()) {
|
| 29 | String partName = entry.getKey();
|
| 30 | List<AnimationChannel> channels = entry.getValue();
|
| 31 | ModelPart part = partLookup.apply(partName);
|
| 32 | if (part == null) {
|
| 33 | throw new IllegalArgumentException("Cannot animate " + partName + ", which does not exist in model");
|
| 34 | }
|
| 35 |
|
| 36 | for (AnimationChannel channel : channels) {
|
| 37 | entries.add(new KeyframeAnimation.Entry(part, channel.target(), channel.keyframes()));
|
| 38 | }
|
| 39 | }
|
| 40 |
|
| 41 | return new KeyframeAnimation(definition, List.copyOf(entries));
|
| 42 | }
|
| 43 |
|
| 44 | public void applyStatic() {
|
| 45 | this.apply(0L, 1.0F);
|
| 46 | }
|
| 47 |
|
| 48 | public void applyWalk(final float animationPos, final float animationSpeed, final float speedFactor, final float scaleFactor) {
|
| 49 | long time = (long)(animationPos * 50.0F * speedFactor);
|
| 50 | float scale = Math.min(animationSpeed * scaleFactor, 1.0F);
|
| 51 | this.apply(time, scale);
|
| 52 | }
|
| 53 |
|
| 54 | public void apply(final AnimationState animationState, final float currentTime) {
|
| 55 | this.apply(animationState, currentTime, 1.0F);
|
| 56 | }
|
| 57 |
|
| 58 | public void apply(final AnimationState animationState, final float currentTime, final float speedFactor) {
|
| 59 | animationState.ifStarted(state -> this.apply((long)((float)state.getTimeInMillis(currentTime) * speedFactor), 1.0F));
|
| 60 | }
|
| 61 |
|
| 62 | public void apply(final long millisSinceStart, final float targetScale) {
|
| 63 | float secondsSinceStart = this.getElapsedSeconds(millisSinceStart);
|
| 64 | Vector3f scratchVector = new Vector3f();
|
| 65 |
|
| 66 | for (KeyframeAnimation.Entry entry : this.entries) {
|
| 67 | entry.apply(secondsSinceStart, targetScale, scratchVector);
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | private float getElapsedSeconds(final long millisSinceStart) {
|
| 72 | float secondsSinceStart = (float)millisSinceStart / 1000.0F;
|
| 73 | return this.definition.looping() ? secondsSinceStart % this.definition.lengthInSeconds() : secondsSinceStart;
|
| 74 | }
|
| 75 |
|
| 76 | @Environment(EnvType.CLIENT)
|
| 77 | private record Entry(ModelPart part, AnimationChannel.Target target, Keyframe[] keyframes) {
|
| 78 | public void apply(final float secondsSinceStart, final float targetScale, final Vector3f scratchVector) {
|
| 79 | int prev = Math.max(0, Mth.binarySearch(0, this.keyframes.length, i -> secondsSinceStart <= this.keyframes[i].timestamp()) - 1);
|
| 80 | int next = Math.min(this.keyframes.length - 1, prev + 1);
|
| 81 | Keyframe previousFrame = this.keyframes[prev];
|
| 82 | Keyframe nextFrame = this.keyframes[next];
|
| 83 | float keyframeTimeDelta = secondsSinceStart - previousFrame.timestamp();
|
| 84 | float lerpAlpha;
|
| 85 | if (next != prev) {
|
| 86 | lerpAlpha = Mth.clamp(keyframeTimeDelta / (nextFrame.timestamp() - previousFrame.timestamp()), 0.0F, 1.0F);
|
| 87 | } else {
|
| 88 | lerpAlpha = 0.0F;
|
| 89 | }
|
| 90 |
|
| 91 | nextFrame.interpolation().apply(scratchVector, lerpAlpha, this.keyframes, prev, next, targetScale);
|
| 92 | this.target.apply(this.part, scratchVector);
|
| 93 | }
|
| 94 | }
|
| 95 | }
|
| 96 |
|