Skip to content

Commit

Permalink
fix: Try to fix the anim model problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Dec 1, 2024
1 parent e687697 commit 86887e3
Show file tree
Hide file tree
Showing 26 changed files with 487 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"kasuga_lib:model"
],
"model": [
"kasuga_lib:block/test/test_model_complicate"
"kasuga_lib:block/test/test_model_complicate",
"kasuga_lib:panel/arrow",
"kasuga_lib:panel/panel",
"kasuga_lib:panel/arrow_2"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"loader": "kasuga_lib:bedrock_model",
"model": "kasuga_lib:block/test/test_model_complicate",
"texture": "kasuga_lib:common/test/test_model_complicate",
"render_type": "translucent"
"render_type": "translucent",
"identifier": "kasuga_lib:test_model_complicate"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"loader": "kasuga_lib:bedrock_model",
"loader": "kasuga_lib:bedrock_animated",
"model": "kasuga_lib:panel/arrow",
"texture": "kasuga_lib:panel/arrow_texture",
"render_type": "translucent"
"geometry": "geometry.unknown",
"render_type": "translucent",
"identifier": "kasuga_lib:arrow"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"format_version": "1.12.0",
"minecraft:geometry": [
{
"description": {
"identifier": "geometry.unknown",
"texture_width": 16,
"texture_height": 16,
"visible_bounds_width": 3,
"visible_bounds_height": 1.5,
"visible_bounds_offset": [0, 0.25, 0]
},
"bones": [
{
"name": "bone",
"pivot": [0, 0, 0],
"rotation": [0, 0, -90],
"cubes": [
{
"origin": [-0.5, 0, -0.5],
"size": [1, 11, 1],
"uv": {
"north": {"uv": [1, 1], "uv_size": [1, 11]},
"east": {"uv": [0, 1], "uv_size": [1, 11]},
"south": {"uv": [3, 1], "uv_size": [1, 11]},
"west": {"uv": [2, 1], "uv_size": [1, 11]},
"up": {"uv": [1, 0], "uv_size": [1, 1]},
"down": {"uv": [2, 1], "uv_size": [1, -1]}
}
},
{
"origin": [-0.75, 12, -0.75],
"size": [1.5, 1, 1.5],
"uv": {
"north": {"uv": [5, 4], "uv_size": [1, 1]},
"east": {"uv": [4, 4], "uv_size": [1, 1]},
"south": {"uv": [7, 4], "uv_size": [1, 1]},
"west": {"uv": [6, 4], "uv_size": [1, 1]},
"up": {"uv": [5, 3], "uv_size": [1, 1]},
"down": {"uv": [6, 4], "uv_size": [1, -1]}
}
},
{
"origin": [-1, 11, -1],
"size": [2, 1, 2],
"uv": {
"north": {"uv": [6, 2], "uv_size": [2, 1]},
"east": {"uv": [4, 2], "uv_size": [2, 1]},
"south": {"uv": [10, 2], "uv_size": [2, 1]},
"west": {"uv": [8, 2], "uv_size": [2, 1]},
"up": {"uv": [6, 0], "uv_size": [2, 2]},
"down": {"uv": [8, 2], "uv_size": [2, -2]}
}
},
{
"origin": [-0.5, 13, -0.5],
"size": [1, 1, 1],
"uv": {
"north": {"uv": [5, 6], "uv_size": [1, 1]},
"east": {"uv": [4, 6], "uv_size": [1, 1]},
"south": {"uv": [7, 6], "uv_size": [1, 1]},
"west": {"uv": [6, 6], "uv_size": [1, 1]},
"up": {"uv": [5, 5], "uv_size": [1, 1]},
"down": {"uv": [6, 6], "uv_size": [1, -1]}
}
}
]
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"loader": "kasuga_lib:bedrock_animated",
"model": "kasuga_lib:panel/arrow_2",
"texture": "kasuga_lib:panel/arrow_texture",
"geometry": "geometry.unknown",
"render_type": "translucent",
"identifier": "kasuga_lib:arrow_2"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"loader": "kasuga_lib:bedrock_model",
"loader": "kasuga_lib:bedrock_animated",
"model": "kasuga_lib:panel/panel",
"texture": "kasuga_lib:panel/panel_texture",
"render_type": "translucent"
"render_type": "translucent",
"geometry": "geometry.unknown",
"identifier": "kasuga_lib:panel"
}
12 changes: 1 addition & 11 deletions src/main/java/kasuga/lib/core/KasugaLibClient.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package kasuga.lib.core;

import kasuga.lib.core.client.model.BedrockModelLoader;
import kasuga.lib.core.client.model.model_json.UnbakedBedrockModel;
import kasuga.lib.core.client.render.texture.ImageMask;
import kasuga.lib.core.client.render.texture.StaticImage;
import kasuga.lib.core.client.model.model_json.BedrockModel;
import kasuga.lib.core.client.render.texture.StaticImageHolder;
import kasuga.lib.core.util.LazyRecomputable;
import kasuga.lib.core.util.data_type.Pair;
import kasuga.lib.core.util.projectile.PanelRenderer;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

import java.io.IOException;
import java.util.HashSet;

import static kasuga.lib.KasugaLib.MOD_ID;
Expand All @@ -23,12 +19,6 @@ public class KasugaLibClient {
public static final StaticImageHolder NO_IMG =
new StaticImageHolder(new ResourceLocation(MOD_ID, "textures/gui/no_img.png"));

public static final LazyRecomputable<UnbakedBedrockModel> panel =
BedrockModelLoader.fromFile(new ResourceLocation(MOD_ID, "panel/panel"));

public static final LazyRecomputable<UnbakedBedrockModel> arrow =
BedrockModelLoader.fromFile(new ResourceLocation(MOD_ID, "panel/arrow"));

public static final HashSet<PanelRenderer> PANEL_RENDERERS = new HashSet<>();

public static void invoke() {}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/kasuga/lib/core/KasugaLibStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import kasuga.lib.core.channel.test.ChannelTest;
import kasuga.lib.core.client.animation.Constants;
import kasuga.lib.core.client.frontend.gui.GuiEngine;
import kasuga.lib.core.client.model.ModelPreloadManager;
import kasuga.lib.core.events.both.BothSetupEvent;
import kasuga.lib.core.events.both.EntityAttributeEvent;
import kasuga.lib.core.events.client.*;
Expand Down Expand Up @@ -118,6 +119,7 @@ public KasugaLibStacks(IEventBus bus) {
if (Envs.isDevEnvironment()) KasugaLibClient.invoke();
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, ()-> TargetsClient::register);
bus.addListener(REGISTRY::hookFluidAndRenders);
bus.addListener(ModelPreloadManager.INSTANCE::registerPreloadedModel);
}

MinecraftForge.EVENT_BUS.addListener(ServerResourceListener::onServerStarting);
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/kasuga/lib/core/client/model/AnimModelLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package kasuga.lib.core.client.model;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.model.anim_model.AnimModel;
import kasuga.lib.core.client.model.model_json.BedrockModel;
import kasuga.lib.core.client.model.model_json.Geometry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceManagerReloadListener;
import net.minecraftforge.client.model.geometry.IGeometryLoader;

import javax.annotation.Nullable;
import java.lang.reflect.Type;
import java.util.HashMap;

public class AnimModelLoader implements IGeometryLoader<AnimModel>, ResourceManagerReloadListener, ItemTransformProvider {

private ResourceManager manager;
private final HashMap<ResourceLocation, AnimModel> MODELS;

public static final AnimModelLoader INSTANCE = new AnimModelLoader();

private AnimModelLoader() {
this.MODELS = new HashMap<>();
}

@Override
public void onResourceManagerReload(ResourceManager pResourceManager) {
this.manager = pResourceManager;
}

@Override
public AnimModel read(JsonObject jsonObject, JsonDeserializationContext deserializationContext) throws JsonParseException {
BedrockModel model = BedrockModelLoader.readModel(jsonObject, deserializationContext);
ResourceLocation renderTypeHint;
if (jsonObject.has("render_type")) {
renderTypeHint = new ResourceLocation(jsonObject.get("render_type").getAsString());
} else {
renderTypeHint = new ResourceLocation("solid");
}
String geometry = jsonObject.get("geometry").getAsString();
Geometry geo = null;
for (Geometry g : model.getGeometries()) {
if (g.getDescription().getIdentifier().equals(geometry)) {
geo = g;
break;
}
}
if (geo == null) return null;
ResourceLocation identifier = new ResourceLocation(jsonObject.get("identifier").getAsString());
AnimModel animModel = new AnimModel(geo, model.getMaterials(), renderTypeHint);
if (MODELS.containsKey(identifier)) {
KasugaLib.MAIN_LOGGER.warn("Anim Model: " + identifier + " already exists, COVERED!");
}
MODELS.put(identifier, animModel);
return animModel;
}

public @Nullable AnimModel getModel(ResourceLocation location) {
return MODELS.get(location);
}
}
91 changes: 22 additions & 69 deletions src/main/java/kasuga/lib/core/client/model/BedrockModelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import com.google.common.collect.Maps;
import com.google.gson.*;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.model.model_json.UnbakedBedrockModel;
import kasuga.lib.core.client.model.model_json.BedrockModel;
import kasuga.lib.core.client.model.anim_model.AnimModel;
import kasuga.lib.core.client.model.model_json.Geometry;
import kasuga.lib.core.util.LazyRecomputable;
import kasuga.lib.core.util.Resources;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.block.model.ItemTransform;
import net.minecraft.client.renderer.block.model.ItemTransforms;
Expand All @@ -31,16 +30,13 @@
import java.util.List;

@OnlyIn(Dist.CLIENT)
public class BedrockModelLoader implements IGeometryLoader<UnbakedBedrockModel>, ResourceManagerReloadListener, ItemTransformProvider {
public class BedrockModelLoader implements IGeometryLoader<BedrockModel>, ResourceManagerReloadListener, ItemTransformProvider {

public static BedrockModelLoader INSTANCE = new BedrockModelLoader();
private ResourceManager manager;
public static final HashSet<ResourceLocation> UNREGISTERED = new HashSet<>();
public static final HashSet<Material> ADDITIONAL_MATERIALS = new HashSet<>();
public static boolean registerFired = false;
public static final HashMap<ResourceLocation, UnbakedBedrockModel> MODELS = new HashMap<>();
public static final HashMap<ResourceLocation, BedrockModel> MODELS = new HashMap<>();
public static final ResourceLocation MISSING_MODEL_LOCATION = new ResourceLocation(KasugaLib.MOD_ID, "default/missing_model");
private static final LazyRecomputable<UnbakedBedrockModel> MISSING = new LazyRecomputable<>(() -> MODELS.get(MISSING_MODEL_LOCATION));
private static final LazyRecomputable<BedrockModel> MISSING = new LazyRecomputable<>(() -> MODELS.get(MISSING_MODEL_LOCATION));

public BedrockModelLoader() {}
@Override
Expand All @@ -50,89 +46,46 @@ public void onResourceManagerReload(ResourceManager resourceManager) {
}

@Override
public UnbakedBedrockModel read(JsonObject jsonObject, @Nullable JsonDeserializationContext deserializationContext) throws JsonParseException {
public BedrockModel read(JsonObject jsonObject, @Nullable JsonDeserializationContext deserializationContext) throws JsonParseException {
return readModel(jsonObject, deserializationContext);
}

public static BedrockModel readModel(JsonObject jsonObject, @Nullable JsonDeserializationContext context) {
ResourceLocation ml = new ResourceLocation(jsonObject.get("model").getAsString());
boolean flipV = jsonObject.has("flip_v") && jsonObject.get("flip_v").getAsBoolean();

UnbakedBedrockModel model = new UnbakedBedrockModel(new ResourceLocation(ml.getNamespace(), "models/" + ml.getPath() + ".geo.json"),
new ResourceLocation(jsonObject.get("texture").getAsString()), flipV);
ArrayList<Material> materials = new ArrayList<>();
Material texture = new Material(TextureAtlas.LOCATION_BLOCKS,
new ResourceLocation(jsonObject.get("texture").getAsString()));
materials.add(texture);
if (jsonObject.has("particle")) {
String particleStr = jsonObject.get("particle").getAsString();
ResourceLocation location = new ResourceLocation(particleStr);
Material material = new Material(TextureAtlas.LOCATION_PARTICLES, location);
ADDITIONAL_MATERIALS.add(material);
}
if (deserializationContext == null) {
ADDITIONAL_MATERIALS.add(model.getMaterial());
materials.add(new Material(TextureAtlas.LOCATION_PARTICLES, location));
}
BedrockModel model = new BedrockModel(
new ResourceLocation(ml.getNamespace(), "models/" + ml.getPath() + ".geo.json"),
flipV, texture, materials);
ResourceLocation identifier = new ResourceLocation(jsonObject.get("identifier").getAsString());
MODELS.put(identifier, model);
return model;
}

@Override
public HashMap<ItemTransforms.TransformType, ItemTransform> generate(JsonObject jsonObject, Type type, JsonDeserializationContext context) {
ResourceLocation ml = new ResourceLocation(jsonObject.get("model").getAsString());
JsonArray geoJson;
try {
Resource resource = Resources.getResource(new ResourceLocation(ml.getNamespace(), "models/" + ml.getPath() + ".geo.json"));
JsonObject geo = JsonParser.parseReader(resource.openAsReader()).getAsJsonObject();
geoJson = geo.getAsJsonArray("minecraft:geometry");
} catch (IOException e) {
KasugaLib.MAIN_LOGGER.error("Failed to read Model: ", e);
return null;
}
if (geoJson == null) {
KasugaLib.MAIN_LOGGER.error("Failed to parse Model: " + ml);
return null;
}
HashMap<ItemTransforms.TransformType, ItemTransform> result = Maps.newHashMap();
for (JsonElement geometry : geoJson.getAsJsonArray()) {
result.putAll(Geometry.parseTransforms(geometry.getAsJsonObject()));
}
return result;
}

public static List<AnimModel> getModels(ResourceLocation location, RenderType type) {
UnbakedBedrockModel unbaked = MODELS.getOrDefault(location, null);
if (unbaked == null) return null;
BedrockModel unbaked = MODELS.getOrDefault(location, null);
if (unbaked == null) return List.of();
List<Geometry> geometry = unbaked.getGeometries();
ArrayList<AnimModel> result = new ArrayList<>(geometry.size());
geometry.forEach(g -> result.add(g.getAnimationModel(type)));
return result;
}

public static AnimModel getModel(ResourceLocation location, RenderType type) {
UnbakedBedrockModel unbaked = MODELS.getOrDefault(location, null);
BedrockModel unbaked = MODELS.getOrDefault(location, null);
if (unbaked == null) return null;
List<Geometry> geometries = unbaked.getGeometries();
if (geometries.isEmpty()) return null;
Geometry geometry = geometries.get(0);
return geometry.getAnimationModel(type);
}

public static LazyRecomputable<UnbakedBedrockModel> fromFile(ResourceLocation location) {
ResourceLocation location1 =
new ResourceLocation(location.getNamespace(), "models/" + location.getPath() + ".json");
if (!registerFired) {
UNREGISTERED.add(location);
return LazyRecomputable.of(() -> MODELS.getOrDefault(location, null));
} else if (MODELS.containsKey(location)) {
MODELS.get(location);
}
try {
Resource resource = Resources.getResource(location1);
JsonElement element = JsonParser.parseReader(resource.openAsReader());
if (!element.isJsonObject()) {
KasugaLib.MAIN_LOGGER.error(location + " is not a JsonObject");
return MISSING;
}
JsonObject jsObj = element.getAsJsonObject();
UnbakedBedrockModel model = INSTANCE.read(jsObj, null);
MODELS.put(location, model);
return LazyRecomputable.of(() -> model);
} catch (IOException e) {
KasugaLib.MAIN_LOGGER.error("Failed to load model file" + location, e);
return MISSING;
}

}
}
Loading

0 comments on commit 86887e3

Please sign in to comment.