Skip to content

Commit

Permalink
修复:模型不能正确加载Material的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MegumiKasuga committed Oct 29, 2024
1 parent 79868c2 commit fbd3a3c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"variants": {
"": {
"model": "kasuga_lib:block/test/test_model_complicate"
}

},
"": {
"model": "kasuga_lib:block/test/test_model_complicate"
}
}
6 changes: 3 additions & 3 deletions src/generated/resources/kasuga.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"client": [
"client.MixinBlockModel$Deserializer",
"client.MixinBlockModelBinding",
"client.MixinTextureManager",
"client.MixinBlockModelBinding",
"client.MixinBlockModelShaper"
"client.MixinBlockModelShaper",
"client.MixinModelBakery",
"client.MixinTextureManager"
],
"minVersion": "0.8"
}
18 changes: 10 additions & 8 deletions src/main/java/kasuga/lib/core/client/model/BedrockModelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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 Down Expand Up @@ -35,15 +36,13 @@ public class BedrockModelLoader implements IGeometryLoader<UnbakedBedrockModel>,
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 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));

public BedrockModelLoader() {
}
public BedrockModelLoader() {}
@Override
public void onResourceManagerReload(ResourceManager resourceManager) {
this.manager = resourceManager;
Expand All @@ -60,9 +59,11 @@ public UnbakedBedrockModel read(JsonObject jsonObject, @Nullable JsonDeserializa
if (jsonObject.has("particle")) {
String particleStr = jsonObject.get("particle").getAsString();
ResourceLocation location = new ResourceLocation(particleStr);
Material material = new Material(TextureAtlas.LOCATION_PARTICLES,
new ResourceLocation(location.getNamespace(), "textures/" + location.getPath() + ".png")
);
Material material = new Material(TextureAtlas.LOCATION_PARTICLES, location);
ADDITIONAL_MATERIALS.add(material);
}
if (deserializationContext == null) {
ADDITIONAL_MATERIALS.add(model.getMaterial());
}
return model;
}
Expand Down Expand Up @@ -124,7 +125,8 @@ public static LazyRecomputable<UnbakedBedrockModel> fromFile(ResourceLocation lo
KasugaLib.MAIN_LOGGER.error(location + " is not a JsonObject");
return MISSING;
}
UnbakedBedrockModel model = INSTANCE.read(element.getAsJsonObject(), null);
JsonObject jsObj = element.getAsJsonObject();
UnbakedBedrockModel model = INSTANCE.read(jsObj, null);
MODELS.put(location, model);
return LazyRecomputable.of(() -> model);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraftforge.client.model.geometry.IGeometryBakingContext;
import net.minecraftforge.client.model.geometry.SimpleUnbakedGeometry;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
Expand All @@ -42,17 +43,26 @@ public UnbakedBedrockModel(ResourceLocation modelLocation, ResourceLocation text
this.textureLocation = textureLocation;
geometries = Lists.newArrayList();
legacy = false;
parse();
parse(null);
}

public void parse() {
public UnbakedBedrockModel(ResourceLocation modelLocation, Material material, boolean flipV) {
this.flipV = flipV;
this.modelLocation = modelLocation;
this.textureLocation = material.texture();
this.geometries = new ArrayList<>();
legacy = false;
parse(material);
}

public void parse(@Nullable Material material) {
JsonObject model = readModel();
if (model == null) {
KasugaLib.MAIN_LOGGER.warn("Unable to open animated model: " + this.modelLocation.toString());
return;
}
formatVersion = model.get("format_version").getAsString();
this.material = new Material(TextureAtlas.LOCATION_BLOCKS, textureLocation);
this.material = material == null ? new Material(TextureAtlas.LOCATION_BLOCKS, textureLocation) : material;

JsonArray geos;
if (model.has("minecraft:geometry")) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/kasuga/lib/mixins/mixin/client/MixinModelBakery.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kasuga.lib.mixins.mixin.client;

import kasuga.lib.core.client.model.BedrockModelLoader;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBakery;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Collection;
import java.util.Set;

@Mixin(ModelBakery.class)
public class MixinModelBakery {

@Redirect(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/util/Set;addAll(Ljava/util/Collection;)Z"))
private boolean addAll(Set<Material> instance, Collection<Material> es) {
return instance.addAll(es) & instance.addAll(BedrockModelLoader.ADDITIONAL_MATERIALS);
}
}

0 comments on commit fbd3a3c

Please sign in to comment.