92 lines
3.3 KiB
Java
92 lines
3.3 KiB
Java
package net.minecraft.client.renderer.block.model;
|
|
|
|
import com.google.gson.JsonArray;
|
|
import com.google.gson.JsonDeserializationContext;
|
|
import com.google.gson.JsonDeserializer;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonParseException;
|
|
import com.mojang.math.Quadrant;
|
|
import java.lang.reflect.Type;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.util.GsonHelper;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public record BlockElementFace(@Nullable Direction cullForDirection, int tintIndex, String texture, @Nullable BlockElementFace.UVs uvs, Quadrant rotation) {
|
|
public static final int NO_TINT = -1;
|
|
|
|
public static float getU(BlockElementFace.UVs uvs, Quadrant rotation, int vertexIndex) {
|
|
return uvs.getVertexU(rotation.rotateVertexIndex(vertexIndex)) / 16.0F;
|
|
}
|
|
|
|
public static float getV(BlockElementFace.UVs uvs, Quadrant rotation, int vertexIndex) {
|
|
return uvs.getVertexV(rotation.rotateVertexIndex(vertexIndex)) / 16.0F;
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
protected static class Deserializer implements JsonDeserializer<BlockElementFace> {
|
|
private static final int DEFAULT_TINT_INDEX = -1;
|
|
private static final int DEFAULT_ROTATION = 0;
|
|
|
|
public BlockElementFace deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
|
|
JsonObject jsonObject = json.getAsJsonObject();
|
|
Direction direction = getCullFacing(jsonObject);
|
|
int i = getTintIndex(jsonObject);
|
|
String string = getTexture(jsonObject);
|
|
BlockElementFace.UVs uVs = getUVs(jsonObject);
|
|
Quadrant quadrant = getRotation(jsonObject);
|
|
return new BlockElementFace(direction, i, string, uVs, quadrant);
|
|
}
|
|
|
|
private static int getTintIndex(JsonObject json) {
|
|
return GsonHelper.getAsInt(json, "tintindex", -1);
|
|
}
|
|
|
|
private static String getTexture(JsonObject json) {
|
|
return GsonHelper.getAsString(json, "texture");
|
|
}
|
|
|
|
@Nullable
|
|
private static Direction getCullFacing(JsonObject json) {
|
|
String string = GsonHelper.getAsString(json, "cullface", "");
|
|
return Direction.byName(string);
|
|
}
|
|
|
|
private static Quadrant getRotation(JsonObject json) {
|
|
int i = GsonHelper.getAsInt(json, "rotation", 0);
|
|
return Quadrant.parseJson(i);
|
|
}
|
|
|
|
@Nullable
|
|
private static BlockElementFace.UVs getUVs(JsonObject json) {
|
|
if (!json.has("uv")) {
|
|
return null;
|
|
} else {
|
|
JsonArray jsonArray = GsonHelper.getAsJsonArray(json, "uv");
|
|
if (jsonArray.size() != 4) {
|
|
throw new JsonParseException("Expected 4 uv values, found: " + jsonArray.size());
|
|
} else {
|
|
float f = GsonHelper.convertToFloat(jsonArray.get(0), "minU");
|
|
float g = GsonHelper.convertToFloat(jsonArray.get(1), "minV");
|
|
float h = GsonHelper.convertToFloat(jsonArray.get(2), "maxU");
|
|
float i = GsonHelper.convertToFloat(jsonArray.get(3), "maxV");
|
|
return new BlockElementFace.UVs(f, g, h, i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public record UVs(float minU, float minV, float maxU, float maxV) {
|
|
public float getVertexU(int vertexIndex) {
|
|
return vertexIndex != 0 && vertexIndex != 1 ? this.maxU : this.minU;
|
|
}
|
|
|
|
public float getVertexV(int vertexIndex) {
|
|
return vertexIndex != 0 && vertexIndex != 3 ? this.maxV : this.minV;
|
|
}
|
|
}
|
|
}
|