| 1 | package net.minecraft.client.resources.model.cuboid;
|
| 2 |
|
| 3 | import com.google.gson.JsonArray;
|
| 4 | import com.google.gson.JsonDeserializationContext;
|
| 5 | import com.google.gson.JsonDeserializer;
|
| 6 | import com.google.gson.JsonElement;
|
| 7 | import com.google.gson.JsonObject;
|
| 8 | import com.google.gson.JsonParseException;
|
| 9 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 10 | import java.lang.reflect.Type;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.util.GsonHelper;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import org.joml.Quaternionf;
|
| 16 | import org.joml.Vector3f;
|
| 17 | import org.joml.Vector3fc;
|
| 18 |
|
| 19 | @Environment(EnvType.CLIENT)
|
| 20 | public record ItemTransform(Vector3fc rotation, Vector3fc translation, Vector3fc scale) {
|
| 21 | public static final ItemTransform NO_TRANSFORM = new ItemTransform(new Vector3f(), new Vector3f(), new Vector3f(1.0F, 1.0F, 1.0F));
|
| 22 |
|
| 23 | public void apply(final boolean applyLeftHandFix, final PoseStack.Pose pose) {
|
| 24 | if (this == NO_TRANSFORM) {
|
| 25 | pose.translate(-0.5F, -0.5F, -0.5F);
|
| 26 | } else {
|
| 27 | float translationX;
|
| 28 | float rotY;
|
| 29 | float rotZ;
|
| 30 | if (applyLeftHandFix) {
|
| 31 | translationX = -this.translation.x();
|
| 32 | rotY = -this.rotation.y();
|
| 33 | rotZ = -this.rotation.z();
|
| 34 | } else {
|
| 35 | translationX = this.translation.x();
|
| 36 | rotY = this.rotation.y();
|
| 37 | rotZ = this.rotation.z();
|
| 38 | }
|
| 39 |
|
| 40 | pose.translate(translationX, this.translation.y(), this.translation.z());
|
| 41 | pose.rotate(new Quaternionf().rotationXYZ(this.rotation.x() * (float) (Math.PI / 180.0), rotY * (float) (Math.PI / 180.0), rotZ * (float) (Math.PI / 180.0)));
|
| 42 | pose.scale(this.scale.x(), this.scale.y(), this.scale.z());
|
| 43 | pose.translate(-0.5F, -0.5F, -0.5F);
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | @Environment(EnvType.CLIENT)
|
| 48 | protected static class Deserializer implements JsonDeserializer<ItemTransform> {
|
| 49 | private static final Vector3fc DEFAULT_ROTATION = new Vector3f();
|
| 50 | private static final Vector3fc DEFAULT_TRANSLATION = new Vector3f();
|
| 51 | private static final Vector3fc DEFAULT_SCALE = new Vector3f(1.0F, 1.0F, 1.0F);
|
| 52 | public static final float MAX_TRANSLATION = 5.0F;
|
| 53 | public static final float MAX_SCALE = 4.0F;
|
| 54 |
|
| 55 | public ItemTransform deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
| 56 | JsonObject object = json.getAsJsonObject();
|
| 57 | Vector3f rotation = getVector3f(object, "rotation", DEFAULT_ROTATION);
|
| 58 | Vector3f translation = getVector3f(object, "translation", DEFAULT_TRANSLATION);
|
| 59 | translation.mul(0.0625F);
|
| 60 | translation.set(Mth.clamp(translation.x, -5.0F, 5.0F), Mth.clamp(translation.y, -5.0F, 5.0F), Mth.clamp(translation.z, -5.0F, 5.0F));
|
| 61 | Vector3f scale = getVector3f(object, "scale", DEFAULT_SCALE);
|
| 62 | scale.set(Mth.clamp(scale.x, -4.0F, 4.0F), Mth.clamp(scale.y, -4.0F, 4.0F), Mth.clamp(scale.z, -4.0F, 4.0F));
|
| 63 | return new ItemTransform(rotation, translation, scale);
|
| 64 | }
|
| 65 |
|
| 66 | private static Vector3f getVector3f(final JsonObject object, final String key, final Vector3fc def) {
|
| 67 | if (!object.has(key)) {
|
| 68 | return new Vector3f(def);
|
| 69 | }
|
| 70 |
|
| 71 | JsonArray vecArray = GsonHelper.getAsJsonArray(object, key);
|
| 72 | if (vecArray.size() != 3) {
|
| 73 | throw new JsonParseException("Expected 3 " + key + " values, found: " + vecArray.size());
|
| 74 | }
|
| 75 |
|
| 76 | float[] elements = new float[3];
|
| 77 |
|
| 78 | for (int i = 0; i < elements.length; i++) {
|
| 79 | elements[i] = GsonHelper.convertToFloat(vecArray.get(i), key + "[" + i + "]");
|
| 80 | }
|
| 81 |
|
| 82 | return new Vector3f(elements[0], elements[1], elements[2]);
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|