Skip to content

Commit

Permalink
修改缓存机制,加快缓存速度
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Sep 6, 2024
1 parent 89a7bef commit fbbb74e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import com.github.tartaricacid.touhoulittlemaid.init.InitEntities;
import com.github.tartaricacid.touhoulittlemaid.tileentity.TileEntityModelSwitcher;
import com.github.tartaricacid.touhoulittlemaid.util.RenderHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.inventory.InventoryScreen;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

Expand Down Expand Up @@ -71,29 +71,31 @@ public static void openModelSwitcherModelGui(EntityMaid maid, TileEntityModelSwi
}

private static CacheScreen<EntityMaid, MaidModelInfo> getMaidCacheScreen(AbstractModelGui<EntityMaid, MaidModelInfo> maidModelGui) {
return new CacheScreen<>(maidModelGui, InitEntities.MAID.get(), MAID_CACHE_QUEUE, (graphics, posX, posY, modelInfo, scaleModified, maid) -> {
return new CacheScreen<>(maidModelGui, InitEntities.MAID.get(), MAID_CACHE_QUEUE, (graphics, posX, posY, posZ, modelInfo, scaleModified, maid) -> {
clearMaidDataResidue(maid, false);
if (modelInfo.getEasterEgg() != null) {
maid.setModelId(EASTER_EGG_MODEL);
} else {
maid.setModelId(modelInfo.getModelId().toString());
}
int scale = scaleModified / 2;
InventoryScreen.renderEntityInInventory(
RenderHelper.renderEntityInInventory(
posX + scale,
posY + scaleModified - 5,
posZ,
(int) (scale * modelInfo.getRenderItemScale() * 0.9),
-25, -20, maid);
});
}

private static CacheScreen<EntityChair, ChairModelInfo> getChairCacheScreen(ChairModelGui chairModelGui) {
return new CacheScreen<>(chairModelGui, InitEntities.CHAIR.get(), CHAIR_CACHE_QUEUE, (graphics, posX, posY, modelInfo, scaleModified, chair) -> {
return new CacheScreen<>(chairModelGui, InitEntities.CHAIR.get(), CHAIR_CACHE_QUEUE, (graphics, posX, posY, posZ, modelInfo, scaleModified, chair) -> {
chair.setModelId(modelInfo.getModelId().toString());
int scale = scaleModified / 2;
InventoryScreen.renderEntityInInventory(
RenderHelper.renderEntityInInventory(
posX + scale,
posY + scaleModified - 5,
posZ,
(int) (scale * modelInfo.getRenderItemScale() * 0.9),
-25, -20, chair);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.tartaricacid.touhoulittlemaid.client.gui.entity.cache;

import com.github.tartaricacid.touhoulittlemaid.TouhouLittleMaid;
import com.github.tartaricacid.touhoulittlemaid.client.renderer.texture.CacheIconTexture;
import com.github.tartaricacid.touhoulittlemaid.client.resource.pojo.IModelInfo;
import com.github.tartaricacid.touhoulittlemaid.util.EntityCacheUtil;
Expand All @@ -16,9 +17,11 @@
import net.minecraft.world.level.Level;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.commons.lang3.time.StopWatch;

import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;


@OnlyIn(Dist.CLIENT)
Expand All @@ -28,18 +31,20 @@ public class CacheScreen<T extends LivingEntity, E extends IModelInfo> extends S
private final Queue<E> modelInfos;
private final EntityRender<T, E> entityRender;
private final int totalCount;
private final StopWatch stopWatch;

public CacheScreen(Screen parent, EntityType<T> entityType, Queue<E> modelInfos, EntityRender<T, E> entityRender) {
super(new TextComponent(" Cache Screen"));
super(new TextComponent("Cache Screen"));
this.parent = parent;
this.entityType = entityType;
this.modelInfos = modelInfos;
this.entityRender = entityRender;
this.totalCount = modelInfos.size();
this.stopWatch = StopWatch.createStarted();
}

@SuppressWarnings("unchecked")
private void drawEntity(PoseStack poseStack, int posX, int posY, E modelInfo, int scaleModified) {
private void drawEntity(PoseStack pPoseStack, int posX, int posY, int posZ, E modelInfo, int scaleModified) {
Level world = getMinecraft().level;
if (world == null) {
return;
Expand All @@ -51,7 +56,7 @@ private void drawEntity(PoseStack poseStack, int posX, int posY, E modelInfo, in
e.fillInStackTrace();
return;
}
entityRender.render(poseStack, posX, posY, modelInfo, scaleModified, entity);
entityRender.render(pPoseStack, posX, posY, posZ, modelInfo, scaleModified, entity);
}

@Override
Expand All @@ -60,30 +65,48 @@ public void render(PoseStack poseStack, int mouseX, int mouseY, float partialTic
super.render(poseStack, mouseX, mouseY, partialTick);

if (modelInfos.isEmpty()) {
stopWatch.stop();
double timeCost = stopWatch.getTime(TimeUnit.MILLISECONDS) / 1000.0;
TouhouLittleMaid.LOGGER.info("Cache icon time: {} seconds", timeCost);
Minecraft.getInstance().setScreen(parent);
return;
}

// 每帧尝试缓存 5 个
poseStack.pushPose();
int posZ = 0;
for (int i = 0; i < 5; i++) {
if (modelInfos.isEmpty()) {
return;
}
posZ += 100;
poseStack.translate(0, 0, 100);
doCacheIcon(poseStack, posZ);
}
poseStack.popPose();

int finishSize = totalCount - modelInfos.size();
drawCenteredString(poseStack, font, new TranslatableComponent("gui.touhou_little_maid.cache_screen.progress", finishSize, totalCount), this.width / 2, this.height - 42, 0xFFFFFF);
drawCenteredString(poseStack, font, new TranslatableComponent("gui.touhou_little_maid.cache_screen.desc"), this.width / 2, this.height - 30, 0xFFFFFF);
}

private void doCacheIcon(PoseStack poseStack, int posZ) {
E modelInfo = modelInfos.poll();
if (modelInfo != null) {
double guiScale = Minecraft.getInstance().getWindow().getGuiScale();
int scaleModified = (int) Math.ceil((256 / guiScale));

fill(poseStack, 0, 0, scaleModified, scaleModified + 2, IconCache.BACKGROUND_COLOR);
this.drawEntity(poseStack, 0, 0, modelInfo, scaleModified);
this.drawEntity(poseStack, 0, 0, posZ, modelInfo, scaleModified);
NativeImage nativeImage = IconCache.exportImageFromScreenshot(256, IconCache.BACKGROUND_COLOR_SHIFTED);

ResourceLocation modelId = modelInfo.getModelId();
CacheIconTexture cacheIconTexture = new CacheIconTexture(modelId, nativeImage);
Minecraft.getInstance().textureManager.register(modelInfo.getCacheIconId(), cacheIconTexture);
}

int finishSize = totalCount - modelInfos.size();
drawCenteredString(poseStack, font, new TranslatableComponent("gui.touhou_little_maid.cache_screen.progress", finishSize, totalCount), this.width / 2, this.height - 42, 0xFFFFFF);
drawCenteredString(poseStack, font, new TranslatableComponent("gui.touhou_little_maid.cache_screen.desc"), this.width / 2, this.height - 30, 0xFFFFFF);
}

public interface EntityRender<T extends LivingEntity, E extends IModelInfo> {
void render(PoseStack poseStack, int posX, int posY, E modelInfo, int scaleModified, T entity);
void render(PoseStack pPoseStack, int posX, int posY, int posZ, E modelInfo, int scaleModified, T entity);
}
}

0 comments on commit fbbb74e

Please sign in to comment.