Rework interpolation & add Hermite interpolation

This commit is contained in:
Moulberry committed 2024-12-29 23:58:02 -05:00
1 parent ea05e90a20
commit ae53512c3d
38 files changed
+653 -364

No files matched your search

+2
View File
@@ -113,6 +113,8 @@ dependencies {
shadow(api(rootProject.files('deps/imgui-java-natives-macos-1.86.11-10-g0dbf36c.jar')))
shadow(api(rootProject.files('deps/imgui-java-natives-windows-1.86.11-10-g0dbf36c.jar')))
shadow(implementation("org.apache.commons:commons-math3:3.6.1"))
// Native File Dialogs
String nfdVersion = "org.lwjgl:lwjgl-nfd:3.3.3";
@@ -1,10 +1,15 @@
package com.moulberry.flashback;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import net.minecraft.util.Mth;
import org.apache.commons.math3.analysis.interpolation.HermiteInterpolator;
import org.joml.Math;
import org.joml.Quaterniond;
import org.joml.Vector3d;
import java.util.Map;
public class Interpolation {
public static double linear(double from, double to, double amount) {
@@ -974,8 +974,8 @@ public class TimelineWindow {
if (leftClicked) {
if (ImGui.isMouseDoubleClicked(ImGuiMouseButton.Left)) {
MinecraftKeyframeHandler minecraftKeyframeHandler = new MinecraftKeyframeHandler(Minecraft.getInstance());
if (minecraftKeyframeHandler.supportedKeyframes().contains(closest.getValue().keyframeType())) {
closest.getValue().apply(minecraftKeyframeHandler);
if (closest.getValue().keyframeType().supportsHandler(minecraftKeyframeHandler)) {
closest.getValue().createChange().apply(minecraftKeyframeHandler);
}
} else {
grabbedKeyframe = true;
@@ -1141,7 +1141,7 @@ public class TimelineWindow {
if (!multiple && editingKeyframe instanceof CameraKeyframe cameraKeyframe) {
ImGui.sameLine();
if (ImGui.button("Apply")) {
cameraKeyframe.apply(new MinecraftKeyframeHandler(Minecraft.getInstance()));
cameraKeyframe.createChange().apply(new MinecraftKeyframeHandler(Minecraft.getInstance()));
}
}
}
@@ -1566,6 +1566,10 @@ public class TimelineWindow {
case HOLD -> {
drawList.addRectFilled(x - keyframeSize, y - keyframeSize, x + keyframeSize, y + keyframeSize, colour);
}
case HERMITE -> {
drawList.addTriangleFilled(x, y - keyframeSize, x + keyframeSize, y + keyframeSize,
x - keyframeSize, y + keyframeSize, colour);
}
}
}
@@ -2,6 +2,7 @@ package com.moulberry.flashback.keyframe;
import com.google.gson.*;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import com.moulberry.flashback.keyframe.impl.CameraOrbitKeyframe;
@@ -14,6 +15,7 @@ import com.moulberry.flashback.keyframe.impl.TimelapseKeyframe;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
@@ -31,10 +33,9 @@ public abstract class Keyframe {
public abstract KeyframeType<?> keyframeType();
public abstract Keyframe copy();
public abstract void apply(KeyframeHandler t);
public abstract void applyInterpolated(KeyframeHandler t, Keyframe other, float amount);
public abstract void applyInterpolatedSmooth(KeyframeHandler t, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount,
float lerpAmount, boolean lerpFromRight);
public abstract KeyframeChange createChange();
public abstract KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount);
public abstract KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount);
public void renderEditKeyframe(Consumer<Consumer<Keyframe>> update) {}
@@ -1,5 +1,7 @@
package com.moulberry.flashback.keyframe;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import org.jetbrains.annotations.Nullable;
public interface KeyframeType<T extends Keyframe> {
@@ -12,6 +14,11 @@ public interface KeyframeType<T extends Keyframe> {
@Nullable T createDirect();
@Nullable KeyframeCreatePopup<T> createPopup();
Class<? extends KeyframeChange> keyframeChangeType();
default boolean supportsHandler(KeyframeHandler handler) {
return handler.supportsKeyframeChange(this.keyframeChangeType());
}
default boolean allowChangingInterpolationType() {
return true;
}
@@ -0,0 +1,10 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public interface KeyframeChange {
void apply(KeyframeHandler keyframeHandler);
KeyframeChange interpolate(KeyframeChange to, double amount);
}
@@ -0,0 +1,23 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import org.joml.Vector3d;
public record KeyframeChangeCameraPosition(Vector3d position, double yaw, double pitch, double roll) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyCameraPosition(this.position, this.yaw, this.pitch, this.roll);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
KeyframeChangeCameraPosition other = (KeyframeChangeCameraPosition) to;
return new KeyframeChangeCameraPosition(
this.position.lerp(other.position, amount, new Vector3d()),
Interpolation.linear(this.yaw, other.yaw, amount),
Interpolation.linear(this.pitch, other.pitch, amount),
Interpolation.linear(this.roll, other.roll, amount)
);
}
}
@@ -0,0 +1,22 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public record KeyframeChangeCameraShake(float frequencyX, float amplitudeX, float frequencyY, float amplitudeY) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyCameraShake(this.frequencyX, this.amplitudeX, this.frequencyY, this.amplitudeY);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
KeyframeChangeCameraShake other = (KeyframeChangeCameraShake) to;
return new KeyframeChangeCameraShake(
(float) Interpolation.linear(this.frequencyX, other.frequencyX, amount),
(float) Interpolation.linear(this.amplitudeX, other.amplitudeX, amount),
(float) Interpolation.linear(this.frequencyY, other.frequencyY, amount),
(float) Interpolation.linear(this.amplitudeY, other.amplitudeY, amount)
);
}
}
@@ -0,0 +1,23 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.Utils;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public record KeyframeChangeFov(float fov) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyFov(this.fov);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
KeyframeChangeFov other = (KeyframeChangeFov) to;
float thisFocalLength = Utils.fovToFocalLength(this.fov);
float otherFocalLength = Utils.fovToFocalLength(other.fov);
float focalLength = (float) Interpolation.linear(thisFocalLength, otherFocalLength, amount);
return new KeyframeChangeFov(Utils.focalLengthToFov(focalLength));
}
}
@@ -0,0 +1,16 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public record KeyframeChangeFreeze(boolean frozen, int frozenDelay) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyFreeze(this.frozen, this.frozenDelay);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
return this;
}
}
@@ -0,0 +1,20 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public record KeyframeChangeTickrate(float tickrate) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyTickrate(this.tickrate);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
KeyframeChangeTickrate other = (KeyframeChangeTickrate) to;
return new KeyframeChangeTickrate(
(float) Interpolation.linear(this.tickrate, other.tickrate, amount)
);
}
}
@@ -0,0 +1,20 @@
package com.moulberry.flashback.keyframe.change;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
public record KeyframeChangeTimeOfDay(int timeOfDay) implements KeyframeChange {
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyTimeOfDay(this.timeOfDay);
}
@Override
public KeyframeChange interpolate(KeyframeChange to, double amount) {
KeyframeChangeTimeOfDay other = (KeyframeChangeTimeOfDay) to;
return new KeyframeChangeTimeOfDay(
(int) Interpolation.linear(this.timeOfDay, other.timeOfDay, amount)
);
}
}
@@ -1,6 +1,7 @@
package com.moulberry.flashback.keyframe.handler;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import org.joml.Vector3d;
import org.joml.Vector3f;
@@ -8,35 +9,28 @@ import java.util.EnumSet;
import java.util.Set;
public interface KeyframeHandler {
Set<KeyframeType<?>> supportedKeyframes();
boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz);
default boolean alwaysApplyLastKeyframe() {
return false;
}
default void applyCameraPosition(Vector3d position, double yaw, double pitch, double roll) {
throw new UnsupportedOperationException();
}
default void applyFov(float fov) {
throw new UnsupportedOperationException();
}
default void applyTickrate(float tickrate) {
throw new UnsupportedOperationException();
}
default void applyFreeze(boolean frozen, int frozenDelay) {
throw new UnsupportedOperationException();
}
default void applyTimeOfDay(int timeOfDay) {
throw new UnsupportedOperationException();
}
default void applyCameraShake(float frequencyX, float amplitudeX, float frequencyY, float amplitudeY) {
throw new UnsupportedOperationException();
}
@@ -1,37 +1,24 @@
package com.moulberry.flashback.keyframe.handler;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.types.CameraKeyframeType;
import com.moulberry.flashback.keyframe.types.CameraOrbitKeyframeType;
import com.moulberry.flashback.keyframe.types.CameraShakeKeyframeType;
import com.moulberry.flashback.keyframe.types.FOVKeyframeType;
import com.moulberry.flashback.keyframe.types.FreezeKeyframeType;
import com.moulberry.flashback.keyframe.types.TimeOfDayKeyframeType;
import com.moulberry.flashback.keyframe.change.*;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.util.Mth;
import net.minecraft.world.phys.Vec3;
import org.joml.Vector3d;
import org.joml.Vector3f;
import java.util.EnumSet;
import java.util.Set;
public class MinecraftKeyframeHandler implements KeyframeHandler {
public record MinecraftKeyframeHandler(Minecraft minecraft) implements KeyframeHandler {
private final Minecraft minecraft;
public MinecraftKeyframeHandler(Minecraft minecraft) {
this.minecraft = minecraft;
}
private static final Set<Class<? extends KeyframeChange>> supportedChanges = Set.of(
KeyframeChangeCameraPosition.class, KeyframeChangeFov.class, KeyframeChangeTimeOfDay.class, KeyframeChangeCameraShake.class
);
@Override
public Set<KeyframeType<?>> supportedKeyframes() {
return Set.of(CameraKeyframeType.INSTANCE, CameraOrbitKeyframeType.INSTANCE, FOVKeyframeType.INSTANCE,
TimeOfDayKeyframeType.INSTANCE, CameraShakeKeyframeType.INSTANCE);
public boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz) {
return supportedChanges.contains(clazz);
}
@Override
@@ -63,11 +50,6 @@ public class MinecraftKeyframeHandler implements KeyframeHandler {
}
}
@Override
public void applyTickrate(float tickrate) {
throw new UnsupportedOperationException();
}
@Override
public void applyTimeOfDay(int timeOfDay) {
timeOfDay = timeOfDay % 24000;
@@ -1,6 +1,9 @@
package com.moulberry.flashback.keyframe.handler;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFreeze;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.types.FreezeKeyframeType;
import com.moulberry.flashback.keyframe.types.SpeedKeyframeType;
import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
@@ -9,12 +12,15 @@ import com.moulberry.flashback.playback.ReplayServer;
import java.util.EnumSet;
import java.util.Set;
public class ReplayServerKeyframeHandler implements KeyframeHandler {
public record ReplayServerKeyframeHandler(ReplayServer replayServer) implements KeyframeHandler {
private final ReplayServer replayServer;
private static final Set<Class<? extends KeyframeChange>> supportedChanges = Set.of(
KeyframeChangeTickrate.class, KeyframeChangeFreeze.class
);
public ReplayServerKeyframeHandler(ReplayServer replayServer) {
this.replayServer = replayServer;
@Override
public boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz) {
return supportedChanges.contains(clazz);
}
@Override
@@ -22,11 +28,6 @@ public class ReplayServerKeyframeHandler implements KeyframeHandler {
return true;
}
@Override
public Set<KeyframeType<?>> supportedKeyframes() {
return Set.of(SpeedKeyframeType.INSTANCE, TimelapseKeyframeType.INSTANCE, FreezeKeyframeType.INSTANCE);
}
@Override
public void applyFov(float fov) {
throw new UnsupportedOperationException();
@@ -1,6 +1,9 @@
package com.moulberry.flashback.keyframe.handler;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFreeze;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.types.FreezeKeyframeType;
import com.moulberry.flashback.keyframe.types.SpeedKeyframeType;
import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
@@ -8,13 +11,17 @@ import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
import java.util.Set;
public class TickrateKeyframeCapture implements KeyframeHandler {
private static final Set<Class<? extends KeyframeChange>> supportedChanges = Set.of(
KeyframeChangeTickrate.class, KeyframeChangeFreeze.class
);
public float tickrate = 20.0f;
public boolean frozen = false;
public int frozenDelay = 0;
@Override
public Set<KeyframeType<?>> supportedKeyframes() {
return Set.of(SpeedKeyframeType.INSTANCE, TimelapseKeyframeType.INSTANCE, FreezeKeyframeType.INSTANCE);
public boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz) {
return supportedChanges.contains(clazz);
}
@Override
@@ -1,21 +1,27 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.*;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.CameraKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
import org.apache.commons.math3.analysis.interpolation.HermiteInterpolator;
import org.joml.Vector3d;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class CameraKeyframe extends Keyframe {
@@ -107,84 +113,43 @@ public class CameraKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyCameraPosition(this.position, this.yaw, this.pitch, this.roll);
public KeyframeChange createChange() {
return new KeyframeChangeCameraPosition(this.position, this.yaw, this.pitch, this.roll);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe genericOther, float amount) {
if (!(genericOther instanceof CameraKeyframe other)) {
this.apply(keyframeHandler);
return;
}
double x = Interpolation.linear(this.position.x, other.position.x, amount);
double y = Interpolation.linear(this.position.y, other.position.y, amount);
double z = Interpolation.linear(this.position.z, other.position.z, amount);
Vector3d position = new Vector3d(x, y, z);
float yaw = Interpolation.linearAngle(this.yaw, other.yaw, amount);
float pitch = Interpolation.linearAngle(this.pitch, other.pitch, amount);
float roll = Interpolation.linearAngle(this.roll, other.roll, amount);
keyframeHandler.applyCameraPosition(position, yaw, pitch, roll);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
// Calculate position
Vector3d position = CatmullRom.position(this.position,
((CameraKeyframe)p1).position, ((CameraKeyframe)p2).position,
((CameraKeyframe)p3).position, time1, time2, time3, amount);
double x = position.x;
double y = position.y;
double z = position.z;
((CameraKeyframe)p1).position, ((CameraKeyframe)p2).position,
((CameraKeyframe)p3).position, time1, time2, time3, amount);
// Calculate rotation
float yaw = CatmullRom.degrees(this.yaw,
((CameraKeyframe)p1).yaw, ((CameraKeyframe)p2).yaw,
((CameraKeyframe)p3).yaw, time1, time2, time3, amount);
((CameraKeyframe)p1).yaw, ((CameraKeyframe)p2).yaw,
((CameraKeyframe)p3).yaw, time1, time2, time3, amount);
float pitch = CatmullRom.degrees(this.pitch,
((CameraKeyframe)p1).pitch, ((CameraKeyframe)p2).pitch,
((CameraKeyframe)p3).pitch, time1, time2, time3, amount);
((CameraKeyframe)p1).pitch, ((CameraKeyframe)p2).pitch,
((CameraKeyframe)p3).pitch, time1, time2, time3, amount);
float roll = CatmullRom.degrees(this.roll,
((CameraKeyframe)p1).roll, ((CameraKeyframe)p2).roll,
((CameraKeyframe)p3).roll, time1, time2, time3, amount);
((CameraKeyframe)p1).roll, ((CameraKeyframe)p2).roll,
((CameraKeyframe)p3).roll, time1, time2, time3, amount);
if (lerpAmount >= 0) {
double linearX = Interpolation.linear(((CameraKeyframe)p1).position.x, ((CameraKeyframe)p2).position.x, lerpAmount);
double linearY = Interpolation.linear(((CameraKeyframe)p1).position.y, ((CameraKeyframe)p2).position.y, lerpAmount);
double linearZ = Interpolation.linear(((CameraKeyframe)p1).position.z, ((CameraKeyframe)p2).position.z, lerpAmount);
float linearYaw = Interpolation.linearAngle(((CameraKeyframe)p1).yaw, ((CameraKeyframe)p2).yaw, lerpAmount);
float linearPitch = Interpolation.linearAngle(((CameraKeyframe)p1).pitch, ((CameraKeyframe)p2).pitch, lerpAmount);
float linearRoll = Interpolation.linearAngle(((CameraKeyframe)p1).roll, ((CameraKeyframe)p2).roll, lerpAmount);
return new KeyframeChangeCameraPosition(position, yaw, pitch, roll);
}
if (lerpFromRight) {
x = Interpolation.linear(x, linearX, amount);
y = Interpolation.linear(y, linearY, amount);
z = Interpolation.linear(z, linearZ, amount);
yaw = Interpolation.linearAngle(yaw, linearYaw, amount);
pitch = Interpolation.linearAngle(pitch, linearPitch, amount);
roll = Interpolation.linearAngle(roll, linearRoll, amount);
} else {
x = Interpolation.linear(linearX, x, amount);
y = Interpolation.linear(linearY, y, amount);
z = Interpolation.linear(linearZ, z, amount);
yaw = Interpolation.linearAngle(linearYaw, yaw, amount);
pitch = Interpolation.linearAngle(linearPitch, pitch, amount);
roll = Interpolation.linearAngle(linearRoll, roll, amount);
}
}
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
Vector3d position = Hermite.position(Maps.transformValues(keyframes, k -> ((CameraKeyframe)k).position), amount);
double yaw = Hermite.degrees(Maps.transformValues(keyframes, k -> (double) ((CameraKeyframe)k).yaw), amount);
double pitch = Hermite.degrees(Maps.transformValues(keyframes, k -> (double) ((CameraKeyframe)k).pitch), amount);
double roll = Hermite.degrees(Maps.transformValues(keyframes, k -> (double) ((CameraKeyframe)k).roll), amount);
position.x = x;
position.y = y;
position.z = z;
keyframeHandler.applyCameraPosition(position, yaw, pitch, roll);
return new KeyframeChangeCameraPosition(position, yaw, pitch, roll);
}
public static class TypeAdapter implements JsonSerializer<CameraKeyframe>, JsonDeserializer<CameraKeyframe> {
@@ -1,5 +1,6 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@@ -11,10 +12,13 @@ import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.CameraOrbitKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import imgui.ImGui;
@@ -28,6 +32,7 @@ import org.joml.Quaternionf;
import org.joml.Vector3d;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class CameraOrbitKeyframe extends Keyframe {
@@ -93,7 +98,7 @@ public class CameraOrbitKeyframe extends Keyframe {
}
}
private void apply(KeyframeHandler keyframeHandler, Vector3d center, float distance, float yaw, float pitch) {
private static KeyframeChangeCameraPosition createChangeFrom(Vector3d center, float distance, float yaw, float pitch) {
float pitchRadians = (float) Math.toRadians(pitch);
float yawRadians = (float) Math.toRadians(-yaw);
float cosYaw = Mth.cos(yawRadians);
@@ -107,79 +112,46 @@ public class CameraOrbitKeyframe extends Keyframe {
if (player != null) {
cameraPosition.y -= player.getEyeHeight();
}
keyframeHandler.applyCameraPosition(cameraPosition, yaw, pitch, 0.0f);
return new KeyframeChangeCameraPosition(cameraPosition, yaw, pitch, 0.0f);
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
apply(keyframeHandler, this.center, this.distance, this.yaw, this.pitch);
public KeyframeChange createChange() {
return createChangeFrom(this.center, this.distance, this.yaw, this.pitch);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe genericOther, float amount) {
if (!(genericOther instanceof CameraOrbitKeyframe other)) {
this.apply(keyframeHandler);
return;
}
double x = Interpolation.linear(this.center.x, other.center.x, amount);
double y = Interpolation.linear(this.center.y, other.center.y, amount);
double z = Interpolation.linear(this.center.z, other.center.z, amount);
float distance = Interpolation.linear(this.distance, other.distance, amount);
float yaw = Interpolation.linear(this.yaw, other.yaw, amount);
float pitch = Interpolation.linear(this.pitch, other.pitch, amount);
Vector3d center = new Vector3d((float) x, (float) y, (float) z);
apply(keyframeHandler, center, distance, yaw, pitch);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
// Calculate position
Vector3d position = CatmullRom.position(this.center,
((CameraOrbitKeyframe)p1).center, ((CameraOrbitKeyframe)p2).center,
((CameraOrbitKeyframe)p3).center, time1, time2, time3, amount);
((CameraOrbitKeyframe)p1).center, ((CameraOrbitKeyframe)p2).center,
((CameraOrbitKeyframe)p3).center, time1, time2, time3, amount);
double x = position.x;
double y = position.y;
double z = position.z;
float distance = CatmullRom.value(this.distance, ((CameraOrbitKeyframe)p1).distance, ((CameraOrbitKeyframe)p2).distance,
((CameraOrbitKeyframe)p3).distance, time1, time2, time3, amount);
((CameraOrbitKeyframe)p3).distance, time1, time2, time3, amount);
// Note: we don't use CatmullRom#degrees because we want to allow multiple rotations in a single orbit
float yaw = CatmullRom.value(this.yaw, ((CameraOrbitKeyframe)p1).yaw, ((CameraOrbitKeyframe)p2).yaw,
((CameraOrbitKeyframe)p3).yaw, time1, time2, time3, amount);
((CameraOrbitKeyframe)p3).yaw, time1, time2, time3, amount);
float pitch = CatmullRom.value(this.pitch, ((CameraOrbitKeyframe)p1).pitch, ((CameraOrbitKeyframe)p2).pitch,
((CameraOrbitKeyframe)p3).pitch, time1, time2, time3, amount);
((CameraOrbitKeyframe)p3).pitch, time1, time2, time3, amount);
if (lerpAmount >= 0) {
double linearX = Interpolation.linear(((CameraOrbitKeyframe)p1).center.x, ((CameraOrbitKeyframe)p2).center.x, lerpAmount);
double linearY = Interpolation.linear(((CameraOrbitKeyframe)p1).center.y, ((CameraOrbitKeyframe)p2).center.y, lerpAmount);
double linearZ = Interpolation.linear(((CameraOrbitKeyframe)p1).center.z, ((CameraOrbitKeyframe)p2).center.z, lerpAmount);
float linearDistance = Interpolation.linear(((CameraOrbitKeyframe)p1).distance, ((CameraOrbitKeyframe)p2).distance, amount);
float linearYaw = Interpolation.linear(((CameraOrbitKeyframe)p1).yaw, ((CameraOrbitKeyframe)p2).yaw, amount);
float linearPitch = Interpolation.linear(((CameraOrbitKeyframe)p1).pitch, ((CameraOrbitKeyframe)p2).pitch, amount);
return createChangeFrom(position, distance, yaw, pitch);
}
if (lerpFromRight) {
x = Interpolation.linear(x, linearX, amount);
y = Interpolation.linear(y, linearY, amount);
z = Interpolation.linear(z, linearZ, amount);
distance = Interpolation.linear(distance, linearDistance, amount);
yaw = Interpolation.linear(yaw, linearYaw, amount);
pitch = Interpolation.linear(pitch, linearPitch, amount);
} else {
x = Interpolation.linear(linearX, x, amount);
y = Interpolation.linear(linearY, y, amount);
z = Interpolation.linear(linearZ, z, amount);
distance = Interpolation.linear(linearDistance, distance, amount);
yaw = Interpolation.linear(linearYaw, yaw, amount);
pitch = Interpolation.linear(linearPitch, pitch, amount);
}
}
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
Vector3d position = Hermite.position(Maps.transformValues(keyframes, k -> ((CameraOrbitKeyframe)k).center), amount);
double distance = Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraOrbitKeyframe)k).distance), amount);
apply(keyframeHandler, new Vector3d(x, y, z), distance, yaw, pitch);
// Note: we don't use Hermite#degrees because we want to allow multiple rotations in a single orbit
double yaw = Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraOrbitKeyframe)k).yaw), amount);
double pitch = Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraOrbitKeyframe)k).pitch), amount);
return createChangeFrom(position, (float) distance, (float) yaw, (float) pitch);
}
public static class TypeAdapter implements JsonSerializer<CameraOrbitKeyframe>, JsonDeserializer<CameraOrbitKeyframe> {
@@ -1,5 +1,6 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@@ -10,15 +11,20 @@ import com.google.gson.JsonSerializer;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraShake;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFov;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.CameraShakeKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import com.moulberry.flashback.state.EditorState;
import com.moulberry.flashback.state.EditorStateManager;
import imgui.ImGui;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class CameraShakeKeyframe extends Keyframe {
@@ -123,60 +129,36 @@ public class CameraShakeKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyCameraShake(this.frequencyX, this.amplitudeX, this.frequencyY, this.amplitudeY);
public KeyframeChange createChange() {
return new KeyframeChangeCameraShake(this.frequencyX, this.amplitudeX, this.frequencyY, this.amplitudeY);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
if (!(otherGeneric instanceof CameraShakeKeyframe other)) {
this.apply(keyframeHandler);
return;
}
float frequencyX = Interpolation.linear(this.frequencyX, other.frequencyX, amount);
float amplitudeX = Interpolation.linear(this.amplitudeX, other.amplitudeX, amount);
float frequencyY = Interpolation.linear(this.frequencyY, other.frequencyY, amount);
float amplitudeY = Interpolation.linear(this.amplitudeY, other.amplitudeY, amount);
keyframeHandler.applyCameraShake(frequencyX, amplitudeX, frequencyY, amplitudeY);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
float frequencyX = CatmullRom.value(this.frequencyX, ((CameraShakeKeyframe)p1).frequencyX,
((CameraShakeKeyframe)p2).frequencyX, ((CameraShakeKeyframe)p3).frequencyX, time1, time2, time3, amount);
((CameraShakeKeyframe)p2).frequencyX, ((CameraShakeKeyframe)p3).frequencyX, time1, time2, time3, amount);
float amplitudeX = CatmullRom.value(this.amplitudeX, ((CameraShakeKeyframe)p1).amplitudeX,
((CameraShakeKeyframe)p2).amplitudeX, ((CameraShakeKeyframe)p3).amplitudeX, time1, time2, time3, amount);
((CameraShakeKeyframe)p2).amplitudeX, ((CameraShakeKeyframe)p3).amplitudeX, time1, time2, time3, amount);
float frequencyY = CatmullRom.value(this.frequencyY, ((CameraShakeKeyframe)p1).frequencyY,
((CameraShakeKeyframe)p2).frequencyY, ((CameraShakeKeyframe)p3).frequencyY, time1, time2, time3, amount);
((CameraShakeKeyframe)p2).frequencyY, ((CameraShakeKeyframe)p3).frequencyY, time1, time2, time3, amount);
float amplitudeY = CatmullRom.value(this.amplitudeY, ((CameraShakeKeyframe)p1).amplitudeY,
((CameraShakeKeyframe)p2).amplitudeY, ((CameraShakeKeyframe)p3).amplitudeY, time1, time2, time3, amount);
((CameraShakeKeyframe)p2).amplitudeY, ((CameraShakeKeyframe)p3).amplitudeY, time1, time2, time3, amount);
if (lerpAmount >= 0) {
float linearFrequencyX = Interpolation.linear(((CameraShakeKeyframe)p1).frequencyX, ((CameraShakeKeyframe)p2).frequencyX, lerpAmount);
float linearAmplitudeX = Interpolation.linear(((CameraShakeKeyframe)p1).amplitudeX, ((CameraShakeKeyframe)p2).amplitudeX, lerpAmount);
float linearFrequencyY = Interpolation.linear(((CameraShakeKeyframe)p1).frequencyY, ((CameraShakeKeyframe)p2).frequencyY, lerpAmount);
float linearAmplitudeY = Interpolation.linear(((CameraShakeKeyframe)p1).amplitudeY, ((CameraShakeKeyframe)p2).amplitudeY, lerpAmount);
return new KeyframeChangeCameraShake(frequencyX, amplitudeX, frequencyY, amplitudeY);
}
if (lerpFromRight) {
frequencyX = Interpolation.linear(frequencyX, linearFrequencyX, amount);
amplitudeX = Interpolation.linear(amplitudeX, linearAmplitudeX, amount);
frequencyY = Interpolation.linear(frequencyY, linearFrequencyY, amount);
amplitudeY = Interpolation.linear(amplitudeY, linearAmplitudeY, amount);
} else {
frequencyX = Interpolation.linear(linearFrequencyX, frequencyX, amount);
amplitudeX = Interpolation.linear(linearAmplitudeX, amplitudeX, amount);
frequencyY = Interpolation.linear(linearFrequencyY, frequencyY, amount);
amplitudeY = Interpolation.linear(linearAmplitudeY, amplitudeY, amount);
}
}
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
float frequencyX = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraShakeKeyframe)k).frequencyX), amount);
float amplitudeX = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraShakeKeyframe)k).amplitudeX), amount);
float frequencyY = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraShakeKeyframe)k).frequencyY), amount);
float amplitudeY = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((CameraShakeKeyframe)k).amplitudeY), amount);
keyframeHandler.applyCameraShake(frequencyX, amplitudeX, frequencyY, amplitudeY);
return new KeyframeChangeCameraShake(frequencyX, amplitudeX, frequencyY, amplitudeY);
}
public static class TypeAdapter implements JsonSerializer<CameraShakeKeyframe>, JsonDeserializer<CameraShakeKeyframe> {
@@ -1,17 +1,22 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.*;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.Utils;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFov;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.FOVKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import imgui.ImGui;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class FOVKeyframe extends Keyframe {
@@ -49,26 +54,12 @@ public class FOVKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyFov(this.fov);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
if (!(otherGeneric instanceof FOVKeyframe other)) {
this.apply(keyframeHandler);
return;
}
float thisFocalLength = Utils.fovToFocalLength(this.fov);
float otherFocalLength = Utils.fovToFocalLength(other.fov);
float focalLength = Interpolation.linear(thisFocalLength, otherFocalLength, amount);
keyframeHandler.applyFov(Utils.focalLengthToFov(focalLength));
public KeyframeChange createChange() {
return new KeyframeChangeFov(this.fov);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
@@ -80,17 +71,13 @@ public class FOVKeyframe extends Keyframe {
float focalLength = CatmullRom.value(f0, f1, f2, f3, time1, time2, time3, amount);
if (lerpAmount >= 0) {
float linearFocalLength = Interpolation.linear(f1, f2, lerpAmount);
return new KeyframeChangeFov(Utils.focalLengthToFov(focalLength));
}
if (lerpFromRight) {
focalLength = Interpolation.linear(focalLength, linearFocalLength, amount);
} else {
focalLength = Interpolation.linear(linearFocalLength, focalLength, amount);
}
}
keyframeHandler.applyFov(Utils.focalLengthToFov(focalLength));
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
float focalLength = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) Utils.fovToFocalLength(((FOVKeyframe)k).fov)), amount);
return new KeyframeChangeFov(Utils.focalLengthToFov(focalLength));
}
public static class TypeAdapter implements JsonSerializer<FOVKeyframe>, JsonDeserializer<FOVKeyframe> {
@@ -11,6 +11,8 @@ import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFreeze;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.SpeedKeyframeType;
@@ -18,6 +20,7 @@ import com.moulberry.flashback.spline.CatmullRom;
import imgui.ImGui;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class FreezeKeyframe extends Keyframe {
@@ -62,18 +65,18 @@ public class FreezeKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyFreeze(this.frozen, Math.max(0, Math.min(10, this.frozenDelay)));
public KeyframeChange createChange() {
return new KeyframeChangeFreeze(this.frozen, Math.max(0, Math.min(10, this.frozenDelay)));
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
this.apply(keyframeHandler);
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
return this.createChange();
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
p1.apply(keyframeHandler);
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
return this.createChange();
}
public static class TypeAdapter implements JsonSerializer<FreezeKeyframe>, JsonDeserializer<FreezeKeyframe> {
@@ -1,16 +1,21 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.*;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.SpeedKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import imgui.ImGui;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class TickrateKeyframe extends Keyframe {
@@ -49,42 +54,27 @@ public class TickrateKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyTickrate(this.tickrate);
public KeyframeChange createChange() {
return new KeyframeChangeTickrate(this.tickrate);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
if (!(otherGeneric instanceof TickrateKeyframe other)) {
this.apply(keyframeHandler);
return;
}
float tickrate = Interpolation.linear(this.tickrate, other.tickrate, amount);
keyframeHandler.applyTickrate(tickrate);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
float tickrate = CatmullRom.value(this.tickrate,
((TickrateKeyframe)p1).tickrate, ((TickrateKeyframe)p2).tickrate,
((TickrateKeyframe)p3).tickrate, time1, time2, time3, amount);
((TickrateKeyframe)p1).tickrate, ((TickrateKeyframe)p2).tickrate,
((TickrateKeyframe)p3).tickrate, time1, time2, time3, amount);
if (lerpAmount >= 0) {
float linearTickrate = Interpolation.linear(((TickrateKeyframe)p1).tickrate, ((TickrateKeyframe)p2).tickrate, lerpAmount);
return new KeyframeChangeTickrate(tickrate);
}
if (lerpFromRight) {
tickrate = Interpolation.linear(tickrate, linearTickrate, amount);
} else {
tickrate = Interpolation.linear(linearTickrate, tickrate, amount);
}
}
keyframeHandler.applyTickrate(tickrate);
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
float tickrate = (float) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((TickrateKeyframe)k).tickrate), amount);
return new KeyframeChangeTickrate(tickrate);
}
public static class TypeAdapter implements JsonSerializer<TickrateKeyframe>, JsonDeserializer<TickrateKeyframe> {
@@ -1,18 +1,24 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.*;
import com.moulberry.flashback.Interpolation;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTimeOfDay;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.TimeOfDayKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import imgui.ImGui;
import imgui.type.ImInt;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class TimeOfDayKeyframe extends Keyframe {
@@ -50,44 +56,28 @@ public class TimeOfDayKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
keyframeHandler.applyTimeOfDay(this.time);
public KeyframeChange createChange() {
return new KeyframeChangeTimeOfDay(this.time);
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
if (!(otherGeneric instanceof TimeOfDayKeyframe other)) {
this.apply(keyframeHandler);
return;
}
int time = Math.round(Interpolation.linear(this.time, other.time, amount));
keyframeHandler.applyTimeOfDay(time);
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
float time1 = t1 - t0;
float time2 = t2 - t0;
float time3 = t3 - t0;
float timeOfDay = CatmullRom.value(this.time,
((TimeOfDayKeyframe)p1).time, ((TimeOfDayKeyframe)p2).time,
((TimeOfDayKeyframe)p3).time, time1, time2, time3, amount);
int timeOfDay = (int) CatmullRom.value(this.time,
((TimeOfDayKeyframe)p1).time, ((TimeOfDayKeyframe)p2).time,
((TimeOfDayKeyframe)p3).time, time1, time2, time3, amount);
if (lerpAmount >= 0) {
float linearTimeOfDay = Interpolation.linear(((TimeOfDayKeyframe)p1).time, ((TimeOfDayKeyframe)p2).time, lerpAmount);
if (lerpFromRight) {
timeOfDay = Interpolation.linear(timeOfDay, linearTimeOfDay, amount);
} else {
timeOfDay = Interpolation.linear(linearTimeOfDay, timeOfDay, amount);
}
}
keyframeHandler.applyTimeOfDay(Math.round(timeOfDay));
return new KeyframeChangeTimeOfDay(timeOfDay);
}
@Override
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
int timeOfDay = (int) Hermite.value(Maps.transformValues(keyframes, k -> (double) ((TimeOfDayKeyframe)k).time), amount);
return new KeyframeChangeTimeOfDay(timeOfDay);
}
public static class TypeAdapter implements JsonSerializer<TimeOfDayKeyframe>, JsonDeserializer<TimeOfDayKeyframe> {
@Override
@@ -1,5 +1,6 @@
package com.moulberry.flashback.keyframe.impl;
import com.google.common.collect.Maps;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@@ -11,13 +12,18 @@ import com.moulberry.flashback.Utils;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
import com.moulberry.flashback.spline.CatmullRom;
import com.moulberry.flashback.spline.Hermite;
import imgui.ImGui;
import imgui.type.ImString;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.function.Consumer;
public class TimelapseKeyframe extends Keyframe {
@@ -61,17 +67,17 @@ public class TimelapseKeyframe extends Keyframe {
}
@Override
public void apply(KeyframeHandler keyframeHandler) {
public KeyframeChange createChange() {
throw new UnsupportedOperationException();
}
@Override
public void applyInterpolated(KeyframeHandler keyframeHandler, Keyframe otherGeneric, float amount) {
public KeyframeChange createSmoothInterpolatedChange(Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount) {
throw new UnsupportedOperationException();
}
@Override
public void applyInterpolatedSmooth(KeyframeHandler keyframeHandler, Keyframe p1, Keyframe p2, Keyframe p3, float t0, float t1, float t2, float t3, float amount, float lerpAmount, boolean lerpFromRight) {
public KeyframeChange createHermiteInterpolatedChange(Map<Integer, Keyframe> keyframes, float amount) {
throw new UnsupportedOperationException();
}
@@ -11,7 +11,8 @@ public enum InterpolationType implements ComboOption {
EASE_IN(SidedInterpolationType.EASE, SidedInterpolationType.LINEAR, "Ease In (Linear Out)"),
EASE_OUT(SidedInterpolationType.LINEAR, SidedInterpolationType.EASE, "Ease Out (Linear In)"),
EASE_IN_OUT(SidedInterpolationType.EASE, SidedInterpolationType.EASE, "Ease In/Out"),
HOLD(SidedInterpolationType.HOLD, SidedInterpolationType.HOLD, "Hold");
HOLD(SidedInterpolationType.HOLD, SidedInterpolationType.HOLD, "Hold"),
HERMITE(SidedInterpolationType.HERMITE, SidedInterpolationType.HERMITE, "Smooth");
public static final InterpolationType[] INTERPOLATION_TYPES = values();
@@ -29,7 +30,8 @@ public enum InterpolationType implements ComboOption {
"Ease In (Linear Out)",
"Ease Out (Linear In)",
"Ease In/Out",
"Hold"
"Hold",
"Hermite"
};
public final SidedInterpolationType leftSide;
@@ -5,11 +5,23 @@ public enum SidedInterpolationType {
SMOOTH,
LINEAR,
EASE,
HOLD;
HOLD,
HERMITE;
private boolean isSpecial() {
return this == SidedInterpolationType.SMOOTH || this == SidedInterpolationType.HERMITE;
}
public static float interpolate(SidedInterpolationType left, SidedInterpolationType right, float amount) {
if (left == SidedInterpolationType.SMOOTH || right == SidedInterpolationType.SMOOTH) {
throw new IllegalArgumentException("SMOOTH not allowed");
if (left.isSpecial()) {
if (right.isSpecial()) {
left = SidedInterpolationType.LINEAR;
right = SidedInterpolationType.LINEAR;
} else {
left = right;
}
} else if (right.isSpecial()) {
right = left;
}
if (left == SidedInterpolationType.HOLD) {
@@ -1,6 +1,9 @@
package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
@@ -14,6 +17,11 @@ public class CameraKeyframeType implements KeyframeType<CameraKeyframe> {
private CameraKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeCameraPosition.class;
}
@Override
public @Nullable String icon() {
return "\ue04b";
@@ -3,6 +3,9 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import com.moulberry.flashback.keyframe.impl.CameraOrbitKeyframe;
import imgui.ImGui;
@@ -22,6 +25,11 @@ public class CameraOrbitKeyframeType implements KeyframeType<CameraOrbitKeyframe
private CameraOrbitKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeCameraPosition.class;
}
@Override
public @Nullable String icon() {
return "\ue577";
@@ -2,6 +2,10 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraShake;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraShakeKeyframe;
import com.moulberry.flashback.keyframe.impl.TickrateKeyframe;
import com.moulberry.flashback.playback.ReplayServer;
@@ -17,6 +21,10 @@ public class CameraShakeKeyframeType implements KeyframeType<CameraShakeKeyframe
private CameraShakeKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeCameraShake.class;
}
@Override
public @Nullable String icon() {
@@ -1,6 +1,10 @@
package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFov;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraOrbitKeyframe;
import com.moulberry.flashback.keyframe.impl.FOVKeyframe;
import com.moulberry.flashback.state.EditorState;
@@ -20,6 +24,11 @@ public class FOVKeyframeType implements KeyframeType<FOVKeyframe> {
private FOVKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeFov.class;
}
@Override
public @Nullable String icon() {
return "\ue3af";
@@ -2,6 +2,10 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeFreeze;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.FreezeKeyframe;
import imgui.ImGui;
import imgui.type.ImBoolean;
@@ -14,6 +18,11 @@ public class FreezeKeyframeType implements KeyframeType<FreezeKeyframe> {
private FreezeKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeFreeze.class;
}
@Override
public @Nullable String icon() {
return "\ueb3b";
@@ -2,6 +2,10 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.FOVKeyframe;
import com.moulberry.flashback.keyframe.impl.TickrateKeyframe;
import com.moulberry.flashback.playback.ReplayServer;
@@ -20,6 +24,11 @@ public class SpeedKeyframeType implements KeyframeType<TickrateKeyframe> {
private SpeedKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeTickrate.class;
}
@Override
public @Nullable String icon() {
return "\ue9e4";
@@ -3,6 +3,10 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.Flashback;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTimeOfDay;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.TickrateKeyframe;
import com.moulberry.flashback.keyframe.impl.TimeOfDayKeyframe;
import com.moulberry.flashback.playback.ReplayServer;
@@ -20,6 +24,11 @@ public class TimeOfDayKeyframeType implements KeyframeType<TimeOfDayKeyframe> {
private TimeOfDayKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeTimeOfDay.class;
}
@Override
public @Nullable String icon() {
return "\ue518";
@@ -3,6 +3,10 @@ package com.moulberry.flashback.keyframe.types;
import com.moulberry.flashback.Utils;
import com.moulberry.flashback.editor.ui.ImGuiHelper;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeCameraPosition;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.TimelapseKeyframe;
import imgui.ImGui;
import imgui.type.ImString;
@@ -15,6 +19,11 @@ public class TimelapseKeyframeType implements KeyframeType<TimelapseKeyframe> {
private TimelapseKeyframeType() {
}
@Override
public Class<? extends KeyframeChange> keyframeChangeType() {
return KeyframeChangeTickrate.class;
}
@Override
public @Nullable String icon() {
return "\ue422";
@@ -0,0 +1,65 @@
package com.moulberry.flashback.spline;
import net.minecraft.util.Mth;
import org.apache.commons.math3.analysis.interpolation.HermiteInterpolator;
import org.joml.Vector3d;
import java.util.Map;
public class Hermite {
public static Vector3d position(Map<Integer, Vector3d> map, float amount) {
HermiteInterpolator hermiteInterpolatorX = new HermiteInterpolator();
HermiteInterpolator hermiteInterpolatorY = new HermiteInterpolator();
HermiteInterpolator hermiteInterpolatorZ = new HermiteInterpolator();
double[] array = new double[1];
for (Map.Entry<Integer, Vector3d> entry : map.entrySet()) {
array[0] = entry.getValue().x;
hermiteInterpolatorX.addSamplePoint(entry.getKey(), array);
array[0] = entry.getValue().y;
hermiteInterpolatorY.addSamplePoint(entry.getKey(), array);
array[0] = entry.getValue().z;
hermiteInterpolatorZ.addSamplePoint(entry.getKey(), array);
}
return new Vector3d(
hermiteInterpolatorX.value(amount)[0],
hermiteInterpolatorY.value(amount)[0],
hermiteInterpolatorZ.value(amount)[0]
);
}
public static double value(Map<Integer, Double> map, float amount) {
HermiteInterpolator hermiteInterpolator = new HermiteInterpolator();
double[] array = new double[1];
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
array[0] = entry.getValue();
hermiteInterpolator.addSamplePoint(entry.getKey(), array);
}
return hermiteInterpolator.value(amount)[0];
}
public static double degrees(Map<Integer, Double> map, float amount) {
HermiteInterpolator hermiteInterpolator = new HermiteInterpolator();
double lastAngle = 0.0;
double[] array = new double[1];
for (Map.Entry<Integer, Double> entry : map.entrySet()) {
double angle = lastAngle + Mth.wrapDegrees(entry.getValue() - lastAngle);
array[0] = angle;
hermiteInterpolator.addSamplePoint(entry.getKey(), array);
lastAngle = angle;
}
return Mth.wrapDegrees(hermiteInterpolator.value(amount)[0]);
}
}
@@ -6,6 +6,7 @@ import com.moulberry.flashback.FlashbackGson;
import com.moulberry.flashback.editor.ui.ReplayUI;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import com.moulberry.flashback.keyframe.types.CameraKeyframeType;
@@ -125,23 +126,63 @@ public class EditorState {
}
public void applyKeyframes(KeyframeHandler keyframeHandler, float tick) {
Set<KeyframeType<?>> applied = new HashSet<>();
Set<Class<? extends KeyframeChange>> applied = new HashSet<>();
Map<Class<? extends KeyframeChange>, KeyframeTrack> maybeApplyLastTick = new HashMap<>();
for (KeyframeTrack keyframeTrack : this.currentScene().keyframeTracks) {
// Ignore lines that are disabled
if (!keyframeTrack.enabled) {
continue;
}
KeyframeType<?> baseType = keyframeTrack.keyframeType == CameraOrbitKeyframeType.INSTANCE ? CameraKeyframeType.INSTANCE : keyframeTrack.keyframeType;
Class<? extends KeyframeChange> keyframeChangeType = keyframeTrack.keyframeType.keyframeChangeType();
// Already applied a keyframe of this type earlier, skip
if (applied.contains(baseType)) {
if (applied.contains(keyframeChangeType)) {
continue;
}
if (!keyframeTrack.keyframeType.supportsHandler(keyframeHandler)) {
continue;
}
// Try to apply keyframes, mark applied if successful
if (keyframeTrack.tryApplyKeyframes(keyframeHandler, tick)) {
applied.add(baseType);
KeyframeChange change = keyframeTrack.createKeyframeChange(tick);
if (change == null) {
if (keyframeHandler.alwaysApplyLastKeyframe() && !keyframeTrack.keyframesByTick.isEmpty()) {
KeyframeTrack oldTrack = maybeApplyLastTick.get(keyframeChangeType);
if (oldTrack == null || keyframeTrack.keyframesByTick.lastKey() > oldTrack.keyframesByTick.lastKey()) {
maybeApplyLastTick.put(keyframeChangeType, keyframeTrack);
}
}
continue;
}
if (change.getClass() != keyframeChangeType) {
throw new IllegalStateException("Expected " + keyframeChangeType + ", got " + change.getClass() + ". Caused by: " + keyframeTrack.keyframeType.id());
}
applied.add(keyframeChangeType);
maybeApplyLastTick.remove(keyframeChangeType);
change.apply(keyframeHandler);
}
if (keyframeHandler.alwaysApplyLastKeyframe() && !maybeApplyLastTick.isEmpty()) {
for (Map.Entry<Class<? extends KeyframeChange>, KeyframeTrack> entry : maybeApplyLastTick.entrySet()) {
KeyframeTrack keyframeTrack = entry.getValue();
KeyframeChange change = keyframeTrack.createKeyframeChange(keyframeTrack.keyframesByTick.lastKey());
if (change == null) {
continue;
}
if (change.getClass() != entry.getKey()) {
throw new IllegalStateException("Expected " + entry.getKey() + ", got " + change.getClass() + ". Caused by: " + keyframeTrack.keyframeType.id());
}
change.apply(keyframeHandler);
}
}
}
@@ -1,17 +1,25 @@
package com.moulberry.flashback.state;
import com.google.common.collect.Maps;
import com.google.common.collect.Range;
import com.moulberry.flashback.editor.ui.ReplayUI;
import com.moulberry.flashback.keyframe.Keyframe;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.KeyframeChange;
import com.moulberry.flashback.keyframe.change.KeyframeChangeTickrate;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.handler.MinecraftKeyframeHandler;
import com.moulberry.flashback.keyframe.handler.ReplayServerKeyframeHandler;
import com.moulberry.flashback.keyframe.impl.CameraKeyframe;
import com.moulberry.flashback.keyframe.impl.TimelapseKeyframe;
import com.moulberry.flashback.keyframe.interpolation.InterpolationType;
import com.moulberry.flashback.keyframe.interpolation.SidedInterpolationType;
import com.moulberry.flashback.keyframe.types.TimelapseKeyframeType;
import imgui.type.ImString;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@@ -31,51 +39,43 @@ public class KeyframeTrack {
this.keyframeType = keyframeType;
}
public boolean tryApplyKeyframes(KeyframeHandler t, float tick) {
if (!t.supportedKeyframes().contains(this.keyframeType)) {
return false;
}
@Nullable
public KeyframeChange createKeyframeChange(float tick) {
if (this.keyframeType == TimelapseKeyframeType.INSTANCE) {
return this.tryApplyKeyframesTimelapse(t, tick);
return this.tryApplyKeyframesTimelapse(tick);
}
// Skip if empty
TreeMap<Integer, Keyframe> keyframeTimes = this.keyframesByTick;
if (keyframeTimes.isEmpty()) {
return false;
return null;
}
Map.Entry<Integer, Keyframe> lowerEntry = keyframeTimes.floorEntry((int) tick);
if (lowerEntry == null) {
return false;
return null;
}
Keyframe lowerKeyframe = lowerEntry.getValue();
if (tick == lowerEntry.getKey()) {
lowerKeyframe.apply(t);
return true;
return lowerKeyframe.createChange();
}
SidedInterpolationType leftInterpolation = lowerEntry.getValue().interpolationType().rightSide;
// Immediately apply hold
if (leftInterpolation == SidedInterpolationType.HOLD) {
lowerKeyframe.apply(t);
return true;
return lowerKeyframe.createChange();
}
// Get next entry, skip if tick is not between two keyframes
Map.Entry<Integer, Keyframe> ceilEntry = keyframeTimes.ceilingEntry(lowerEntry.getKey() + 1);
if (ceilEntry == null) {
if ((int) tick == lowerEntry.getKey()) {
lowerKeyframe.apply(t);
return true;
} else if (t.alwaysApplyLastKeyframe()) {
lowerKeyframe.apply(t);
return lowerKeyframe.createChange();
}
return false;
return null;
}
SidedInterpolationType rightInterpolation = ceilEntry.getValue().interpolationType().leftSide;
@@ -85,10 +85,11 @@ public class KeyframeTrack {
float amount = (tick - lowerEntry.getKey()) / (ceilEntry.getKey() - lowerEntry.getKey());
boolean isUsingSmooth = leftInterpolation == SidedInterpolationType.SMOOTH ||
rightInterpolation == SidedInterpolationType.SMOOTH;
KeyframeChange leftChange = null;
KeyframeChange rightChange = null;
if (isUsingSmooth) {
if (leftInterpolation == SidedInterpolationType.SMOOTH ||
rightInterpolation == SidedInterpolationType.SMOOTH) {
Map.Entry<Integer, Keyframe> beforeEntry = keyframeTimes.floorEntry(lowerEntry.getKey() - 1);
if (beforeEntry == null || beforeEntry.getValue().interpolationType() == InterpolationType.HOLD) { // don't include the right-side of the hold keyframe
beforeEntry = lowerEntry;
@@ -99,39 +100,109 @@ public class KeyframeTrack {
afterAfterEntry = ceilEntry;
}
float lerpAmount;
boolean lerpFromRight;
if (leftInterpolation != SidedInterpolationType.SMOOTH) {
lerpAmount = SidedInterpolationType.interpolate(leftInterpolation, leftInterpolation, amount);
lerpFromRight = false;
} else if (rightInterpolation != SidedInterpolationType.SMOOTH) {
lerpAmount = SidedInterpolationType.interpolate(rightInterpolation, rightInterpolation, amount);
lerpFromRight = true;
} else {
lerpAmount = -1;
lerpFromRight = false;
KeyframeChange smoothChange = beforeEntry.getValue().createSmoothInterpolatedChange(lowerEntry.getValue(), ceilEntry.getValue(), afterAfterEntry.getValue(),
beforeEntry.getKey(), lowerEntry.getKey(), ceilEntry.getKey(), afterAfterEntry.getKey(), amount);
if (leftInterpolation == SidedInterpolationType.SMOOTH) {
leftChange = smoothChange;
}
if (rightInterpolation == SidedInterpolationType.SMOOTH) {
rightChange = smoothChange;
}
}
if (leftInterpolation == SidedInterpolationType.HERMITE ||
rightInterpolation == SidedInterpolationType.HERMITE) {
Integer minKey = lowerEntry.getKey();
while (minKey != null) {
Map.Entry<Integer, Keyframe> before = keyframeTimes.floorEntry(minKey - 1);
if (before == null) {
minKey = null;
break;
} else if (before.getValue().interpolationType() == InterpolationType.HOLD) {
// don't include the right-side of the hold keyframe, or anything before it
break;
}
minKey = before.getKey();
}
beforeEntry.getValue().applyInterpolatedSmooth(t, lowerEntry.getValue(), ceilEntry.getValue(), afterAfterEntry.getValue(),
beforeEntry.getKey(), lowerEntry.getKey(), ceilEntry.getKey(), afterAfterEntry.getKey(), amount, lerpAmount, lerpFromRight);
} else {
amount = SidedInterpolationType.interpolate(leftInterpolation, rightInterpolation, amount);
Integer maxKey = ceilEntry.getKey();
if (amount == 0.0) {
lowerKeyframe.apply(t);
while (maxKey != null) {
Map.Entry<Integer, Keyframe> after = keyframeTimes.ceilingEntry(maxKey + 1);
if (after == null) {
maxKey = null;
break;
} else if (after.getValue().interpolationType() == InterpolationType.HOLD) {
// include the hold keyframe, but not anything after it
maxKey = after.getKey();
break;
}
maxKey = after.getKey();
}
Map<Integer, Keyframe> subMap;
if (minKey != null) {
if (maxKey != null) {
subMap = Maps.subMap(this.keyframesByTick, Range.closed(minKey, maxKey));
} else {
subMap = Maps.subMap(this.keyframesByTick, Range.atLeast(minKey));
}
} else if (maxKey != null) {
subMap = Maps.subMap(this.keyframesByTick, Range.atMost(maxKey));
} else {
lowerKeyframe.applyInterpolated(t, ceilEntry.getValue(), amount);
subMap = this.keyframesByTick;
}
KeyframeChange hermiteChange = lowerKeyframe.createHermiteInterpolatedChange(subMap, tick);
if (leftInterpolation == SidedInterpolationType.HERMITE) {
leftChange = hermiteChange;
}
if (rightInterpolation == SidedInterpolationType.HERMITE) {
rightChange = hermiteChange;
}
}
// todo: hermite
if (leftChange == null || rightChange == null) {
double adjustedAmount = SidedInterpolationType.interpolate(leftInterpolation, rightInterpolation, amount);
KeyframeChange keyframeChange = lowerKeyframe.createChange();
if (adjustedAmount != 0.0) {
KeyframeChange keyframeChangeCeil = ceilEntry.getValue().createChange();
keyframeChange = keyframeChange.interpolate(keyframeChangeCeil, (float) adjustedAmount);
}
if (leftChange == null) {
leftChange = keyframeChange;
}
if (rightChange == null) {
rightChange = keyframeChange;
}
}
return true;
if (leftChange == rightChange) {
return leftChange;
} else if (leftChange == null) {
return rightChange;
} else if (rightChange == null) {
return leftChange;
} else {
return leftChange.interpolate(rightChange, amount);
}
}
private boolean tryApplyKeyframesTimelapse(KeyframeHandler t, float tick) {
@Nullable
private KeyframeChangeTickrate tryApplyKeyframesTimelapse(float tick) {
// Skip if empty
TreeMap<Integer, Keyframe> keyframeTimes = this.keyframesByTick;
if (keyframeTimes.isEmpty()) {
return false;
return null;
}
Map.Entry<Integer, Keyframe> lowerEntry = keyframeTimes.floorEntry((int) tick);
@@ -148,16 +219,14 @@ public class KeyframeTrack {
if (ceilTicks <= lowerTicks) {
ReplayUI.setInfoOverlayShort("Unable to timelapse. Right keyframe's time must be greater than left keyframe's time");
t.applyTickrate(20);
return new KeyframeChangeTickrate(20.0f);
} else {
double tickrate = (double) (ceilEntry.getKey() - lowerEntry.getKey()) / (ceilTicks - lowerTicks) * 20;
t.applyTickrate((float) tickrate);
return new KeyframeChangeTickrate((float) tickrate);
}
return true;
}
return false;
return null;
}
}
@@ -15,6 +15,7 @@ import com.moulberry.flashback.Utils;
import com.moulberry.flashback.combo_options.Sizing;
import com.moulberry.flashback.editor.ui.windows.TimelineWindow;
import com.moulberry.flashback.keyframe.KeyframeType;
import com.moulberry.flashback.keyframe.change.*;
import com.moulberry.flashback.keyframe.handler.KeyframeHandler;
import com.moulberry.flashback.keyframe.types.CameraKeyframeType;
import com.moulberry.flashback.keyframe.types.CameraOrbitKeyframeType;
@@ -59,7 +60,7 @@ public class CameraPath {
int replayTick = TimelineWindow.getCursorTick();
if (lastEditorStateModCount != state.modCount || lastCursorTick != replayTick) {
CameraPathArgs cameraPathArgs = createCameraPathArgs(state.currentScene(), replayTick, SUPPORTED_CAMERA_KEYFRAMES);
CameraPathArgs cameraPathArgs = createCameraPathArgs(state.currentScene(), replayTick);
if (lastEditorStateModCount != state.modCount || !cameraPathArgs.equals(lastCameraPathArgs)) {
lastCameraPathArgs = cameraPathArgs;
@@ -135,13 +136,13 @@ public class CameraPath {
private record CameraPathArgs(int lastLastCameraTick, int lastCameraTick, int nextCameraTick, int nextNextCameraTick) {}
private static CameraPathArgs createCameraPathArgs(EditorScene scene, int replayTick, Set<KeyframeType<?>> supportedKeyframes) {
private static CameraPathArgs createCameraPathArgs(EditorScene scene, int replayTick) {
int lastCameraTick = -1;
int nextCameraTick = -1;
for (int trackIndex = 0; trackIndex < scene.keyframeTracks.size(); trackIndex++) {
KeyframeTrack keyframeTrack = scene.keyframeTracks.get(trackIndex);
if (keyframeTrack.enabled && supportedKeyframes.contains(keyframeTrack.keyframeType) && !keyframeTrack.keyframesByTick.isEmpty()) {
if (keyframeTrack.enabled && keyframeTrack.keyframeType.keyframeChangeType() == KeyframeChangeCameraPosition.class && !keyframeTrack.keyframesByTick.isEmpty()) {
var lastEntry = keyframeTrack.keyframesByTick.floorEntry(replayTick);
var nextEntry = keyframeTrack.keyframesByTick.ceilingEntry(replayTick + 1);
@@ -163,7 +164,7 @@ public class CameraPath {
for (int trackIndex = 0; trackIndex < scene.keyframeTracks.size(); trackIndex++) {
KeyframeTrack keyframeTrack = scene.keyframeTracks.get(trackIndex);
if (keyframeTrack.enabled && supportedKeyframes.contains(keyframeTrack.keyframeType) && !keyframeTrack.keyframesByTick.isEmpty()) {
if (keyframeTrack.enabled &&keyframeTrack.keyframeType.keyframeChangeType() == KeyframeChangeCameraPosition.class && !keyframeTrack.keyframesByTick.isEmpty()) {
var lastLastEntry = lastCameraTick == -1 ? null : keyframeTrack.keyframesByTick.floorEntry(lastCameraTick - 1);
var nextNextEntry = nextCameraTick == -1 ? null : keyframeTrack.keyframesByTick.ceilingEntry(nextCameraTick + 1);
@@ -309,15 +310,13 @@ public class CameraPath {
cameraPoseStack.popPose();
}
private static final Set<KeyframeType<?>> SUPPORTED_CAMERA_KEYFRAMES = Set.of(CameraKeyframeType.INSTANCE, CameraOrbitKeyframeType.INSTANCE);
private static class CapturingKeyframeHandler implements KeyframeHandler {
private Vector3d position;
private Quaterniond angle;
@Override
public Set<KeyframeType<?>> supportedKeyframes() {
return SUPPORTED_CAMERA_KEYFRAMES;
public boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz) {
return clazz == KeyframeChangeCameraPosition.class;
}
@Override
@@ -336,8 +335,8 @@ public class CameraPath {
private float fov;
@Override
public Set<KeyframeType<?>> supportedKeyframes() {
return Set.of(FOVKeyframeType.INSTANCE);
public boolean supportsKeyframeChange(Class<? extends KeyframeChange> clazz) {
return clazz == KeyframeChangeFov.class;
}
@Override