| 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.math.Quadrant;
|
| 10 | import java.lang.reflect.Type;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.core.Direction;
|
| 14 | import net.minecraft.util.GsonHelper;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public record CuboidFace(@Nullable Direction cullForDirection, int tintIndex, String texture, CuboidFace.@Nullable UVs uvs, Quadrant rotation) {
|
| 19 | public static final int NO_TINT = -1;
|
| 20 |
|
| 21 | public static float getU(final CuboidFace.UVs uvs, final Quadrant rotation, final int vertex) {
|
| 22 | return uvs.getVertexU(rotation.rotateVertexIndex(vertex)) / 16.0F;
|
| 23 | }
|
| 24 |
|
| 25 | public static float getV(final CuboidFace.UVs uvs, final Quadrant rotation, final int index) {
|
| 26 | return uvs.getVertexV(rotation.rotateVertexIndex(index)) / 16.0F;
|
| 27 | }
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | protected static class Deserializer implements JsonDeserializer<CuboidFace> {
|
| 31 | private static final int DEFAULT_TINT_INDEX = -1;
|
| 32 | private static final int DEFAULT_ROTATION = 0;
|
| 33 |
|
| 34 | public CuboidFace deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
|
| 35 | JsonObject object = json.getAsJsonObject();
|
| 36 | Direction cullDirection = getCullFacing(object);
|
| 37 | int tintIndex = getTintIndex(object);
|
| 38 | String texture = getTexture(object);
|
| 39 | CuboidFace.UVs uvs = getUVs(object);
|
| 40 | Quadrant rotation = getRotation(object);
|
| 41 | return new CuboidFace(cullDirection, tintIndex, texture, uvs, rotation);
|
| 42 | }
|
| 43 |
|
| 44 | private static int getTintIndex(final JsonObject object) {
|
| 45 | return GsonHelper.getAsInt(object, "tintindex", -1);
|
| 46 | }
|
| 47 |
|
| 48 | private static String getTexture(final JsonObject object) {
|
| 49 | return GsonHelper.getAsString(object, "texture");
|
| 50 | }
|
| 51 |
|
| 52 | private static @Nullable Direction getCullFacing(final JsonObject object) {
|
| 53 | String cullFace = GsonHelper.getAsString(object, "cullface", "");
|
| 54 | return Direction.byName(cullFace);
|
| 55 | }
|
| 56 |
|
| 57 | private static Quadrant getRotation(final JsonObject object) {
|
| 58 | int rotation = GsonHelper.getAsInt(object, "rotation", 0);
|
| 59 | return Quadrant.parseJson(rotation);
|
| 60 | }
|
| 61 |
|
| 62 | private static CuboidFace.@Nullable UVs getUVs(final JsonObject object) {
|
| 63 | if (!object.has("uv")) {
|
| 64 | return null;
|
| 65 | }
|
| 66 |
|
| 67 | JsonArray uvArray = GsonHelper.getAsJsonArray(object, "uv");
|
| 68 | if (uvArray.size() != 4) {
|
| 69 | throw new JsonParseException("Expected 4 uv values, found: " + uvArray.size());
|
| 70 | }
|
| 71 |
|
| 72 | float minU = GsonHelper.convertToFloat(uvArray.get(0), "minU");
|
| 73 | float minV = GsonHelper.convertToFloat(uvArray.get(1), "minV");
|
| 74 | float maxU = GsonHelper.convertToFloat(uvArray.get(2), "maxU");
|
| 75 | float maxV = GsonHelper.convertToFloat(uvArray.get(3), "maxV");
|
| 76 | return new CuboidFace.UVs(minU, minV, maxU, maxV);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | @Environment(EnvType.CLIENT)
|
| 81 | public record UVs(float minU, float minV, float maxU, float maxV) {
|
| 82 | public float getVertexU(final int index) {
|
| 83 | return index != 0 && index != 1 ? this.maxU : this.minU;
|
| 84 | }
|
| 85 |
|
| 86 | public float getVertexV(final int index) {
|
| 87 | return index != 0 && index != 3 ? this.maxV : this.minV;
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 |
|