Skip to content

Commit

Permalink
Add optimization for DynamicTrainModel with BBMODEL
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 30, 2023
1 parent cb8c7aa commit eb8fd0a
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,27 @@
import cn.zbx1425.sowcer.util.GlStateTracker;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import mtr.client.ClientData;
import mtr.client.ICustomResources;
import mtr.client.TrainClientRegistry;
import mtr.mappings.Utilities;
import mtr.mappings.UtilitiesClient;
import mtr.render.TrainRendererBase;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.function.Consumer;
import java.util.function.Function;
Expand All @@ -48,22 +57,39 @@ private static void reloadTail(ResourceManager manager, CallbackInfo ci) {
TrainRendererBase renderer = TrainClientRegistry.getTrainProperties(train.trainId).renderer;
((TrainClientAccessor)train).setTrainRenderer(renderer.createTrainInstance(train));
});
if (MtrModelRegistryUtil.loadingErrorList.size() > 0) {
if (!MtrModelRegistryUtil.loadingErrorList.isEmpty()) {
Minecraft.getInstance().setScreen(new ErrorScreen(MtrModelRegistryUtil.loadingErrorList, Minecraft.getInstance().screen));
}
GlStateTracker.restore();
}

@Inject(at = @At("HEAD"), method = "readResource", cancellable = true)
private static void readResource(ResourceManager manager, String path, Consumer<JsonObject> callback, CallbackInfo ci) {
JsonObject dummyBbData = MtrModelRegistryUtil.createDummyBbDataPack(path, capturedTextureId, capturedFlipV);
if (path.toLowerCase(Locale.ROOT).endsWith(".obj") || path.contains("|")) {
JsonObject dummyBbData = MtrModelRegistryUtil.createDummyBbDataPack(path, capturedTextureId, capturedFlipV);
callback.accept(dummyBbData);
ci.cancel();
} else {
try {
UtilitiesClient.getResources(manager, new ResourceLocation(path)).forEach(resource -> {
try (final InputStream stream = Utilities.getInputStream(resource)) {
JsonObject modelObject = new JsonParser().parse(new InputStreamReader(stream, StandardCharsets.UTF_8)).getAsJsonObject();
if (path.toLowerCase(Locale.ROOT).endsWith(".bbmodel")) {
modelObject.add("dummyBbData", dummyBbData);
}
callback.accept(modelObject);
} catch (Exception e) { Main.LOGGER.error("On behalf of MTR: Parsing JSON " + path, e); }
try {
Utilities.closeResource(resource);
} catch (IOException e) { Main.LOGGER.error("On behalf of MTR: Closing resource " + path, e); }
});
} catch (Exception ignored) { }
}
ci.cancel();
}

@Unique
private static String capturedTextureId = "";
@Unique
private static boolean capturedFlipV = false;

@Inject(at = @At("RETURN"), method = "getOrDefault", remap = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.zbx1425.mtrsteamloco.mixin;

import mtr.mappings.ModelMapper;
import net.minecraft.client.model.geom.ModelPart;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(ModelMapper.class)
public interface ModelMapperAccessor {

@Accessor
ModelPart getModelPart();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cn.zbx1425.mtrsteamloco.mixin;

import net.minecraft.client.model.geom.ModelPart;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;
import java.util.Map;

@Mixin(ModelPart.class)
public interface ModelPartAccessor {

@Accessor
List<ModelPart.Cube> getCubes();

@Accessor
Map<String, ModelPart> getChildren();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package cn.zbx1425.mtrsteamloco.render.integration;

import cn.zbx1425.mtrsteamloco.mixin.ModelPartAccessor;
import cn.zbx1425.sowcer.batch.MaterialProp;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.Face;
import cn.zbx1425.sowcerext.model.RawMesh;
import cn.zbx1425.sowcerext.model.RawModel;
import cn.zbx1425.sowcerext.model.Vertex;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import mtr.model.ModelTrainBase;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;

Expand All @@ -24,6 +27,29 @@ public CapturingVertexConsumer() {
Arrays.setAll(models, ignored -> new RawModel());
}

public void captureModelPart(ModelPart modelPart) {
dumpModelPartQuads(modelPart, new PoseStack(), this, 0, 0);
}

// Sodium mixins into ModelPart.compile, so cannot directly call that
public static void dumpModelPartQuads(ModelPart modelPart, PoseStack poseStack, VertexConsumer vertexConsumer, int packedLight, int packedOverlay) {
if (modelPart.visible) {
if (!((ModelPartAccessor)(Object)modelPart).getCubes().isEmpty() || !((ModelPartAccessor)(Object)modelPart).getChildren().isEmpty()) {
poseStack.pushPose();
modelPart.translateAndRotate(poseStack);
if (!modelPart.skipDraw) {
for (ModelPart.Cube cube : ((ModelPartAccessor)(Object)modelPart).getCubes()) {
cube.compile(poseStack.last(), vertexConsumer, packedLight,packedOverlay, 1, 1, 1, 1);
}
}
for (ModelPart child : ((ModelPartAccessor)(Object)modelPart).getChildren().values()) {
dumpModelPartQuads(child, poseStack, vertexConsumer, packedLight, packedOverlay);
}
poseStack.popPose();
}
}
}

public void beginStage(ResourceLocation texture, ModelTrainBase.RenderStage stage) {
MaterialProp materialProp = new MaterialProp();
materialProp.texture = texture;
Expand Down
Loading

0 comments on commit eb8fd0a

Please sign in to comment.