Fix item exporting. Closes #2445. Thanks to ramidzkh
This commit is contained in:
parent
99c46f0a56
commit
2d9cdfbdc9
1 changed files with 30 additions and 83 deletions
|
@ -32,21 +32,16 @@ import net.minecraft.client.MinecraftClient;
|
|||
import net.minecraft.client.gl.Framebuffer;
|
||||
import net.minecraft.client.gl.SimpleFramebuffer;
|
||||
import net.minecraft.client.render.DiffuseLighting;
|
||||
import net.minecraft.client.render.OverlayTexture;
|
||||
import net.minecraft.client.render.VertexConsumerProvider;
|
||||
import net.minecraft.client.render.item.ItemRenderer;
|
||||
import net.minecraft.client.render.model.BakedModel;
|
||||
import net.minecraft.client.render.model.json.ModelTransformation;
|
||||
import net.minecraft.client.texture.NativeImage;
|
||||
import net.minecraft.client.texture.SpriteAtlasTexture;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.Matrix4f;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.lwjgl.opengl.GL12;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
* Initially take from https://github.com/JamiesWhiteShirt/developer-mode/tree/experimental-item-render and then ported to 1.15
|
||||
|
@ -54,100 +49,52 @@ import java.io.File;
|
|||
*/
|
||||
public class ItemStackRenderer implements HudRenderCallback {
|
||||
|
||||
private static final int SIZE = 512;
|
||||
|
||||
@Override
|
||||
public void onHudRender(MatrixStack matrixStack, float v) {
|
||||
if (!ItemStackRenderManager.RENDER_QUEUE.isEmpty()) {
|
||||
|
||||
MinecraftClient.getInstance().textRenderer.draw(matrixStack, "Rendering " + ItemStackRenderManager.RENDER_QUEUE.size() + " items left", 5, 5, -1);
|
||||
|
||||
ItemStack itemStack = ItemStackRenderManager.RENDER_QUEUE.poll();
|
||||
export(matrixStack, itemStack, 512, Registry.ITEM.getId(itemStack.getItem()));
|
||||
ItemStack itemStack = ItemStackRenderManager.RENDER_QUEUE.remove();
|
||||
Identifier id = Registry.ITEM.getId(itemStack.getItem());
|
||||
MinecraftClient.getInstance().textRenderer.draw(matrixStack, "Rendering " + id + ", " + ItemStackRenderManager.RENDER_QUEUE.size() + " items left", 5, 5, -1);
|
||||
export(id, itemStack);
|
||||
}
|
||||
}
|
||||
|
||||
private void export(MatrixStack matrixStack, ItemStack stack, int size, Identifier identifier) {
|
||||
File dir = new File(FabricLoader.getInstance().getGameDir().toFile(), "item_renderer/" + identifier.getNamespace());
|
||||
if (!dir.exists()) {
|
||||
dir.mkdir();
|
||||
}
|
||||
File file = new File(dir, identifier.getPath() + ".png");
|
||||
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
MinecraftClient minecraft = MinecraftClient.getInstance();
|
||||
|
||||
if (minecraft.getItemRenderer() == null || minecraft.world == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
final Framebuffer framebuffer = new SimpleFramebuffer(size, size, true, true);
|
||||
framebuffer.setClearColor(0.0F, 0.0F, 0.0F, 0.0F);
|
||||
framebuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
|
||||
|
||||
framebuffer.beginWrite(true);
|
||||
|
||||
final ItemRenderer itemRenderer = MinecraftClient.getInstance().getItemRenderer();
|
||||
final BakedModel model = itemRenderer.getHeldItemModel(stack, minecraft.world, minecraft.player, 0);
|
||||
|
||||
// FIXME 1.17
|
||||
Matrix4f matrix4f = Matrix4f.projectionMatrix(-1, 1, 1, -1, -100.0F, -100.0F); // ortho
|
||||
RenderSystem.backupProjectionMatrix();
|
||||
RenderSystem.setProjectionMatrix(matrix4f);
|
||||
|
||||
matrixStack.push();
|
||||
matrixStack.loadIdentity();
|
||||
private void export(Identifier identifier, ItemStack item) {
|
||||
RenderSystem.setProjectionMatrix(Matrix4f.projectionMatrix(0, 16, 0, 16, 1000, 3000));
|
||||
MatrixStack stack = RenderSystem.getModelViewStack();
|
||||
stack.loadIdentity();
|
||||
stack.translate(0, 0, -2000);
|
||||
DiffuseLighting.enableGuiDepthLighting();
|
||||
RenderSystem.applyModelViewMatrix();
|
||||
|
||||
{
|
||||
minecraft.getTextureManager().getTexture(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE).setFilter(false, false);
|
||||
RenderSystem.setShaderTexture(0, SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE);
|
||||
Framebuffer framebuffer = new SimpleFramebuffer(SIZE, SIZE, true, MinecraftClient.IS_SYSTEM_MAC);
|
||||
|
||||
RenderSystem.enableBlend();
|
||||
RenderSystem.enableDepthTest();
|
||||
RenderSystem.blendFunc(GlStateManager.SrcFactor.SRC_ALPHA, GlStateManager.DstFactor.ONE_MINUS_SRC_ALPHA);
|
||||
try (NativeImage nativeImage = new NativeImage(SIZE, SIZE, true)) {
|
||||
framebuffer.setClearColor(0, 0, 0, 0);
|
||||
framebuffer.clear(MinecraftClient.IS_SYSTEM_MAC);
|
||||
|
||||
RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
MatrixStack matrixStack2 = new MatrixStack();
|
||||
framebuffer.beginWrite(true);
|
||||
GlStateManager._clear(GL12.GL_COLOR_BUFFER_BIT | GL12.GL_DEPTH_BUFFER_BIT, MinecraftClient.IS_SYSTEM_MAC);
|
||||
MinecraftClient.getInstance().getItemRenderer().renderInGui(item, 0, 0);
|
||||
framebuffer.endWrite();
|
||||
|
||||
matrixStack2.scale(2F, -2F, 1F);
|
||||
|
||||
boolean frontLit = !model.isSideLit();
|
||||
if (frontLit) {
|
||||
DiffuseLighting.disableGuiDepthLighting();
|
||||
}
|
||||
|
||||
VertexConsumerProvider.Immediate immediate = MinecraftClient.getInstance().getBufferBuilders().getEntityVertexConsumers();
|
||||
itemRenderer.renderItem(stack, ModelTransformation.Mode.GUI, false, matrixStack2, immediate, 15728880, OverlayTexture.DEFAULT_UV, model);
|
||||
immediate.draw();
|
||||
|
||||
RenderSystem.enableDepthTest();
|
||||
|
||||
if (frontLit) {
|
||||
DiffuseLighting.enableGuiDepthLighting();
|
||||
}
|
||||
}
|
||||
|
||||
matrixStack.pop();
|
||||
RenderSystem.applyModelViewMatrix();
|
||||
RenderSystem.restoreProjectionMatrix();
|
||||
|
||||
framebuffer.endWrite();
|
||||
|
||||
try (NativeImage nativeImage = new NativeImage(size, size, false)) {
|
||||
RenderSystem.bindTexture(framebuffer.getColorAttachment());
|
||||
// framebuffer.beginRead
|
||||
framebuffer.method_35610();
|
||||
nativeImage.loadFromTextureImage(0, false);
|
||||
nativeImage.mirrorVertically();
|
||||
framebuffer.endRead();
|
||||
|
||||
try {
|
||||
byte[] bytes = nativeImage.getBytes();
|
||||
FileUtils.writeByteArrayToFile(file, bytes);
|
||||
System.out.println("Wrote " + file.getAbsolutePath());
|
||||
Path path = FabricLoader.getInstance().getGameDir().resolve("item_renderer").resolve(identifier.getNamespace()).resolve(identifier.getPath() + ".png");
|
||||
Files.createDirectories(path.getParent());
|
||||
nativeImage.writeFile(path);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
framebuffer.delete();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue