taces -> spabs

This commit is contained in:
George Paton 2024-10-08 19:10:17 +11:00
parent 9ae58dfdb3
commit 142fec169e

View File

@ -21,163 +21,163 @@ import com.hbm.render.anim.BusAnimationSequence.Dimension;
public class AnimationLoader { public class AnimationLoader {
// The collada loader is great, but is not so backwards compatible and spews keyframes rather than doing interpolation // The collada loader is great, but is not so backwards compatible and spews keyframes rather than doing interpolation
// Yeah - more animation loading is not so great, but 3mb for a single door opening is maybe overkill on a 50mb mod // Yeah - more animation loading is not so great, but 3mb for a single door opening is maybe overkill on a 50mb mod
// and even though the format supports multiple animations, no fucking animation software will actually export multiple animations, // and even though the format supports multiple animations, no fucking animation software will actually export multiple animations,
// (even though blender even has a fucking toggle for it, but it doesn't _do_ anything) // (even though blender even has a fucking toggle for it, but it doesn't _do_ anything)
// This instead just loads transformation data from a JSON file, turning it into a set of BusAnimations // This instead just loads transformation data from a JSON file, turning it into a set of BusAnimations
// See ntm-animator.blend for a JSON format creation script // See ntm-animator.blend for a JSON format creation script
// "How do I make animations?" // "How do I make animations?"
// See ntm-animator.blend, it has the Colt/Python already setup and animated as an example, it'll generate JSON data that this can load // See ntm-animator.blend, it has the Colt/Python already setup and animated as an example, it'll generate JSON data that this can load
public static final Gson gson = new Gson(); public static final Gson gson = new Gson();
public static HashMap<String, BusAnimation> load(ResourceLocation file) { public static HashMap<String, BusAnimation> load(ResourceLocation file) {
HashMap<String, BusAnimation> animations = new HashMap<String, BusAnimation>(); HashMap<String, BusAnimation> animations = new HashMap<String, BusAnimation>();
InputStream in; InputStream in;
try { try {
in = Minecraft.getMinecraft().getResourceManager().getResource(file).getInputStream(); in = Minecraft.getMinecraft().getResourceManager().getResource(file).getInputStream();
} catch (IOException ex) { } catch (IOException ex) {
return null; return null;
} }
InputStreamReader reader = new InputStreamReader(in); InputStreamReader reader = new InputStreamReader(in);
JsonObject json = gson.fromJson(reader, JsonObject.class); JsonObject json = gson.fromJson(reader, JsonObject.class);
// Load our model offsets, we'll place these into all the sequences that share the name of the offset // Load our model offsets, we'll place these into all the sequences that share the name of the offset
// The offsets are only required when sequences are played for an object, which is why we don't globally offset! The obj rendering handles the non-animated case fine // The offsets are only required when sequences are played for an object, which is why we don't globally offset! The obj rendering handles the non-animated case fine
// Effectively, this removes double translation AND ensures that rotations occur around the individual object origin, rather than the weapon origin // Effectively, this removes double translation AND ensures that rotations occur around the individual object origin, rather than the weapon origin
HashMap<String, double[]> offsets = new HashMap<String, double[]>(); HashMap<String, double[]> offsets = new HashMap<String, double[]>();
for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("offset").entrySet()) { for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("offset").entrySet()) {
double[] offset = new double[3]; double[] offset = new double[3];
for(int i = 0; i < 3; i++) { for(int i = 0; i < 3; i++) {
offset[i] = root.getValue().getAsJsonArray().get(i).getAsDouble(); offset[i] = root.getValue().getAsJsonArray().get(i).getAsDouble();
} }
offsets.put(root.getKey(), offset); offsets.put(root.getKey(), offset);
} }
// Top level parsing, this is for the animation name as set in Blender // Top level parsing, this is for the animation name as set in Blender
for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("anim").entrySet()) { for(Map.Entry<String, JsonElement> root : json.getAsJsonObject("anim").entrySet()) {
BusAnimation animation = new BusAnimation(); BusAnimation animation = new BusAnimation();
// Loading the buses for this animation // Loading the buses for this animation
JsonObject entryObject = root.getValue().getAsJsonObject(); JsonObject entryObject = root.getValue().getAsJsonObject();
for(Map.Entry<String, JsonElement> model : entryObject.entrySet()) { for(Map.Entry<String, JsonElement> model : entryObject.entrySet()) {
String modelName = model.getKey(); String modelName = model.getKey();
double[] offset = new double[3]; double[] offset = new double[3];
if (offsets.containsKey(modelName)) offset = offsets.get(modelName); if (offsets.containsKey(modelName)) offset = offsets.get(modelName);
animation.addBus(modelName, loadSequence(model.getValue().getAsJsonObject(), offset)); animation.addBus(modelName, loadSequence(model.getValue().getAsJsonObject(), offset));
} }
animations.put(root.getKey(), animation); animations.put(root.getKey(), animation);
} }
return animations; return animations;
} }
private static BusAnimationSequence loadSequence(JsonObject json, double[] offset) { private static BusAnimationSequence loadSequence(JsonObject json, double[] offset) {
BusAnimationSequence sequence = new BusAnimationSequence(); BusAnimationSequence sequence = new BusAnimationSequence();
// Location fcurves // Location fcurves
if(json.has("location")) { if(json.has("location")) {
JsonObject location = json.getAsJsonObject("location"); JsonObject location = json.getAsJsonObject("location");
if(location.has("x")) { if(location.has("x")) {
addToSequence(sequence, Dimension.TX, location.getAsJsonArray("x")); addToSequence(sequence, Dimension.TX, location.getAsJsonArray("x"));
} }
if(location.has("y")) { if(location.has("y")) {
addToSequence(sequence, Dimension.TY, location.getAsJsonArray("y")); addToSequence(sequence, Dimension.TY, location.getAsJsonArray("y"));
} }
if(location.has("z")) { if(location.has("z")) {
addToSequence(sequence, Dimension.TZ, location.getAsJsonArray("z")); addToSequence(sequence, Dimension.TZ, location.getAsJsonArray("z"));
} }
} }
// Rotation fcurves, only euler at the moment // Rotation fcurves, only euler at the moment
if(json.has("rotation_euler")) { if(json.has("rotation_euler")) {
JsonObject rotation = json.getAsJsonObject("rotation_euler"); JsonObject rotation = json.getAsJsonObject("rotation_euler");
if(rotation.has("x")) { if(rotation.has("x")) {
addToSequence(sequence, Dimension.RX, rotation.getAsJsonArray("x")); addToSequence(sequence, Dimension.RX, rotation.getAsJsonArray("x"));
} }
if(rotation.has("y")) { if(rotation.has("y")) {
addToSequence(sequence, Dimension.RY, rotation.getAsJsonArray("y")); addToSequence(sequence, Dimension.RY, rotation.getAsJsonArray("y"));
} }
if(rotation.has("z")) { if(rotation.has("z")) {
addToSequence(sequence, Dimension.RZ, rotation.getAsJsonArray("z")); addToSequence(sequence, Dimension.RZ, rotation.getAsJsonArray("z"));
} }
} }
// Scale fcurves // Scale fcurves
if(json.has("scale")) { if(json.has("scale")) {
JsonObject scale = json.getAsJsonObject("scale"); JsonObject scale = json.getAsJsonObject("scale");
if(scale.has("x")) { if(scale.has("x")) {
addToSequence(sequence, Dimension.SX, scale.getAsJsonArray("x")); addToSequence(sequence, Dimension.SX, scale.getAsJsonArray("x"));
} }
if(scale.has("y")) { if(scale.has("y")) {
addToSequence(sequence, Dimension.SY, scale.getAsJsonArray("y")); addToSequence(sequence, Dimension.SY, scale.getAsJsonArray("y"));
} }
if(scale.has("z")) { if(scale.has("z")) {
addToSequence(sequence, Dimension.SZ, scale.getAsJsonArray("z")); addToSequence(sequence, Dimension.SZ, scale.getAsJsonArray("z"));
} }
} }
sequence.offset = offset; sequence.offset = offset;
return sequence; return sequence;
} }
private static void addToSequence(BusAnimationSequence sequence, Dimension dimension, JsonArray array) { private static void addToSequence(BusAnimationSequence sequence, Dimension dimension, JsonArray array) {
IType prevInterp = null; IType prevInterp = null;
for(JsonElement element : array) { for(JsonElement element : array) {
BusAnimationKeyframe keyframe = loadKeyframe(element, prevInterp); BusAnimationKeyframe keyframe = loadKeyframe(element, prevInterp);
prevInterp = keyframe.interpolationType; prevInterp = keyframe.interpolationType;
sequence.addKeyframe(dimension, keyframe); sequence.addKeyframe(dimension, keyframe);
} }
} }
private static BusAnimationKeyframe loadKeyframe(JsonElement element, IType prevInterp) { private static BusAnimationKeyframe loadKeyframe(JsonElement element, IType prevInterp) {
JsonArray array = element.getAsJsonArray(); JsonArray array = element.getAsJsonArray();
double value = array.get(0).getAsDouble(); double value = array.get(0).getAsDouble();
int duration = array.get(1).getAsInt(); int duration = array.get(1).getAsInt();
IType interpolation = array.size() >= 3 ? IType.valueOf(array.get(2).getAsString()) : IType.LINEAR; IType interpolation = array.size() >= 3 ? IType.valueOf(array.get(2).getAsString()) : IType.LINEAR;
EType easing = array.size() >= 4 ? EType.valueOf(array.get(3).getAsString()) : EType.AUTO; EType easing = array.size() >= 4 ? EType.valueOf(array.get(3).getAsString()) : EType.AUTO;
BusAnimationKeyframe keyframe = new BusAnimationKeyframe(value, duration, interpolation, easing); BusAnimationKeyframe keyframe = new BusAnimationKeyframe(value, duration, interpolation, easing);
int i = 4; int i = 4;
if(prevInterp == IType.BEZIER) { if(prevInterp == IType.BEZIER) {
keyframe.leftX = array.get(i++).getAsDouble(); keyframe.leftX = array.get(i++).getAsDouble();
keyframe.leftY = array.get(i++).getAsDouble(); keyframe.leftY = array.get(i++).getAsDouble();
keyframe.leftType = HType.valueOf(array.get(i++).getAsString()); keyframe.leftType = HType.valueOf(array.get(i++).getAsString());
} }
if(interpolation == IType.LINEAR || interpolation == IType.CONSTANT) if(interpolation == IType.LINEAR || interpolation == IType.CONSTANT)
return keyframe; return keyframe;
if(interpolation == IType.BEZIER) { if(interpolation == IType.BEZIER) {
keyframe.rightX = array.get(i++).getAsDouble(); keyframe.rightX = array.get(i++).getAsDouble();
keyframe.rightY = array.get(i++).getAsDouble(); keyframe.rightY = array.get(i++).getAsDouble();
keyframe.rightType = HType.valueOf(array.get(i++).getAsString()); keyframe.rightType = HType.valueOf(array.get(i++).getAsString());
} }
if(interpolation == IType.ELASTIC) { if(interpolation == IType.ELASTIC) {
keyframe.amplitude = array.get(i++).getAsDouble(); keyframe.amplitude = array.get(i++).getAsDouble();
keyframe.period = array.get(i++).getAsDouble(); keyframe.period = array.get(i++).getAsDouble();
} else if(interpolation == IType.BACK) { } else if(interpolation == IType.BACK) {
keyframe.back = array.get(i++).getAsDouble(); keyframe.back = array.get(i++).getAsDouble();
} }
return keyframe; return keyframe;
} }
} }