Fix and improve itemstack renderer

This commit is contained in:
modmuss50 2023-05-13 18:51:18 +01:00 committed by modmuss
parent e5b5c399ef
commit a4dd50bf8d

View file

@ -24,8 +24,8 @@
package reborncore.client; package reborncore.client;
import com.mojang.blaze3d.platform.GlStateManager;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.systems.VertexSorter;
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback; import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
@ -39,7 +39,6 @@ import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries; import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.lwjgl.opengl.GL12;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
@ -58,15 +57,19 @@ public class ItemStackRenderer implements HudRenderCallback {
if (!ItemStackRenderManager.RENDER_QUEUE.isEmpty()) { if (!ItemStackRenderManager.RENDER_QUEUE.isEmpty()) {
ItemStack itemStack = ItemStackRenderManager.RENDER_QUEUE.remove(); ItemStack itemStack = ItemStackRenderManager.RENDER_QUEUE.remove();
Identifier id = Registries.ITEM.getId(itemStack.getItem()); Identifier id = Registries.ITEM.getId(itemStack.getItem());
drawContext.drawText(MinecraftClient.getInstance().textRenderer, "Rendering " + id + ", " + ItemStackRenderManager.RENDER_QUEUE.size() + " items left", 5, 5, -1, false); drawContext.drawText(MinecraftClient.getInstance().textRenderer, "Rendering " + id, 5, 5, -1, false);
drawContext.drawText(MinecraftClient.getInstance().textRenderer, ItemStackRenderManager.RENDER_QUEUE.size() + " items left", 5, 15, -1, false);
export(id, itemStack); export(id, itemStack);
} }
} }
private void export(Identifier identifier, ItemStack item) { private void export(Identifier identifier, ItemStack item) {
Matrix4f matrix4f = new Matrix4f().setOrtho(0, 16, 0, 16, 1000, 3000); MinecraftClient client = MinecraftClient.getInstance();
RenderSystem.setProjectionMatrix(matrix4f, null); // TODO 1.20
Matrix4f matrix4f = new Matrix4f().setOrtho(0, 16, 16, 0, 1000, 3000);
RenderSystem.setProjectionMatrix(matrix4f, VertexSorter.BY_Z);
MatrixStack stack = RenderSystem.getModelViewStack(); MatrixStack stack = RenderSystem.getModelViewStack();
stack.push();
stack.loadIdentity(); stack.loadIdentity();
stack.translate(0, 0, -2000); stack.translate(0, 0, -2000);
DiffuseLighting.enableGuiDepthLighting(); DiffuseLighting.enableGuiDepthLighting();
@ -78,15 +81,20 @@ public class ItemStackRenderer implements HudRenderCallback {
framebuffer.setClearColor(0, 0, 0, 0); framebuffer.setClearColor(0, 0, 0, 0);
framebuffer.clear(MinecraftClient.IS_SYSTEM_MAC); framebuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
framebuffer.beginWrite(true); {
GlStateManager._clear(GL12.GL_COLOR_BUFFER_BIT | GL12.GL_DEPTH_BUFFER_BIT, MinecraftClient.IS_SYSTEM_MAC); framebuffer.beginWrite(true);
// MinecraftClient.getInstance().getItemRenderer().renderInGui(stack, item, 0, 0); // TODO 1.20 DrawContext drawContext = new DrawContext(client, client.getBufferBuilders().getEntityVertexConsumers());
framebuffer.endWrite(); drawContext.drawItem(item, 0, 0);
drawContext.draw();
framebuffer.endWrite();
}
framebuffer.beginRead(); {
nativeImage.loadFromTextureImage(0, false); framebuffer.beginRead();
nativeImage.mirrorVertically(); nativeImage.loadFromTextureImage(0, false);
framebuffer.endRead(); nativeImage.mirrorVertically();
framebuffer.endRead();
}
try { try {
Path path = FabricLoader.getInstance().getGameDir().resolve("item_renderer").resolve(identifier.getNamespace()).resolve(identifier.getPath() + ".png"); Path path = FabricLoader.getInstance().getGameDir().resolve("item_renderer").resolve(identifier.getNamespace()).resolve(identifier.getPath() + ".png");
@ -98,5 +106,7 @@ public class ItemStackRenderer implements HudRenderCallback {
} }
framebuffer.delete(); framebuffer.delete();
stack.pop();
RenderSystem.applyModelViewMatrix();
} }
} }