package dev.lumareth.resonantcombat.anim; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import org.joml.Matrix3f; import org.joml.Matrix4f; import org.joml.Quaternionf; import org.joml.Vector3f; import java.util.Arrays; /** * The keyframes of an animation's "Cam" bone, as exported by the Epic Fight Blockbench * plugin (matrix format: per key a row-major 4x4, translation in [3], [7], [11]). *

* Space: the exported (Blender) model space of the animation, origin at the character's * feet: +X right, +Y forward (where the character faces), +Z up, units in blocks. * Convention for the Cam bone: it looks along its local +Y and its local +Z is up, i.e. * an unrotated Cam looks the way the character faces. */ public final class CamTrack { public static final String BONE = "Cam"; private final float[] times; private final Vector3f[] positions; private final Quaternionf[] rotations; private CamTrack(float[] times, Vector3f[] positions, Quaternionf[] rotations) { this.times = times; this.positions = positions; this.rotations = rotations; } /** The Cam track of an Epic Fight animation JSON, or null if it has none (or it is broken). */ public static CamTrack fromAnimationJson(JsonObject root) { if (!root.has("animation")) return null; for (JsonElement el : root.getAsJsonArray("animation")) { JsonObject track = el.getAsJsonObject(); if (!track.has("name") || !BONE.equalsIgnoreCase(track.get("name").getAsString())) continue; if (!track.has("time") || !track.has("transform")) return null; JsonArray t = track.getAsJsonArray("time"), m = track.getAsJsonArray("transform"); int n = Math.min(t.size(), m.size()); if (n == 0) return null; float[] times = new float[n]; Vector3f[] pos = new Vector3f[n]; Quaternionf[] rot = new Quaternionf[n]; for (int i = 0; i < n; i++) { times[i] = t.get(i).getAsFloat(); JsonArray a = m.get(i).getAsJsonArray(); if (a.size() != 16) return null; // not matrix format float[] f = new float[16]; for (int k = 0; k < 16; k++) f[k] = a.get(k).getAsFloat(); Matrix4f mat = rowMajor(f); pos[i] = mat.getTranslation(new Vector3f()); rot[i] = mat.get3x3(new Matrix3f()).getNormalizedRotation(new Quaternionf()); } return new CamTrack(times, pos, rot); } return null; } /** Row-major float[16] (Epic Fight JSON) to a JOML matrix (column-major). */ public static Matrix4f rowMajor(float[] a) { return new Matrix4f(a[0], a[4], a[8], a[12], a[1], a[5], a[9], a[13], a[2], a[6], a[10], a[14], a[3], a[7], a[11], a[15]); } /** The Cam transform at an animation time (seconds): position lerped, rotation slerped. */ public Matrix4f sample(float time) { int n = times.length; if (n == 1 || time <= times[0]) return compose(positions[0], rotations[0]); if (time >= times[n - 1]) return compose(positions[n - 1], rotations[n - 1]); int i = Arrays.binarySearch(times, time); if (i >= 0) return compose(positions[i], rotations[i]); int hi = -i - 1, lo = hi - 1; float k = (time - times[lo]) / (times[hi] - times[lo]); return compose(positions[lo].lerp(positions[hi], k, new Vector3f()), rotations[lo].slerp(rotations[hi], k, new Quaternionf())); } public float endTime() { return times[times.length - 1]; } private static Matrix4f compose(Vector3f p, Quaternionf q) { return new Matrix4f().translation(p).rotate(q); } }